예제 #1
0
    def test_complex_error_all(self):

        schema = schemaish.Structure([
            ("one",
             schemaish.Integer(validator=v.All(
                 v.Required(),
                 v.Integer(),
                 v.Range(min=18),
                 v.Range(min=20),
             ))),
        ])
        f = formish.Form(schema, name="form", add_default_action=False)

        f.add_action('submit', "Submit Me")
        r = webob.Request.blank('http://localhost/',
                                environ={'REQUEST_METHOD': 'POST'})
        r.POST['__formish_form__'] = 'form'
        r.POST['one'] = '9'
        try:
            f.validate(r)
        except fv.FormError:
            assert str(
                f.errors['one']
            ) == 'must be greater than or equal to 18; must be greater than or equal to 20'
            assert str(f['one'].field.errors.exceptions[0]
                       ) == 'must be greater than or equal to 18'
예제 #2
0
class InvoiceEntrySchema(schemaish.Structure):
    id = schemaish.Integer()
    description = schemaish.String(validator=validator.Required())
    currency_code = schemaish.String(validator=validator.Required())
    vat = schemaish.Integer(validator=validator.Required())
    unit_price = schemaish.Decimal(validator=validator.Required())
    units = schemaish.Decimal(
        validator=validator.All(validator.Required(), validator.Range(
            min=0.1)))
예제 #3
0
class TestAny_RangeInteger(unittest.TestCase):

    type = 'AnyRangeInteger'
    fn = validator.Any(validator.Range(min=8), validator.Integer())

    def test_validate_pass(self):
        self.section = 'pass'
        values = [
            4,
            12.3,
            15,
            None,
        ]
        check_pass('class', self, self.fn, values)

    def test_validate_fail(self):
        self.section = 'fail'
        values = [
            3.4,
        ]
        check_fail('class', self, self.fn, values)
예제 #4
0
class TestRange(unittest.TestCase):

    type = 'Range'

    fn_min = staticmethod(lambda v: validate.is_in_range(v, min=3))
    class_fn_min = validator.Range(min=3)

    fn_max = staticmethod(lambda v: validate.is_in_range(v, max=3))
    class_fn_max = validator.Range(max=3)

    fn_between = staticmethod(lambda v: validate.is_in_range(v, min=1, max=3))
    class_fn_between = validator.Range(min=1, max=3)

    def test_validate_min_pass(self):
        self.section = 'pass'
        values = [
            4,
            5.5,
        ]
        check_pass('function', self, self.fn_min, values)
        check_pass('class', self, self.class_fn_min, values)

    def test_validate_min_fail(self):
        self.section = 'fail'
        values = [
            0,
            -1,
            -0.23,
        ]
        check_fail('function', self, self.fn_min, values)
        check_fail('class', self, self.class_fn_min, values)

    def test_validate_max_pass(self):
        self.section = 'pass'
        values = [
            0,
            -1,
            -0.23,
            2.99,
        ]
        check_pass('function', self, self.fn_max, values)
        check_pass('class', self, self.class_fn_max, values)

    def test_validate_max_fail(self):
        self.section = 'fail'
        values = [
            4,
            5,
            6.9845,
        ]
        check_fail('function', self, self.fn_max, values)
        check_fail('class', self, self.class_fn_max, values)

    def test_validate_between_pass(self):
        self.section = 'pass'
        values = [
            1.4,
            1,
            2.23,
            2.99,
            3,
        ]
        check_pass('function', self, self.fn_between, values)
        check_pass('class', self, self.class_fn_between, values)

    def test_validate_between_fail(self):
        self.section = 'fail'
        values = [
            4,
            5,
            6.9845,
            0,
            -2,
            -9,
        ]
        check_fail('function', self, self.fn_between, values)
        check_fail('class', self, self.class_fn_between, values)

    def test_messages(self):
        try:
            self.fn_min(-999999)
        except error.Invalid, e:
            assert 'greater' in e.message
        try:
            self.fn_max(999999)
        except error.Invalid, e:
            assert 'less' in e.message
예제 #5
0
class InvoiceSchema(schemaish.Structure):
    payment_term = schemaish.Integer(
        validator=validator.All(validator.Required(), validator.Range(min=1)))
    note = schemaish.String()
    entries = schemaish.Sequence(attr=InvoiceEntrySchema())