예제 #1
0
    def test_nested_dataclass_caches_both_classes(self):
        json_dataclass_with_dataclass = '{"dc_with_list": {"xs": [1]}}'
        _ = DataClassWithDataClass.from_json(json_dataclass_with_dataclass)

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

        # force cache_hit
        _ = DataClassWithDataClass.from_json(json_dataclass_with_dataclass)

        assert _get_type_hints_cache._Cache__currsize == 2
        assert _is_supported_generic_cache._Cache__currsize == 2
        assert _user_overrides_cache._Cache__currsize == 2
예제 #2
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)
예제 #3
0
 def test_warns_when_required_field_is_none(self):
     with pytest.warns(RuntimeWarning, match='`NoneType` object'):
         assert (DataClassWithDataClass.from_json('{"dc_with_list": null}')
                 == DataClassWithDataClass(None))
예제 #4
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)
 def test_nested_dataclass(self):
     assert (DataClassWithDataClass.from_json(
         '{"dc_with_list": {"xs": [1]}}') == DataClassWithDataClass(
             DataClassWithList([1])))
예제 #6
0
 def test_warns_when_required_field_is_none(self):
     with pytest.warns(RuntimeWarning, match='None value'):
         assert (DataClassWithDataClass.from_json('{"xs": null}') ==
                 DataClassWithDataClass(None))