|
| 1 | +from dataclasses import dataclass |
1 | 2 | from unittest import TestCase |
| 3 | +from typing import Any, List, Optional |
2 | 4 | from os import remove |
3 | 5 |
|
4 | 6 | from electionguard.serializable import ( |
5 | 7 | set_deserializers, |
6 | 8 | set_serializers, |
| 9 | + read_json, |
| 10 | + read_json_file, |
| 11 | + read_json_object, |
| 12 | + write_json, |
7 | 13 | write_json_file, |
8 | 14 | write_json_object, |
9 | | - write_json, |
10 | 15 | ) |
11 | 16 |
|
12 | 17 |
|
| 18 | +@dataclass |
| 19 | +class NestedModel: |
| 20 | + """Nested model for testing""" |
| 21 | + |
| 22 | + test: int |
| 23 | + from_json_file: Optional[Any] = None |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class DataModel: |
| 28 | + """Data model for testing""" |
| 29 | + |
| 30 | + test: int |
| 31 | + nested: NestedModel |
| 32 | + array: List[NestedModel] |
| 33 | + from_json_file: Optional[Any] = None |
| 34 | + |
| 35 | + |
| 36 | +JSON_DATA: DataModel = DataModel( |
| 37 | + test=1, nested=NestedModel(test=1), array=[NestedModel(test=1)] |
| 38 | +) |
| 39 | +EXPECTED_JSON_STRING = '{"array": [{"test": 1}], "nested": {"test": 1}, "test": 1}' |
| 40 | +EXPECTED_JSON_OBJECT = { |
| 41 | + "test": 1, |
| 42 | + "nested": {"test": 1}, |
| 43 | + "array": [{"test": 1}], |
| 44 | +} |
| 45 | + |
| 46 | + |
13 | 47 | class TestSerializable(TestCase): |
14 | | - def test_write_json(self) -> None: |
15 | | - # Arrange |
16 | | - json_data = { |
17 | | - "from_json_file": {}, |
18 | | - "test": 1, |
19 | | - "nested": {"from_json_file": {}, "test": 1}, |
20 | | - "array": [{"from_json_file": {}, "test": 1}], |
21 | | - } |
22 | | - expected_json_string = ( |
23 | | - '{"test": 1, "nested": {"test": 1}, "array": [{"test": 1}]}' |
24 | | - ) |
| 48 | + def test_read_and_write_json(self) -> None: |
| 49 | + # Act |
| 50 | + json_string = write_json(JSON_DATA) |
| 51 | + |
| 52 | + # Assert |
| 53 | + self.assertEqual(json_string, EXPECTED_JSON_STRING) |
25 | 54 |
|
26 | 55 | # Act |
27 | | - json_string = write_json(json_data) |
| 56 | + read_json_data = read_json(json_string, DataModel) |
28 | 57 |
|
29 | 58 | # Assert |
30 | | - self.assertEqual(json_string, expected_json_string) |
| 59 | + self.assertEqual(read_json_data, JSON_DATA) |
31 | 60 |
|
32 | | - def test_write_json_object(self) -> None: |
33 | | - # Arrange |
34 | | - json_data = { |
35 | | - "from_json_file": {}, |
36 | | - "test": 1, |
37 | | - "nested": {"from_json_file": {}, "test": 1}, |
38 | | - "array": [{"from_json_file": {}, "test": 1}], |
39 | | - } |
40 | | - expected_json_object = { |
41 | | - "test": 1, |
42 | | - "nested": {"test": 1}, |
43 | | - "array": [{"test": 1}], |
44 | | - } |
| 61 | + def test_read_and_write_json_object(self) -> None: |
| 62 | + # Act |
| 63 | + json_object = write_json_object(JSON_DATA) |
| 64 | + |
| 65 | + # Assert |
| 66 | + self.assertEqual(json_object, EXPECTED_JSON_OBJECT) |
45 | 67 |
|
46 | 68 | # Act |
47 | | - json_object = write_json_object(json_data) |
| 69 | + read_json_data = read_json_object(json_object, DataModel) |
48 | 70 |
|
49 | 71 | # Assert |
50 | | - self.assertEqual(json_object, expected_json_object) |
| 72 | + self.assertEqual(read_json_data, JSON_DATA) |
51 | 73 |
|
52 | | - def test_write_json_file(self) -> None: |
| 74 | + def test_read_and_write_json_file(self) -> None: |
53 | 75 | # Arrange |
54 | | - json_data = { |
55 | | - "from_json_file": {}, |
56 | | - "test": 1, |
57 | | - "nested": {"from_json_file": {}, "test": 1}, |
58 | | - "array": [{"from_json_file": {}, "test": 1}], |
59 | | - } |
60 | | - expected_json_data = ( |
61 | | - '{"test": 1, "nested": {"test": 1}, "array": [{"test": 1}]}' |
62 | | - ) |
63 | 76 | file_name = "json_write_test" |
64 | 77 | json_file = file_name + ".json" |
65 | 78 |
|
66 | 79 | # Act |
67 | | - write_json_file(json_data, file_name) |
| 80 | + write_json_file(JSON_DATA, file_name) |
68 | 81 |
|
69 | 82 | # Assert |
70 | 83 | with open(json_file) as reader: |
71 | | - self.assertEqual(reader.read(), expected_json_data) |
| 84 | + self.assertEqual(reader.read(), EXPECTED_JSON_STRING) |
| 85 | + |
| 86 | + # Act |
| 87 | + read_json_data = read_json_file(DataModel, file_name) |
| 88 | + |
| 89 | + # Assert |
| 90 | + self.assertEqual(read_json_data, JSON_DATA) |
72 | 91 |
|
73 | 92 | # Cleanup |
74 | 93 | remove(json_file) |
|
0 commit comments