MCPcopy
hub / github.com/python-attrs/attrs / test_recurse_retain

Method test_recurse_retain

tests/test_funcs.py:341–377  ·  view source on GitHub ↗

Property tests for asserting collection types are retained.

(self, cls, tuple_class)

Source from the content-addressed store, hash-verified

339
340 @given(nested_classes, st.sampled_from(SEQUENCE_TYPES))
341 def test_recurse_retain(self, cls, tuple_class):
342 """
343 Property tests for asserting collection types are retained.
344 """
345 obj = cls()
346 obj_tuple = astuple(
347 obj, tuple_factory=tuple_class, retain_collection_types=True
348 )
349
350 def assert_proper_col_class(obj, obj_tuple):
351 # Iterate over all attributes, and if they are lists or mappings
352 # in the original, assert they are the same class in the dumped.
353 for index, field in enumerate(fields(obj.__class__)):
354 field_val = getattr(obj, field.name)
355 if has(field_val.__class__):
356 # This field holds a class, recurse the assertions.
357 assert_proper_col_class(field_val, obj_tuple[index])
358 elif isinstance(field_val, (list, tuple)):
359 # This field holds a sequence of something.
360 expected_type = type(obj_tuple[index])
361 assert type(field_val) is expected_type
362 for obj_e, obj_tuple_e in zip(field_val, obj_tuple[index]):
363 if has(obj_e.__class__):
364 assert_proper_col_class(obj_e, obj_tuple_e)
365 elif isinstance(field_val, dict):
366 orig = field_val
367 tupled = obj_tuple[index]
368 assert type(orig) is type(tupled)
369 for obj_e, obj_tuple_e in zip(
370 orig.items(), tupled.items()
371 ):
372 if has(obj_e[0].__class__): # Dict key
373 assert_proper_col_class(obj_e[0], obj_tuple_e[0])
374 if has(obj_e[1].__class__): # Dict value
375 assert_proper_col_class(obj_e[1], obj_tuple_e[1])
376
377 assert_proper_col_class(obj, obj_tuple)
378
379 @given(st.sampled_from(SEQUENCE_TYPES))
380 def test_filter(self, C, tuple_factory):

Callers

nothing calls this directly

Calls 1

astupleFunction · 0.90

Tested by

no test coverage detected