Exemple #1
0
    def test_dataclass_with_xs_caches_class(self):
        json_dataclass_with_xs = '{"xs": [{"x": 1}]}'
        _ = DataClassXs.from_json(json_dataclass_with_xs)

        # type hints for class and DataClassX cached
        assert _get_type_hints_cache._Cache__currsize == 2
        # supported generic called for List[DataClassX] and int
        assert _is_supported_generic_cache._Cache__currsize == 2
        # for the dataclasses
        assert _user_overrides_cache._Cache__currsize == 2

        # force cache hits
        _ = DataClassXs.from_json(json_dataclass_with_xs)

        assert _get_type_hints_cache._Cache__currsize == 2
        assert _is_supported_generic_cache._Cache__currsize == 2
        assert _user_overrides_cache._Cache__currsize == 2
Exemple #2
0
    def test_wrong_nested_list_of_dataclasses(self):
        try:
            _ = DataClassX.from_json('{"a": "incorrect_field"}')
            x_error = None
        except Exception as e:
            x_error = e
        assert x_error is not None

        try:
            _ = DataClassXs.from_json('{"xs": [{"a": "incorrect_field"}]}')
            xs_error = None
        except Exception as e:
            xs_error = e
        assert xs_error is not None
        assert x_error.__class__ == xs_error.__class__
        assert x_error.args == xs_error.args
 def test_nested_list_of_dataclasses(self):
     assert (DataClassXs([DataClassX(0), DataClassX(1)
                          ]).to_json() == '{"xs": [{"x": 0}, {"x": 1}]}')