def test_less_or_equal_validate_fail(self): schema = {'less_or_equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 5) self.assertEqual(_('The value "5" of property "prop" must be less ' 'than or equal to "4".'), str(error))
def test_min_length_validate_fail(self): schema = {'min_length': 4} constraint = Constraint('prop', Schema.STRING, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 'abc') self.assertEqual(_('Length of value "abc" of property "prop" must ' 'be at least "4".'), str(error))
def test_in_range_validate_fail(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 8) self.assertEqual(_('The value "8" of property "prop" is out of range ' '"(min:2, max:6)".'), str(error))
def test_equal_validate_fail(self): schema = {'equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 8) self.assertEqual('The value "8" of property "prop" is not equal to ' '"4".', str(error))
def test_pattern_validate_fail(self): schema = {'pattern': '[0-9]*'} constraint = Constraint('prop', Schema.STRING, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 'abc') self.assertEqual(_('The value "abc" of property "prop" does not ' 'match pattern "[0-9]*".'), str(error))
def test_max_length_with_map(self): schema = {'max_length': 1} constraint = Constraint('prop', Schema.MAP, schema) try: constraint.validate({"k": "v"}) except Exception as ex: self.fail(ex)
def test_max_length_with_list(self): schema = {'max_length': 2} constraint = Constraint('prop', Schema.LIST, schema) try: constraint.validate(["1", "2"]) except Exception as ex: self.fail(ex)
def test_validvalues_validate_fail(self): schema = {'valid_values': [2, 4, 6, 8]} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 5) self.assertEqual(_('The value "5" of property "prop" is not valid. ' 'Expected a value from "[2, 4, 6, 8]".'), str(error))
def test_max_length_validate_fail(self): schema = {'max_length': 4} constraint = Constraint('prop', Schema.STRING, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 'abcde') self.assertEqual( _('Length of value "abcde" of property "prop" ' 'must be no greater than "4".'), str(error))
def test_greater_than_validate_fail(self): schema = {'greater_than': 4} constraint = Constraint('prop', Schema.INTEGER, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 3) self.assertEqual(_('The value "3" of property "prop" must be greater ' 'than "4".'), str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, 4) self.assertEqual(_('The value "4" of property "prop" must be greater ' 'than "4".'), str(error))
def test_greater_or_equal_validate_fail(self): schema = {'greater_or_equal': 3.9} constraint = Constraint('prop', Schema.FLOAT, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, 3.0) self.assertEqual(_('The value "3.0" of property "prop" must be ' 'greater than or equal to "3.9".'), str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, 3.8) self.assertEqual(_('The value "3.8" of property "prop" must be ' 'greater than or equal to "3.9".'), str(error))
def __init__(self, name, definition, datatype=None): self.name = name if name in ['and', 'or', 'not', 'assert']: self.conditions = list(self.getConditions(definition)) else: # 3.6.25.3 Direct assertion definition if isinstance(definition, dict): definition = [definition] # hack: pick a prop_type to avoid validation error # XXX need to get the real property_type from the target's attribute definition self.conditions = [ Constraint( name, datatype or get_constraint_class( next(iter(constraint))).valid_prop_types[0], constraint) for constraint in definition ]
def test_less_than_validate_fail(self): schema = {'less_than': datetime.date(2014, 0o7, 25)} constraint = Constraint('prop', Schema.TIMESTAMP, schema) error = self.assertRaises(exception.ValidationError, constraint.validate, datetime.date(2014, 0o7, 25)) self.assertEqual(_('The value "2014-07-25" of property "prop" must be ' 'less than "2014-07-25".'), str(error)) error = self.assertRaises(exception.ValidationError, constraint.validate, datetime.date(2014, 0o7, 27)) self.assertEqual(_('The value "2014-07-27" of property "prop" must be ' 'less than "2014-07-25".'), str(error))
def test_less_than_validate(self): schema = {'less_than': datetime.date(2014, 0o7, 25)} constraint = Constraint('prop', Schema.TIMESTAMP, schema) self.assertIsNone(constraint.validate(datetime.date(2014, 0o7, 20))) self.assertIsNone(constraint.validate(datetime.date(2014, 0o7, 24)))
def test_validvalues_validate(self): schema = {'valid_values': [2, 4, 6, 8]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(4))
def test_in_range_min_max(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertEqual(2, constraint.min) self.assertEqual(6, constraint.max)
def test_in_range_validate(self): schema = {'in_range': [2, 6]} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(2)) self.assertIsNone(constraint.validate(4)) self.assertIsNone(constraint.validate(6))
def test_pattern_validate(self): schema = {'pattern': '[0-9]*'} constraint = Constraint('prop', Schema.STRING, schema) self.assertIsNone(constraint.validate('123'))
def test_max_length_validate(self): schema = {'max_length': 4} constraint = Constraint('prop', Schema.STRING, schema) self.assertIsNone(constraint.validate('abcd')) self.assertIsNone(constraint.validate('abc'))
def test_greater_than_validate(self): schema = {'greater_than': 4} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(6))
def test_greater_or_equal_validate(self): schema = {'greater_or_equal': 3.9} constraint = Constraint('prop', Schema.FLOAT, schema) self.assertIsNone(constraint.validate(3.9)) self.assertIsNone(constraint.validate(4.0))
def test_less_or_equal_validate(self): schema = {'less_or_equal': 4} constraint = Constraint('prop', Schema.INTEGER, schema) self.assertIsNone(constraint.validate(4)) self.assertIsNone(constraint.validate(3))