Skip to content

Commit

Permalink
Add a tests for model validators() method and add this method to Mode…
Browse files Browse the repository at this point in the history
…l class.
  • Loading branch information
GrAndSE committed May 10, 2012
1 parent f48e5b1 commit 6f9c844
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lighty/db/models.py
Expand Up @@ -233,3 +233,5 @@ def fields(cls):
def validators(cls):
'''Get a dictinary view contains validators for fields
'''
return dict([(field_name, getattr(cls, field_name).validators)
for field_name in cls._fields])
28 changes: 27 additions & 1 deletion tests/validators.py
Expand Up @@ -2,8 +2,9 @@
"""
import unittest

from lighty.db import fields, models
from lighty.monads import ErrorMonad
from lighty import validators
from lighty import utils, validators


class TrueFalseValidator(validators.Validator):
Expand All @@ -16,6 +17,11 @@ def message(self, value):
return 'Value: %s' % value


class TestModel(models.Model):
test_type = fields.CharField(choices=[('t', 'test'), ('c', 'test case')])
positive = fields.PositiveIntegerField()


class ValidatorClassTestCase(unittest.TestCase):
"""Test case for Validator class and validate function
"""
Expand Down Expand Up @@ -60,6 +66,25 @@ def testChoicesValidator(self):
assert not result, 'Validation error missed: %s' % result


class ModelValidatorsTestCase(unittest.TestCase):
'''Test case to check model validators
'''

def testModelValidator(self):
'''Test validators() method in model class
'''
validators = TestModel.validators()
assert len(validators) == 2, ('Wrong validator groups number: %s' %
len(validators))
names = sorted(utils.dict_keys(validators.keys()))
assert names == sorted(TestModel._fields), ('Wrong validator groups '
'names: %s' % names)
assert len(validators['test_type']) == 2, ('Wrong validators number '
'for CharField with choices: %s' % validators['test_type'])
assert len(validators['positive']) == 1, ('Wrong validators number '
'for PositiveIntegerField: %s' % validators['positive'])


class ValidateTestCase(unittest.TestCase):
'''Test case used to check validate function
'''
Expand All @@ -81,5 +106,6 @@ def test():
suite.addTest(ValidatorClassTestCase('testSimpleValidator'))
suite.addTest(ValidatorClassTestCase('testCustomErrorMessage'))
suite.addTest(ValidatorsTestCase('testChoicesValidator'))
suite.addTest(ModelValidatorsTestCase('testModelValidator'))
suite.addTest(ValidateTestCase('testValidateFunction'))
return suite

0 comments on commit 6f9c844

Please sign in to comment.