@@ -79,3 +79,97 @@ def test_message_should_be_space_separated(
79
79
raw_log_output = capsys .readouterr ().out
80
80
log_output = json .loads (raw_log_output )
81
81
assert log_output ["message" ] == expected_message
82
+
83
+ def test_remove_circular_references (self , capsys : pytest .CaptureFixture [str ]):
84
+ # Create an object with a circular reference.
85
+ circ = {"b" : "foo" }
86
+ circ ["circ" ] = circ
87
+
88
+ entry = {
89
+ "severity" : "ERROR" ,
90
+ "message" : "testing circular" ,
91
+ "circ" : circ ,
92
+ }
93
+ logger .write (entry )
94
+ raw_log_output = capsys .readouterr ().err
95
+ log_output = json .loads (raw_log_output )
96
+
97
+ expected = {
98
+ "severity" : "ERROR" ,
99
+ "message" : "testing circular" ,
100
+ "circ" : {"b" : "foo" , "circ" : "[CIRCULAR]" },
101
+ }
102
+ assert log_output == expected
103
+
104
+ def test_remove_circular_references_in_arrays (self , capsys : pytest .CaptureFixture [str ]):
105
+ # Create an object with a circular reference inside an array.
106
+ circ = {"b" : "foo" }
107
+ circ ["circ" ] = [circ ]
108
+
109
+ entry = {
110
+ "severity" : "ERROR" ,
111
+ "message" : "testing circular" ,
112
+ "circ" : circ ,
113
+ }
114
+ logger .write (entry )
115
+ raw_log_output = capsys .readouterr ().err
116
+ log_output = json .loads (raw_log_output )
117
+
118
+ expected = {
119
+ "severity" : "ERROR" ,
120
+ "message" : "testing circular" ,
121
+ "circ" : {"b" : "foo" , "circ" : ["[CIRCULAR]" ]},
122
+ }
123
+ assert log_output == expected
124
+
125
+ def test_no_false_circular_for_duplicates (self , capsys : pytest .CaptureFixture [str ]):
126
+ # Ensure that duplicate objects (used in multiple keys) are not marked as circular.
127
+ obj = {"a" : "foo" }
128
+ entry = {
129
+ "severity" : "ERROR" ,
130
+ "message" : "testing circular" ,
131
+ "a" : obj ,
132
+ "b" : obj ,
133
+ }
134
+ logger .write (entry )
135
+ raw_log_output = capsys .readouterr ().err
136
+ log_output = json .loads (raw_log_output )
137
+
138
+ expected = {
139
+ "severity" : "ERROR" ,
140
+ "message" : "testing circular" ,
141
+ "a" : {"a" : "foo" },
142
+ "b" : {"a" : "foo" },
143
+ }
144
+ assert log_output == expected
145
+
146
+ def test_no_false_circular_in_array_duplicates (self , capsys : pytest .CaptureFixture [str ]):
147
+ # Ensure that duplicate objects in arrays are not falsely detected as circular.
148
+ obj = {"a" : "foo" }
149
+ arr = [
150
+ {"a" : obj , "b" : obj },
151
+ {"a" : obj , "b" : obj },
152
+ ]
153
+ entry = {
154
+ "severity" : "ERROR" ,
155
+ "message" : "testing circular" ,
156
+ "a" : arr ,
157
+ "b" : arr ,
158
+ }
159
+ logger .write (entry )
160
+ raw_log_output = capsys .readouterr ().err
161
+ log_output = json .loads (raw_log_output )
162
+
163
+ expected = {
164
+ "severity" : "ERROR" ,
165
+ "message" : "testing circular" ,
166
+ "a" : [
167
+ {"a" : {"a" : "foo" }, "b" : {"a" : "foo" }},
168
+ {"a" : {"a" : "foo" }, "b" : {"a" : "foo" }},
169
+ ],
170
+ "b" : [
171
+ {"a" : {"a" : "foo" }, "b" : {"a" : "foo" }},
172
+ {"a" : {"a" : "foo" }, "b" : {"a" : "foo" }},
173
+ ],
174
+ }
175
+ assert log_output == expected
0 commit comments