Пример #1
0
    def test_modulo_validate(self):
        r = constraints.Modulo(step=2, offset=1, description='a modulo')
        r.validate(1)
        r.validate(3)
        r.validate(5)
        r.validate(777777)

        r = constraints.Modulo(step=111, offset=0, description='a modulo')
        r.validate(111)
        r.validate(222)
        r.validate(444)
        r.validate(1110)

        r = constraints.Modulo(step=111, offset=11, description='a modulo')
        r.validate(122)
        r.validate(233)
        r.validate(1121)

        r = constraints.Modulo(step=-2, offset=-1, description='a modulo')
        r.validate(-1)
        r.validate(-3)
        r.validate(-5)
        r.validate(-777777)

        r = constraints.Modulo(step=-2, offset=0, description='a modulo')
        r.validate(-2)
        r.validate(-4)
        r.validate(-8888888)
Пример #2
0
    def test_modulo_validate_fail(self):
        r = constraints.Modulo(step=2, offset=1)
        err = self.assertRaises(ValueError, r.validate, 4)
        self.assertIn('4 is not a multiple of 2 with an offset of 1',
                      six.text_type(err))

        self.assertRaises(ValueError, r.validate, 0)
        self.assertRaises(ValueError, r.validate, 2)
        self.assertRaises(ValueError, r.validate, 888888)

        r = constraints.Modulo(step=2, offset=0)
        self.assertRaises(ValueError, r.validate, 1)
        self.assertRaises(ValueError, r.validate, 3)
        self.assertRaises(ValueError, r.validate, 5)
        self.assertRaises(ValueError, r.validate, 777777)

        err = self.assertRaises(exception.InvalidSchemaError,
                                constraints.Modulo,
                                step=111,
                                offset=111)
        self.assertIn('offset must be smaller (by absolute value) than step',
                      six.text_type(err))

        err = self.assertRaises(exception.InvalidSchemaError,
                                constraints.Modulo,
                                step=111,
                                offset=112)
        self.assertIn('offset must be smaller (by absolute value) than step',
                      six.text_type(err))

        err = self.assertRaises(exception.InvalidSchemaError,
                                constraints.Modulo,
                                step=0,
                                offset=1)
        self.assertIn('step cannot be 0', six.text_type(err))

        err = self.assertRaises(exception.InvalidSchemaError,
                                constraints.Modulo,
                                step=-2,
                                offset=1)
        self.assertIn('step and offset must be both positive or both negative',
                      six.text_type(err))

        err = self.assertRaises(exception.InvalidSchemaError,
                                constraints.Modulo,
                                step=2,
                                offset=-1)
        self.assertIn('step and offset must be both positive or both negative',
                      six.text_type(err))
Пример #3
0
    def _constraint_from_def(cls, constraint):
        desc = constraint.get(DESCRIPTION)

        if MODULO in constraint:
            cdef = constraint.get(MODULO)
            cls._check_dict(cdef, MODULO_KEYS, 'modulo constraint')
            return constr.Modulo(parameters.Schema.get_num(STEP, cdef),
                                 parameters.Schema.get_num(OFFSET, cdef), desc)
        else:
            return super(HOTParamSchema20170224,
                         cls)._constraint_from_def(constraint)
Пример #4
0
 def test_modulo_schema(self):
     d = {'modulo': {'step': 2, 'offset': 1}, 'description': 'a modulo'}
     r = constraints.Modulo(2, 1, description='a modulo')
     self.assertEqual(d, dict(r))
Пример #5
0
 def test_modulo_invalid_type(self):
     schema = constraints.Schema('String',
                                 constraints=[constraints.Modulo(2, 1)])
     err = self.assertRaises(exception.InvalidSchemaError, schema.validate)
     self.assertIn('Modulo constraint invalid for String',
                   six.text_type(err))