-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_serialization.py
More file actions
64 lines (46 loc) · 1.8 KB
/
test_serialization.py
File metadata and controls
64 lines (46 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import difflib
import json
import time
from jsondoc.serialize import load_page
from jsondoc.utils import diff_strings, load_json_file, timer
PAGE_PATH = "../schema/page/ex1_success.json"
def remove_null_fields(string):
"""
This is just to make the test pass, because they get removed when
we serialize the model with exclude_none=True
"""
lines = string.splitlines()
to_remove = [
'"link": null',
'"href": null',
]
# Remove lines that contain the fields
lines = [line for line in lines if all(field not in line for field in to_remove)]
return "\n".join(lines)
def remove_commas_from_end_of_lines(string):
return "\n".join([line.rstrip(",") for line in string.splitlines()])
def test_load_page():
content = load_json_file(PAGE_PATH)
# This should not raise any errors
with timer("load_page", unit="ms"):
page = load_page(content)
assert page is not None
# Serialize it again
serialized = page.model_dump_json(
serialize_as_any=True, # This argument is needed to output nested models
exclude_none=True,
)
canonical_content = json.dumps(content, indent=2, sort_keys=True)
canonical_serialized = json.dumps(json.loads(serialized), indent=2, sort_keys=True)
canonical_content = remove_null_fields(canonical_content)
# Remove commas from the end of lines
canonical_content = remove_commas_from_end_of_lines(canonical_content)
canonical_serialized = remove_commas_from_end_of_lines(canonical_serialized)
# Indented versions
# canonical_content = json.dumps
diff = diff_strings(canonical_content, canonical_serialized)
assert len(diff) == 0, (
"The serialized content is different from the original content:\n" + diff
)
if __name__ == "__main__":
test_load_page()