Example #1
0
    def test_recurse_property(self, cls, dict_class):
        """
        Property tests for recursive asdict.
        """
        obj = cls()
        obj_dict = asdict(obj, dict_factory=dict_class)

        def assert_proper_dict_class(obj, obj_dict):
            assert isinstance(obj_dict, dict_class)
            for field in fields(obj.__class__):
                field_val = getattr(obj, field.name)
                if has(field_val.__class__):
                    # This field holds a class, recurse the assertions.
                    assert_proper_dict_class(field_val, obj_dict[field.name])
                elif isinstance(field_val, Sequence):
                    dict_val = obj_dict[field.name]
                    for item, item_dict in zip(field_val, dict_val):
                        if has(item.__class__):
                            assert_proper_dict_class(item, item_dict)
                elif isinstance(field_val, Mapping):
                    # This field holds a dictionary.
                    assert isinstance(obj_dict[field.name], dict_class)
                    for key, val in field_val.items():
                        if has(val.__class__):
                            assert_proper_dict_class(val,
                                                     obj_dict[field.name][key])

        assert_proper_dict_class(obj, obj_dict)
Example #2
0
    def test_recurse_property(self, cls, dict_class):
        """
        Property tests for recursive asdict.
        """
        obj = cls()
        obj_dict = asdict(obj, dict_factory=dict_class)

        def assert_proper_dict_class(obj, obj_dict):
            assert isinstance(obj_dict, dict_class)
            for field in fields(obj.__class__):
                field_val = getattr(obj, field.name)
                if has(field_val.__class__):
                    # This field holds a class, recurse the assertions.
                    assert_proper_dict_class(field_val, obj_dict[field.name])
                elif isinstance(field_val, Sequence):
                    dict_val = obj_dict[field.name]
                    for item, item_dict in zip(field_val, dict_val):
                        if has(item.__class__):
                            assert_proper_dict_class(item, item_dict)
                elif isinstance(field_val, Mapping):
                    # This field holds a dictionary.
                    assert isinstance(obj_dict[field.name], dict_class)
                    for key, val in field_val.items():
                        if has(val.__class__):
                            assert_proper_dict_class(val, obj_dict[key])

        assert_proper_dict_class(obj, obj_dict)
