Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
 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))
Beispiel #4
0
 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))
Beispiel #5
0
 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))
Beispiel #6
0
 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))
Beispiel #7
0
 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))
Beispiel #8
0
 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))
Beispiel #9
0
 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))
Beispiel #10
0
    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))
Beispiel #11
0
    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))
Beispiel #12
0
 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
         ]
Beispiel #13
0
    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))
Beispiel #14
0
 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)))
Beispiel #15
0
 def test_validvalues_validate(self):
     schema = {'valid_values': [2, 4, 6, 8]}
     constraint = Constraint('prop', Schema.INTEGER, schema)
     self.assertIsNone(constraint.validate(4))
Beispiel #16
0
 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)
Beispiel #17
0
 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))
Beispiel #18
0
 def test_pattern_validate(self):
     schema = {'pattern': '[0-9]*'}
     constraint = Constraint('prop', Schema.STRING, schema)
     self.assertIsNone(constraint.validate('123'))
Beispiel #19
0
 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'))
Beispiel #20
0
 def test_greater_than_validate(self):
     schema = {'greater_than': 4}
     constraint = Constraint('prop', Schema.INTEGER, schema)
     self.assertIsNone(constraint.validate(6))
Beispiel #21
0
 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))
Beispiel #22
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))