示例#1
0
 def test_lists_tuples(self, container, C):
     """
     If recurse is True, also recurse into lists.
     """
     assert ((1, [(2, 3), (4, 5), "a"])
             == astuple(C(1, container([C(2, 3), C(4, 5), "a"])))
             )
示例#2
0
 def test_dicts(self, C, tuple_factory):
     """
     If recurse is True, also recurse into dicts.
     """
     res = astuple(C(1, {"a": C(4, 5)}), tuple_factory=tuple_factory)
     assert tuple_factory([1, {"a": tuple_factory([4, 5])}]) == res
     assert isinstance(res, tuple_factory)
示例#3
0
    def test_recurse_retain(self, cls, tuple_class):
        """
        Property tests for asserting collection types are retained.
        """
        obj = cls()
        obj_tuple = astuple(obj, tuple_factory=tuple_class,
                            retain_collection_types=True)

        def assert_proper_col_class(obj, obj_tuple):
            # Iterate over all attributes, and if they are lists or mappings
            # in the original, assert they are the same class in the dumped.
            for index, field in enumerate(fields(obj.__class__)):
                field_val = getattr(obj, field.name)
                if has(field_val.__class__):
                    # This field holds a class, recurse the assertions.
                    assert_proper_col_class(field_val, obj_tuple[index])
                elif isinstance(field_val, (list, tuple)):
                    # This field holds a sequence of something.
                    assert type(field_val) is type(obj_tuple[index])  # noqa: E721
                    for obj_e, obj_tuple_e in zip(field_val, obj_tuple[index]):
                        if has(obj_e.__class__):
                            assert_proper_col_class(obj_e, obj_tuple_e)
                elif isinstance(field_val, dict):
                    orig = field_val
                    tupled = obj_tuple[index]
                    assert type(orig) is type(tupled)  # noqa: E721
                    for obj_e, obj_tuple_e in zip(orig.items(),
                                                  tupled.items()):
                        if has(obj_e[0].__class__):  # Dict key
                            assert_proper_col_class(obj_e[0], obj_tuple_e[0])
                        if has(obj_e[1].__class__):  # Dict value
                            assert_proper_col_class(obj_e[1], obj_tuple_e[1])

        assert_proper_col_class(obj, obj_tuple)
示例#4
0
 def test_filter(self, C, tuple_factory):
     """
     Attributes that are supposed to be skipped are skipped.
     """
     assert tuple_factory([tuple_factory([1, ]), ]) == astuple(C(
         C(1, 2),
         C(3, 4),
     ), filter=lambda a, v: a.name != "y", tuple_factory=tuple_factory)
示例#5
0
 def test_dicts_retain_type(self, container, C):
     """
     If recurse and retain_collection_types are True, also recurse
     into lists and do not convert them into list.
     """
     assert (
         (1, container({"a": (4, 5)}))
         == astuple(C(1, container({"a": C(4, 5)})),
                    retain_collection_types=True))
示例#6
0
 def test_lists_tuples_retain_type(self, container, C):
     """
     If recurse and retain_collection_types are True, also recurse
     into lists and do not convert them into list.
     """
     assert (
         (1, container([(2, 3), (4, 5), "a"]))
         == astuple(C(1, container([C(2, 3), C(4, 5), "a"])),
                    retain_collection_types=True))
示例#7
0
 def test_recurse(self, C, tuple_factory):
     """
     Deep astuple returns correct tuple.
     """
     assert (tuple_factory([tuple_factory([1, 2]),
                           tuple_factory([3, 4])])
             == astuple(C(
                         C(1, 2),
                         C(3, 4),
                         ),
                        tuple_factory=tuple_factory))
示例#8
0
    def test_roundtrip(self, cls, tuple_class):
        """
        Test dumping to tuple and back for Hypothesis-generated classes.
        """
        instance = cls()
        tuple_instance = astuple(instance, tuple_factory=tuple_class)

        assert isinstance(tuple_instance, tuple_class)

        roundtrip_instance = cls(*tuple_instance)

        assert instance == roundtrip_instance
示例#9
0
    def test_recurse_property(self, cls, tuple_class):
        """
        Property tests for recursive astuple.
        """
        obj = cls()
        obj_tuple = astuple(obj, tuple_factory=tuple_class)

        def assert_proper_tuple_class(obj, obj_tuple):
            assert isinstance(obj_tuple, tuple_class)
            for index, field in enumerate(fields(obj.__class__)):
                field_val = getattr(obj, field.name)
                if has(field_val.__class__):
                    # This field holds a class, recurse the assertions.
                    assert_proper_tuple_class(field_val, obj_tuple[index])

        assert_proper_tuple_class(obj, obj_tuple)
示例#10
0
 def test_shallow(self, C, tuple_factory):
     """
     Shallow astuple returns correct dict.
     """
     assert (tuple_factory([1, 2]) ==
             astuple(C(x=1, y=2), False, tuple_factory=tuple_factory))