Ejemplo n.º 1
0
    def test_dataclass_with_list_caches_class(self):
        json_dataclass_with_list = '{"xs": [1]}'
        _ = DataClassWithList.from_json(json_dataclass_with_list)

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

        # force cache hits
        _ = DataClassWithList.from_json(json_dataclass_with_list)

        assert _get_type_hints_cache._Cache__currsize == 1
        assert _is_supported_generic_cache._Cache__currsize == 2
        assert _user_overrides_cache._Cache__currsize == 1
Ejemplo n.º 2
0
 def test_loads_many_nested(self):
     json_s = '[{"dc_with_list": {"xs": [1]}}]'
     assert (DataClassWithDataClass.schema().loads(json_s, many=True) == [
         DataClassWithDataClass(DataClassWithList([1]))
     ])
Ejemplo n.º 3
0
 def test_loads_many(self):
     json_s = '[{"xs": [1]}]'
     assert (DataClassWithList.schema().loads(
         json_s, many=True) == [DataClassWithList([1])])
Ejemplo n.º 4
0
 def test_dumps_many_nested(self):
     dumped = DataClassWithDataClass.schema().dumps(
         [DataClassWithDataClass(DataClassWithList([1]))], many=True)
     json_s = '[{"dc_with_list": {"xs": [1]}}]'
     assert dumped == json_s
Ejemplo n.º 5
0
 def test_dumps_many(self):
     actual = DataClassWithList.schema().dumps([DataClassWithList([1])],
                                               many=True)
     json_s = '[{"xs": [1]}]'
     assert actual == json_s
Ejemplo n.º 6
0
 def test_error_when_nonoptional_field_is_missing(self):
     with pytest.raises(KeyError):
         actual = DataClassWithDataClass.from_json('{"dc_with_list": {}}')
         expected = DataClassWithDataClass(DataClassWithList(None))
         assert (actual == expected)
Ejemplo n.º 7
0
 def test_warns_when_nonoptional_field_is_missing_with_infer_missing(self):
     with pytest.warns(RuntimeWarning, match='Missing value'):
         actual = DataClassWithDataClass.from_json('{"dc_with_list": {}}',
                                                   infer_missing=True)
         expected = DataClassWithDataClass(DataClassWithList(None))
         assert (actual == expected)
Ejemplo n.º 8
0
 def test_nested_dataclass(self):
     assert (DataClassWithDataClass(DataClassWithList(
         [1])).to_json() == '{"dc_with_list": {"xs": [1]}}')
Ejemplo n.º 9
0
 def test_nested_dataclass(self):
     assert (DataClassWithDataClass.from_json(
         '{"dc_with_list": {"xs": [1]}}') == DataClassWithDataClass(
             DataClassWithList([1])))
Ejemplo n.º 10
0
 def test_list(self):
     assert (
         DataClassWithList.from_json('{"xs": [1]}') == DataClassWithList(
             [1]))
Ejemplo n.º 11
0
 def test_list(self):
     assert DataClassWithList([1]).to_json() == '{"xs": [1]}'