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_validate_config_with_correct_remap():
@dataclass
class X:
i: int
config = Config(remap={"i": "j"})
try:
config.validate(data_class=X, data={"j": 1})
except InvalidConfigurationError:
pytest.fail("this config should be valid")
def test_from_dict_with_wrong_filed_name_in_config_remap():
@dataclass
class X:
i: int
with pytest.raises(InvalidConfigurationError) as exception_info:
from_dict(X, {'i': 1}, Config(remap={'s': 'z'}))
assert exception_info.value.parameter == 'remap'
assert exception_info.value.value == 's'
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_wrong_filed_name_in_config_cast():
@dataclass
class X:
i: int
s: str
with pytest.raises(InvalidConfigurationError) as exception_info:
from_dict(X, {'s': 'test', 'i': '1'}, Config(cast=['j']))
assert exception_info.value.parameter == 'cast'
assert exception_info.value.value == 'j'
def _validate_config_field_name(data_class: Type[T], config: Config, parameter: str) -> None:
data_class_fields = {field.name for field in fields(data_class)}
for data_class_field in getattr(config, parameter):
if '.' not in data_class_field:
if data_class_field not in data_class_fields:
raise InvalidConfigurationError(
parameter=parameter,
available_choices=data_class_fields,
value=data_class_field,
)
def _validate_config_data_key(data_class: Type[T], data: Data, config: Config, parameter: str,
validator=lambda v, c: v in c) -> None:
input_data_keys = set(data.keys())
data_class_fields = {field.name: field for field in fields(data_class)}
for field_name, input_data_field in getattr(config, parameter).items():
if '.' not in field_name:
field = data_class_fields[field_name]
if not validator(input_data_field, input_data_keys) and not _has_field_default_value(field):
raise InvalidConfigurationError(
parameter=parameter,
available_choices=input_data_keys,
value=input_data_field,
)