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_required_on_init():
class Foo(Model):
bar = BooleanType(required=True)
baz = BooleanType()
foo = Foo({'baz': 0}, partial=True, validate=True)
foo.bar = 1
assert foo.serialize() == {'bar': True, 'baz': False}
def count(n):
while True:
yield n
n += 1
class User(Model):
id = IntType(default=next(count(42)))
name = StringType()
email = StringType()
password = StringType()
class Options:
roles = {
'create': all_fields - ['id'],
'public': all_fields - ['password'],
'nospam': blacklist('password') + blacklist('email'),
'empty': whitelist(),
'everything': blacklist(),
}
roles = User.Options.roles
assert len(roles['create']) == 3
assert len(roles['public']) == 3
assert len(roles['nospam']) == 2
assert len(roles['empty']) == 0
assert len(roles['everything']) == 0
# Sets sort different with different Python versions. We should be getting something back
# like: "whitelist('password', 'email', 'name')"
s = str(roles['create'])
assert s.startswith('whitelist(') and s.endswith(')')
assert sorted(s[10:-1].split(', ')) == ["'email'", "'name'", "'password'"]
yield n
n += 1
class User(Model):
id = IntType(default=next(count(42)))
name = StringType()
email = StringType()
password = StringType()
class Options:
roles = {
'create': all_fields - ['id'],
'public': all_fields - ['password'],
'nospam': blacklist('password') + blacklist('email'),
'empty': whitelist(),
'everything': blacklist(),
}
roles = User.Options.roles
assert len(roles['create']) == 3
assert len(roles['public']) == 3
assert len(roles['nospam']) == 2
assert len(roles['empty']) == 0
assert len(roles['everything']) == 0
# Sets sort different with different Python versions. We should be getting something back
# like: "whitelist('password', 'email', 'name')"
s = str(roles['create'])
assert s.startswith('whitelist(') and s.endswith(')')
assert sorted(s[10:-1].split(', ')) == ["'email'", "'name'", "'password'"]
# Similar, but now looking for:
def test_resource_collection():
class TestCollection(ResourceCollection):
foo = types.StringType(required=True)
bar = types.BooleanType(default=True)
def create_resources(self):
self.res1 = Resource("res1", "foo", foo=self.foo)
tc = TestCollection(foo="foo!")
assert tc.foo == "foo!"
assert tc.res1.foo == "foo!"
assert tc.res1.id == "${res1.foo.id}"
with pytest.raises(SCHEMATICS_EXCEPTIONS):
TestCollection()
def test_returns_partial_data_with_conversion_errors():
class User(Model):
name = StringType(required=True)
age = IntType(required=True)
account_level = IntType()
with pytest.raises(DataError) as exception:
User({"name": "Jóhann", "age": "100 years", "account_level": "3"})
partial_data = exception.value.partial_data
assert partial_data == {
"name": u"Jóhann",
"account_level": 3,
}
def test_mock_object():
assert ListType(IntType, required=True).mock() is not None
with pytest.raises(MockCreationError) as exception:
ListType(IntType, min_size=10, max_size=1, required=True).mock()
def test_validate_convert():
class M(Model):
field1 = IntType()
m = M()
m.field1 = "1"
m.validate()
assert m.field1 == 1
m = M()
m.field1 = "foo"
m.validate(convert=False)
assert m.field1 == "foo"
def test_validate_strict_with_trusted_data():
class Player(Model):
id = IntType()
try:
validate(Player, {'id': 4}, strict=True, trusted_data={'name': 'Arthur'})
except ValidationError as e:
assert 'name' in e.messages
def test_model_field_validate_only_when_field_is_set():
class M0(Model):
bar = StringType()
def validate_bar(self, data, value):
if data['bar'] and 'bar' not in data['bar']:
raise ValidationError('Illegal value')
class M1(Model):
foo = StringType(required=True)
def validate_foo(self, data, value):
if 'foo' not in data['foo']:
raise ValidationError('Illegal value')
m = M0({})
m.validate()
def test_nested_model_override_mapping_methods():
"""
overriding mapping methods on child models should not cause issues
with validation on the parent.
"""
class Nested(Model):
items, values, get, keys = IntType(), IntType(), IntType(), IntType()
class Root(Model):
keys = ModelType(Nested)
root = Root({"keys": {"items": 1, "values": 1, "get": 1, "keys": 1}})
root.validate()
for key in ["items", "values", "get", "keys"]:
assert getattr(root.keys, key) == 1