Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_from_dict_with_transform_of_none_optional_field():
@dataclass
class X:
s: Optional[str]
result = from_dict(X, {'s': None}, Config(transform={'s': str.lower}))
assert result == X(s=None)
def test_from_dict_with_prefix():
@dataclass
class X:
i: int
@dataclass
class Y:
s: str
x: X
result = from_dict(Y, {'s': 'test', 'x_i': 1}, Config(prefixed={'x': 'x_'}))
assert result == Y(s='test', x=X(i=1))
def test_get_value_for_flattened_field():
@dataclass
class X:
i: int
@dataclass
class Y:
x: X
config = Config(flattened=["x"])
value = config.get_value(field=fields(Y)[0], data={"i": 1})
assert value == {"i": 1}
def test_validate_empty_config():
@dataclass
class X:
i: int
config = Config()
try:
config.validate(data_class=X, data={"i": 1})
except InvalidConfigurationError:
pytest.fail("empty config should be valid")
def test_from_dict_with_remap():
@dataclass
class X:
s: str
i: int
result = from_dict(X, {'s': 'test', 'j': 1}, Config(remap={'i': 'j'}))
assert result == X(s='test', i=1)
def test_get_value_for_field_with_empty_config():
@dataclass
class X:
i: int
config = Config()
value = config.get_value(field=fields(X)[0], data={"i": 1})
assert value == 1
def test_get_value_for_field_with_transform():
@dataclass
class X:
i: int
config = Config(transform={"i": lambda v: v + 1})
value = config.get_value(field=fields(X)[0], data={"i": 1})
assert value == 2
def test_get_value_for_remapped_field():
@dataclass
class X:
i: int
config = Config(remap={"i": "j"})
value = config.get_value(field=fields(X)[0], data={"j": 1})
assert value == 1
def test_form_dict_with_disabled_type_checking():
@dataclass
class X:
i: int
result = from_dict(X, {"i": "test"}, config=Config(check_types=False))
# noinspection PyTypeChecker
assert result == X(i="test")
def _make_inner_config(field: Field, config: Config) -> Config:
return Config(
remap=_extract_nested_dict(field, config.remap),
prefixed=_extract_nested_dict(field, config.prefixed),
cast=_extract_nested_list(field, config.cast),
transform=_extract_nested_dict(field, config.transform),
flattened=_extract_nested_list(field, config.flattened),
)