Exemple #1
0
    def test_is_length_between(self):
        s1 = 'abc'
        mn1 = 1
        mn2 = 6
        mx1 = 8
        mx2 = 3

        r = rules.IsLengthBetween(mn1, mx1).run(s1)
        self.assertTrue(r)

        r = rules.IsLengthBetween(mn2, mx2).run(s1)
        self.assertFalse(r) 
 def setUp(self):
     self.c = collection.Collection().append([
         field.Field('username', 'wilhelm').append([
             rules.IsRequired(),
             rules.IsAlphaNumeric(),
             rules.IsLengthBetween(3, 10)
         ]),
         field.Field('email', '*****@*****.**').append(
             [rules.IsRequired(), rules.IsEmail()]),
         field.Field('password', 'root').append(
             [rules.IsRequired(),
              rules.IsLengthBetween(2, 10)]),
         field.Field('password-confirm',
                     'root').append([rules.Matches('root')]),
     ])
    def test_validator_fail(self):
        self.c.append(
            field.Field('foo', 'bar').append(
                [rules.IsLengthBetween(1, 1),
                 rules.IsEmail()]))

        r = self.c.run()

        self.assertFalse(r)
Exemple #4
0
    def test_single_rule_pass(self):
        f = 'username'
        v = 'wilhelm'
        mn = 3
        mx = 10

        r = field.Field(f, v).append(rules.IsLengthBetween(mn, mx)).run()

        self.assertTrue(r[0])
        self.assertEquals(r[1], [])
        self.assertEquals(len(r), 2)
    def test_validator_errors(self):
        self.c.append(
            field.Field('foo', 'bar').append(rules.IsLengthBetween(1, 1)))

        r = self.c.run(True)
        e = self.c.errors()

        self.assertEquals(type(e), dict)
        self.assertIsNotNone(e['foo'])
        self.assertTrue(len(e['foo']), 1)
        self.assertEquals(e['foo'][0],
                          'String `bar` length is not within `1` and `1`')
Exemple #6
0
    def test_single_rule_fail(self):
        f = 'username'
        v = 'wilhelm'
        mn = 3
        mx = 4

        r = field.Field(f, v).append(rules.IsLengthBetween(mn, mx)).run()

        self.assertFalse(r[0])
        self.assertEquals(len(r[1]), 1)
        self.assertEquals(
            r[1][0],
            'String `{}` length is not within `{}` and `{}`'.format(v, mn, mx))
        self.assertEquals(len(r), 2)
Exemple #7
0
    def test_composite_rule_pass(self):
        f = 'username'
        v = 'wilhelm'
        mn = 3
        mx = 10

        r = field.Field(f, v).append([
            rules.IsRequired(),
            rules.IsAlphaNumeric(),
            rules.IsLengthBetween(mn, mx)
        ]).run()

        self.assertTrue(r[0])
        self.assertEquals(r[1], [])
        self.assertEquals(len(r), 2)
    def test_validator_fail_collated_results(self):
        self.c.append(
            field.Field('foo', 'bar').append(
                [rules.IsLengthBetween(1, 1),
                 rules.IsEmail()]))

        r = self.c.run(True)

        self.assertTrue(type(r), dict)
        self.assertEquals(len(r), 5)

        for f in r:
            if f['field'] == 'foo':
                self.assertFalse(f['passed'])
                self.assertEquals(len(f['errors']), 1)
Exemple #9
0
    def test_composite_rule_fail(self):
        f = 'username'
        v = 'wilhelm'
        mn = 3
        mx = 4

        r = field.Field(f, v).append([
            rules.IsRequired(),
            rules.IsNumeric(),
            rules.IsLengthBetween(mn, mx)
        ]).run()

        self.assertFalse(r[0])
        self.assertEquals(len(r[1]), 1)
        self.assertEquals(r[1][0], 'This is not a number.')
        self.assertEquals(len(r), 2)
Exemple #10
0
    def test_composite_rule_multiple_fail_messages(self):
        f = 'username'
        v = 'wilhelm'
        mn = 3
        mx = 4

        r = field.Field(f, v, False).append([
            rules.IsRequired(),
            rules.IsNumeric(),
            rules.IsLengthBetween(mn, mx)
        ]).run()

        self.assertFalse(r[0])
        self.assertEquals(len(r[1]), 2)
        self.assertEquals(r[1][0], 'This is not a number.')
        self.assertEquals(
            r[1][1],
            'String `{}` length is not within `{}` and `{}`'.format(v, mn, mx))
        self.assertEquals(len(r), 2)