Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a tests for model validators() method and add this method to Mode…

…l class.
  • Loading branch information...
commit 6f9c84466f51026f686e70df864c5a11341a0637 1 parent f48e5b1
@GrAndSE authored
Showing with 29 additions and 1 deletion.
  1. +2 −0  lighty/db/models.py
  2. +27 −1 tests/validators.py
View
2  lighty/db/models.py
@@ -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])
View
28 tests/validators.py
@@ -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):
@@ -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
"""
@@ -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
'''
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.