Skip to content

Commit 03e6571

Browse files
committed
test: Data Classes str()
Add tests for DictWrapper str() functionality, including Data Classes with: - no properties - single property - sensitive property - recursive properties (Data Classes inside Data Classes) - exceptions reading properties - exceptions when transforming a list with DictWrapper subclasses inside
1 parent d9d1bc7 commit 03e6571

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed

tests/functional/test_data_classes.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,143 @@ class DataClassSample(DictWrapper):
126126
assert event_source.items() == data.items()
127127

128128

129+
def test_dict_wrapper_str_no_property():
130+
class DataClassSample(DictWrapper):
131+
attribute = None
132+
133+
def function(self):
134+
pass
135+
136+
event_source = DataClassSample({})
137+
assert event_source._properties() == ["raw_event"]
138+
assert event_source._str_helper() == {"raw_event": "[SENSITIVE]"}
139+
assert str(event_source) == "{'raw_event': '[SENSITIVE]'}"
140+
141+
142+
def test_dict_wrapper_str_single_property():
143+
class DataClassSample(DictWrapper):
144+
attribute = None
145+
146+
def function(self):
147+
pass
148+
149+
@property
150+
def data_property(self):
151+
return "value"
152+
153+
event_source = DataClassSample({})
154+
assert event_source._properties() == ["data_property", "raw_event"]
155+
assert event_source._str_helper() == {"data_property": "value", "raw_event": "[SENSITIVE]"}
156+
assert str(event_source) == "{'data_property': 'value', 'raw_event': '[SENSITIVE]'}"
157+
158+
159+
def test_dict_wrapper_str_property_exception():
160+
class DataClassSample(DictWrapper):
161+
attribute = None
162+
163+
def function(self):
164+
pass
165+
166+
@property
167+
def data_property(self):
168+
raise Exception()
169+
170+
event_source = DataClassSample({})
171+
assert event_source._properties() == ["data_property", "raw_event"]
172+
assert event_source._str_helper() == {
173+
"data_property": "[EXCEPTION <class 'Exception'>]",
174+
"raw_event": "[SENSITIVE]",
175+
}
176+
assert str(event_source) == "{'data_property': \"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}"
177+
178+
179+
def test_dict_wrapper_str_property_list_exception():
180+
class BrokenDataClass(DictWrapper):
181+
@property
182+
def broken_data_property(self):
183+
raise Exception()
184+
185+
class DataClassSample(DictWrapper):
186+
attribute = None
187+
188+
def function(self):
189+
pass
190+
191+
@property
192+
def data_property(self):
193+
return ["string", 0, 0.0, BrokenDataClass({})]
194+
195+
event_source = DataClassSample({})
196+
assert event_source._properties() == ["data_property", "raw_event"]
197+
assert event_source._str_helper() == {
198+
"data_property": [
199+
"string",
200+
0,
201+
0.0,
202+
{"broken_data_property": "[EXCEPTION <class 'Exception'>]", "raw_event": "[SENSITIVE]"},
203+
],
204+
"raw_event": "[SENSITIVE]",
205+
}
206+
event_str = (
207+
"{'data_property': ['string', 0, 0.0, {'broken_data_property': "
208+
+ "\"[EXCEPTION <class 'Exception'>]\", 'raw_event': '[SENSITIVE]'}], 'raw_event': '[SENSITIVE]'}"
209+
)
210+
assert str(event_source) == event_str
211+
212+
213+
def test_dict_wrapper_str_recursive_property():
214+
class DataClassTerminal(DictWrapper):
215+
attribute = None
216+
217+
def function(self):
218+
pass
219+
220+
@property
221+
def terminal_property(self):
222+
return "end-recursion"
223+
224+
class DataClassRecursive(DictWrapper):
225+
attribute = None
226+
227+
def function(self):
228+
pass
229+
230+
@property
231+
def data_property(self):
232+
return DataClassTerminal({})
233+
234+
event_source = DataClassRecursive({})
235+
assert event_source._properties() == ["data_property", "raw_event"]
236+
assert event_source._str_helper() == {
237+
"data_property": {"raw_event": "[SENSITIVE]", "terminal_property": "end-recursion"},
238+
"raw_event": "[SENSITIVE]",
239+
}
240+
assert (
241+
str(event_source)
242+
== "{'data_property': {'raw_event': '[SENSITIVE]', 'terminal_property': 'end-recursion'},"
243+
+ " 'raw_event': '[SENSITIVE]'}"
244+
)
245+
246+
247+
def test_dict_wrapper_sensitive_properties_property():
248+
class DataClassSample(DictWrapper):
249+
attribute = None
250+
251+
def function(self):
252+
pass
253+
254+
_sensitive_properties = ["data_property"]
255+
256+
@property
257+
def data_property(self):
258+
return "value"
259+
260+
event_source = DataClassSample({})
261+
assert event_source._properties() == ["data_property", "raw_event"]
262+
assert event_source._str_helper() == {"data_property": "[SENSITIVE]", "raw_event": "[SENSITIVE]"}
263+
assert str(event_source) == "{'data_property': '[SENSITIVE]', 'raw_event': '[SENSITIVE]'}"
264+
265+
129266
def test_cloud_watch_dashboard_event():
130267
event = CloudWatchDashboardCustomWidgetEvent(load_event("cloudWatchDashboardEvent.json"))
131268
assert event.describe is False

0 commit comments

Comments
 (0)