Debian Codegraph @

tests/test_summarize.py

raw
1 from copy import deepcopy
2 from deepdiff.summarize import summarize, _truncate
3
4
5 class TestSummarize:
6
7 def test_empty_dict(self):
8 summary = summarize({}, max_length=50)
9 assert summary == "{}", "Empty dict should be summarized as {}"
10
11 def test_empty_list(self):
12 summary = summarize([], max_length=50)
13 assert summary == "[]", "Empty list should be summarized as []"
14
15 def test_primitive_int_truncation(self):
16 summary = summarize(1234567890123, max_length=10)
17 # The summary should be the string representation, truncated to max_length
18 assert isinstance(summary, str)
19 assert len(summary) <= 10
20
21 def test_primitive_string_no_truncation(self):
22 summary = summarize("short", max_length=50)
23 assert '"short"' == summary, "Short strings should not be truncated, but we are adding double quotes to it."
24
25 def test_small_dict_summary(self):
26 data = {"a": "alpha", "b": "beta"}
27 summary = summarize(data, max_length=50)
28 # Should be JSON-like, start with { and end with } and not exceed the max length.
29 assert summary.startswith("{") and summary.endswith("}")
30 assert len(summary) <= 50
31
32 def test_long_value_truncation_in_dict(self):
33 data = {
34 "key1": "a" * 100,
35 "key2": "b" * 50,
36 "key3": "c" * 150
37 }
38 summary = summarize(data, max_length=100)
39 # The summary should be under 100 characters and include ellipsis to indicate truncation.
40 assert len(summary) == 113, "Yes we are going slightly above"
41 assert "..." in summary
42
43 def test_nested_structure_summary1(self):
44 data = {
45 "RecordType": "CID",
46 "RecordNumber": 2719,
47 "RecordTitle": "Chloroquine",
48 "Section": [
49 {
50 "TOCHeading": "Structures",
51 "Description": "Structure depictions and information for 2D, 3D, and crystal related",
52 "Section": [
53 {
54 "TOCHeading": "2D Structure",
55 "Description": "A two-dimensional representation of the compound",
56 "DisplayControls": {"MoveToTop": True},
57 "Information": [
58 {
59 "ReferenceNumber": 69,
60 "Value": {"Boolean": [True]}
61 }
62 ]
63 },
64 {
65 "TOCHeading": "3D Conformer",
66 "Description": ("A three-dimensional representation of the compound. "
67 "The 3D structure is not experimentally determined, but computed by PubChem. "
68 "More detailed information on this conformer model is described in the PubChem3D thematic series published in the Journal of Cheminformatics."),
69 "DisplayControls": {"MoveToTop": True},
70 "Information": [
71 {
72 "ReferenceNumber": 69,
73 "Description": "Chloroquine",
74 "Value": {"Number": [2719]}
75 }
76 ]
77 }
78 ]
79 },
80 {
81 "TOCHeading": "Chemical Safety",
82 "Description": "Launch the Laboratory Chemical Safety Summary datasheet, and link to the safety and hazard section",
83 "DisplayControls": {"HideThisSection": True, "MoveToTop": True},
84 "Information": [
85 {
86 "ReferenceNumber": 69,
87 "Name": "Chemical Safety",
88 "Value": {
89 "StringWithMarkup": [
90 {
91 "String": " ",
92 "Markup": [
93 {
94 "Start": 0,
95 "Length": 1,
96 "URL": "https://pubchem.ncbi.nlm.nih.gov/images/ghs/GHS07.svg",
97 "Type": "Icon",
98 "Extra": "Irritant"
99 }
100 ]
101 }
102 ]
103 }
104 }
105 ]
106 }
107 ]
108 }
109 data_copy = deepcopy(data)
110 summary = summarize(data_copy, max_length=200)
111 assert len(summary) == 240, "Yes slightly above"
112 # Check that some expected keys are in the summary
113 assert '"RecordType"' in summary
114 assert '"RecordNumber"' in summary
115 assert '"RecordTitle"' in summary
116 expected = '{"Section":[{"Section":[{"Description":""},{"Description":""}],"Description":"Structure depictions a...ed"},{"Information":[{"Name":"C"}],"Description":"Launch the ...on"}],"RecordTitle":"Chloroquine","RecordNumber":2719,"RecordType":"CID"}'
117 assert expected == summary
118 assert data_copy == data, "We should not have modified the original data"
119
120 def test_nested_structure_summary2(self, compounds):
121 summary = summarize(compounds, max_length=200)
122 assert len(summary) == 319, "Ok yeah max_length is more like a guide"
123 data_copy = deepcopy(compounds)
124 expected = '{"Section":[{"Section":[{"Description":""},{"Description":""}],"Description":"Toxicity information r...y."},{"Section":[{"Section":["..."]},{"Section":["..."]}],"Description":"Spectral ...ds"},"..."],"Reference":[{"LicenseNote":"Use of th...e.","Description":"T...s."},{"LicenseNote":"U...e.","Description":"T"},"..."]}'
125 assert expected == summary
126 assert data_copy == compounds, "We should not have modified the original data"
127
128 def test_list_summary(self):
129 data = [1, 2, 3, 4]
130 summary = summarize(data, max_length=50)
131 # The summary should start with '[' and end with ']'
132 assert summary.startswith("[") and summary.endswith("]")
133 # When more than one element exists, expect a trailing ellipsis or indication of more elements
134 assert "..." not in summary
135
136 data2 = list(range(1, 200))
137 summary2 = summarize(data2, max_length=14)
138 assert "..." in summary2
139 expected = '[100,101,102,103,10,"..."]'
140 assert expected == summary2
141
142 def test_direct_truncate_function(self):
143 s = "abcdefghijklmnopqrstuvwxyz"
144 truncated = _truncate(s, 20)
145 assert len(truncated) == 20
146 assert "..." in truncated