def test_config_strict_mode_errors(): config.register(Item, strict_mode=True) test_config_path = testing.relative_module_path(__file__, 'test_config.yaml') with pytest.raises(ConfigLoadError, match='missing type declaration'): config.load_config(test_config_path)
def test_config_unsafe_object_creation(): from ruamel import yaml import calendar test_config_path = testing.relative_module_path(__file__, 'test_config_unsafe.yaml') # Unsafe default loader allows any python object initialization data = yaml.load(open(test_config_path), Loader=yaml.Loader) assert 'time' in data assert isinstance(data['time'], calendar.Calendar) # With use safe version only to allow construct previously registered objects with pytest.raises(config.ConfigLoadError, match='could not determine a constructor'): config.load_config(test_config_path)
def test_dataclass(): test_config_path = testing.relative_module_path(__file__, 'test_dataclasses.yaml') data = config.load_config(test_config_path) dc1 = data['dc1'] assert dc1.x == 5 assert dc1.y is None assert dc1.z == [1, 2, 3] assert dc1.d == 'asd' assert dc1.e == '/abc/def' assert dc1.foo == 'foobaz' dc2 = data['dc2'] assert dc2.x == 1 assert dc2.y == 'newble' assert dc2.z == [3, 4, 5] assert dc2.d == 4 assert dc2.foo == FooEnum.BAR
def test_config_with_simple_classes(): # Another method for registering items (other than using decorator). config.register(Item, strict_mode=False) test_config_path = testing.relative_module_path(__file__, 'test_config.yaml') data = config.load_config(test_config_path) foo_with_defaults = data['foo_with_defaults'] assert foo_with_defaults.f == 1 empty_boo = data['empty_boo'] assert empty_boo.foo is None foo = data['foo'] boo = data['boo'] assert foo.s == 'some_string' assert foo.f == 2.5 assert boo.foo is foo assert len(boo.items) == 2 assert isinstance(boo.items[0], Item)
def test_dataclass_exceed_maximum(): test_config_path = testing.relative_module_path( __file__, 'test_dataclasses_exceed_maximum.yaml') with pytest.raises(config.ConfigLoadError, match="field 'x'.*Maximum value is 5. Got 123."): config.load_config(test_config_path)
def test_dataclass_invalid_field(): test_config_path = testing.relative_module_path( __file__, 'test_dataclasses_invalid_field.yaml') with pytest.raises(config.ConfigLoadError, match="constructor signature"): config.load_config(test_config_path)
def test_invalid_dataclass_union(): test_config_path = testing.relative_module_path( __file__, 'test_dataclasses_invalid_union.yaml') with pytest.raises(config.ConfigLoadError, match="field 'd'.*improper type from union"): config.load_config(test_config_path)
def test_invalid_dataclass(): test_config_path = testing.relative_module_path( __file__, 'test_dataclasses_invalid.yaml') with pytest.raises(config.ConfigLoadError, match="field 'x'.*Invalid type"): config.load_config(test_config_path)
def test_dataclass_validation_invalid_dict(): test_config_path = testing.relative_module_path(__file__, 'test_dataclasses_validation_invalid_dict.yaml') with pytest.raises(ConfigLoadError, match='wrong_value'): config.load_config(test_config_path)
def test_dataclass_validation(): test_config_path = testing.relative_module_path(__file__, 'test_dataclasses_validation.yaml') data = config.load_config(test_config_path) assert data['dc1'].rr == [[2, 3], []]