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)
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`')
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)
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)
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)
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)
def setUp(self): self.t = 'username' self.v = 'wilhelm' self.f = field.Field(self.t, self.v, False).append( [rules.IsRequired(), rules.IsNumeric()])