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
def test_loads_many_nested(self): json_s = '[{"dc_with_list": {"xs": [1]}}]' assert (DataClassWithDataClass.schema().loads(json_s, many=True) == [ DataClassWithDataClass(DataClassWithList([1])) ])
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
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)
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))
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(DataClassWithList( [1])).to_json() == '{"dc_with_list": {"xs": [1]}}')
def test_nested_dataclass(self): assert (DataClassWithDataClass.from_json( '{"dc_with_list": {"xs": [1]}}') == DataClassWithDataClass( DataClassWithList([1])))
def test_warns_when_required_field_is_none(self): with pytest.warns(RuntimeWarning, match='None value'): assert (DataClassWithDataClass.from_json('{"xs": null}') == DataClassWithDataClass(None))