Example #3
0
 def test_lists_tuples(self, container, C):
     """
     If recurse is True, also recurse into lists.
     """
     assert {"x": 1, "y": [{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"]} == asdict(
         C(1, container([C(2, 3), C(4, 5), "a"]))
     )
Example #4
0
    def test_asdict_preserve_order(self, cls):
        """
        Field order should be preserved when dumping to OrderedDicts.
        """
        instance = cls()
        dict_instance = asdict(instance, dict_factory=OrderedDict)

        assert [a.name for a in fields(cls)] == list(dict_instance.keys())
Example #5
0
 def test_shallow(self, C, dict_factory):
     """
     Shallow asdict returns correct dict.
     """
     assert {
         "x": 1,
         "y": 2,
     } == asdict(C(x=1, y=2), False, dict_factory=dict_factory)
Example #6
0
    def test_asdict_preserve_order(self, cls):
        """
        Field order should be preserved when dumping to OrderedDicts.
        """
        instance = cls()
        dict_instance = asdict(instance, dict_factory=OrderedDict)

        assert [a.name for a in fields(cls)] == list(dict_instance.keys())
Example #7
0
 def test_shallow(self, C, dict_factory):
     """
     Shallow asdict returns correct dict.
     """
     assert {
         "x": 1,
         "y": 2,
     } == asdict(C(x=1, y=2), False, dict_factory=dict_factory)
Example #8
0
 def test_lists_tuples(self, container, C):
     """
     If recurse is True, also recurse into lists.
     """
     assert {
         "x": 1,
         "y": [{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"],
     } == asdict(C(1, container([C(2, 3), C(4, 5), "a"])))
Example #9
0
 def test_shallow(self):
     """
     Shallow asdict returns correct dict.
     """
     assert {
         "x": 1,
         "y": 2,
     } == asdict(C(x=1, y=2), False)
Example #10
0
 def test_dicts(self, C, dict_factory):
     """
     If recurse is True, also recurse into dicts.
     """
     res = asdict(C(1, {"a": C(4, 5)}), dict_factory=dict_factory)
     assert {
         "x": 1,
         "y": {"a": {"x": 4, "y": 5}},
     } == res
     assert isinstance(res, dict_factory)
Example #11
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 {
         "x": 1,
         "y": container([{"x": 2, "y": 3}, {"x": 4, "y": 5}, "a"]),
     } == asdict(C(1, container([C(2, 3), C(4, 5), "a"])),
                 retain_collection_types=True)
Example #12
0
 def test_filter(self, C, dict_factory):
     """
     Attributes that are supposed to be skipped are skipped.
     """
     assert {
         "x": {"x": 1},
     } == asdict(C(
         C(1, 2),
         C(3, 4),
     ), filter=lambda a, v: a.name != "y", dict_factory=dict_factory)
Example #13
0
 def test_filter(self, C, dict_factory):
     """
     Attributes that are supposed to be skipped are skipped.
     """
     assert {
         "x": {"x": 1},
     } == asdict(C(
         C(1, 2),
         C(3, 4),
     ), filter=lambda a, v: a.name != "y", dict_factory=dict_factory)
Example #14
0
 def test_dicts(self, C, dict_factory):
     """
     If recurse is True, also recurse into dicts.
     """
     res = asdict(C(1, {"a": C(4, 5)}), dict_factory=dict_factory)
     assert {
         "x": 1,
         "y": {"a": {"x": 4, "y": 5}},
     } == res
     assert isinstance(res, dict_factory)
Example #15
0
 def test_recurse(self, C, dict_class):
     """
     Deep asdict returns correct dict.
     """
     assert {
         "x": {"x": 1, "y": 2},
         "y": {"x": 3, "y": 4},
     } == asdict(C(
         C(1, 2),
         C(3, 4),
     ), dict_factory=dict_class)
Example #16
0
 def test_recurse(self, C, dict_class):
     """
     Deep asdict returns correct dict.
     """
     assert {
         "x": {"x": 1, "y": 2},
         "y": {"x": 3, "y": 4},
     } == asdict(C(
         C(1, 2),
         C(3, 4),
     ), dict_factory=dict_class)
Example #17
0
    def test_roundtrip(self, cls, dict_class):
        """
        Test dumping to dicts and back for Hypothesis-generated classes.
        """
        instance = cls()
        dict_instance = asdict(instance, dict_factory=dict_class)

        assert isinstance(dict_instance, dict_class)

        roundtrip_instance = cls(**dict_instance)

        assert instance == roundtrip_instance
Example #18
0
    def test_roundtrip(self, cls, dict_class):
        """
        Test dumping to dicts and back for Hypothesis-generated classes.
        """
        instance = cls()
        dict_instance = asdict(instance, dict_factory=dict_class)

        assert isinstance(dict_instance, dict_class)

        roundtrip_instance = cls(**dict_instance)

        assert instance == roundtrip_instance
Example #19
0
 def test_dicts(self):
     """
     If recurse is True, also recurse into dicts.
     """
     assert {
         "x": 1,
         "y": {
             "a": {
                 "x": 4,
                 "y": 5
             }
         },
     } == asdict(C(1, {"a": C(4, 5)}))
Example #20
0
    def test_roundtrip(self, cls, dict_class):
        """
        Test dumping to dicts and back for Hypothesis-generated classes.

        Private attributes don't round-trip (the attribute name is different
        than the initializer argument).
        """
        instance = cls()
        dict_instance = asdict(instance, dict_factory=dict_class)

        assert isinstance(dict_instance, dict_class)

        roundtrip_instance = cls(**dict_instance)

        assert instance == roundtrip_instance
Example #21
0
 def test_recurse(self):
     """
     Deep asdict returns correct dict.
     """
     assert {
         "x": {
             "x": 1,
             "y": 2
         },
         "y": {
             "x": 3,
             "y": 4
         },
     } == asdict(C(
         C(1, 2),
         C(3, 4),
     ))
Example #22
0
 def test_dicts(self, C):
     """
     If recurse is True, also recurse into dicts.
     """
     assert {"x": 1, "y": {"a": {"x": 4, "y": 5}}} == asdict(C(1, {"a": C(4, 5)}))
Example #23
0
 def test_recurse(self, C):
     """
     Deep asdict returns correct dict.
     """
     assert {"x": {"x": 1, "y": 2}, "y": {"x": 3, "y": 4}} == asdict(C(C(1, 2), C(3, 4)))
Example #24
0
 def test_shallow(self, C):
     """
     Shallow asdict returns correct dict.
     """
     assert {"x": 1, "y": 2} == asdict(C(x=1, y=2), False)