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_validation_multiline():
validator = validators.Regex('^s.*e$')
validator.validate('some')
with pytest.raises(errors.ValidationError):
validator.validate('some\nso more')
validator = validators.Regex('^s.*e$', multiline=True)
validator.validate('some')
validator.validate('some\nso more')
class Parking(models.Base):
location = fields.StringField()
cars = fields.ListField([Viper, Lamborghini])
data = {
'location': 'somewhere',
'cars': object(),
}
with pytest.raises(errors.ValidationError):
Parking(**data)
parking = Parking()
with pytest.raises(errors.ValidationError):
parking.populate(**data)
def test_get_field_not_found():
class Person(models.Base):
children = fields.ListField()
alan = Person()
with pytest.raises(errors.FieldNotFound):
alan.get_field('bazinga')
def test_base_field_should_not_be_usable():
class Person(models.Base):
name = fields.BaseField()
alan = Person()
with pytest.raises(errors.ValidationError):
alan.name = 'some name'
with pytest.raises(errors.ValidationError):
alan.name = 2345
def test_to_struct_basic():
class Person(models.Base):
name = fields.StringField(required=True)
surname = fields.StringField(required=True)
age = fields.IntField()
cash = fields.FloatField()
alan = Person()
with pytest.raises(errors.ValidationError):
alan.to_struct()
alan.name = 'Alan'
alan.surname = 'Wake'
assert {'name': 'Alan', 'surname': 'Wake'} == alan.to_struct()
alan.age = 24
alan.cash = 2445.45
pattern = {
'name': 'Alan',
'surname': 'Wake',
'age': 24,
'cash': 2445.45,
}
except KeyError:
print("Model {} is not found in models list".format(table))
return
try:
obj = model.from_json(json_str)
except ValueError:
print("Json {} is not valid".format(json_str))
return
except TypeError:
print("Json {} is not applicable to {}".format(json_str, table))
return
try:
nb_api.create(obj)
except errors.ValidationError:
print("Json {} is not applicable to {}".format(json_str, table))
:param table: table name where object should be added
:param action_handler: which method to run on the received json
:return: None
"""
try:
obj = model_object_from_json(json_str, table)
except ValueError:
print("Record {} is not valid".format(json_str))
return
except TypeError:
print("Json {} does not match schema of {}".format(json_str, table))
return
try:
action_handler(obj)
except errors.ValidationError:
print("Json {} failed validation for {}".format(json_str, table))
def validate(self):
"""
Validate the rule. That is, verify dscp_mark is set if type is
dscp_marking, and that max_burst_kbps is set if type is bandwidth_limit
"""
super(QosPolicyRule, self).validate()
if self.type == RULE_TYPE_DSCP_MARKING:
if self.dscp_mark is None:
raise errors.ValidationError("dscp_mark is required if "
"type is dscp_marking")
elif self.type == RULE_TYPE_BANDWIDTH_LIMIT:
if self.max_burst_kbps is None:
raise errors.ValidationError("max_burst_kbps is required if "
"type is bandwidth_limit")
def validate(self, value):
if self.required and not value:
raise errors.ValidationError(_('Field is required!'))
if value is None:
return
for elem in value:
if elem not in self._valid_values:
raise errors.ValidationError(
_('{value} is not one of: [{valid_values}]').format(
value=value,
valid_values=', '.join(self._valid_values)))
def build(self):
if issubclass(self.type, six.string_types):
obj = {'type': 'string'}
elif issubclass(self.type, bool):
obj = {'type': 'boolean'}
elif issubclass(self.type, int):
obj = {'type': 'number'}
elif issubclass(self.type, float):
obj = {'type': 'number'}
else:
raise errors.FieldNotSupported(
"Can't specify value schema!", self.type
)
return obj if not self.nullable else {'type': [obj['type'], 'null']}