Refactored ModelTestCaseMixin
This commit is contained in:
parent
af14cce3f8
commit
27520c7ca4
@ -5,17 +5,13 @@ from django.test import TestCase
|
|||||||
|
|
||||||
# Creating a test user
|
# Creating a test user
|
||||||
|
|
||||||
class ModelTestCaseMixin:
|
class ModelTestCase(TestCase):
|
||||||
'''
|
'''
|
||||||
Base class for ModelTests
|
Base class for ModelTests.
|
||||||
|
Parameters:
|
||||||
|
- model : Class to test
|
||||||
'''
|
'''
|
||||||
model = None
|
model = None
|
||||||
model_name = None
|
|
||||||
|
|
||||||
def setUp(self):
|
|
||||||
if not self.model._meta.abstract:
|
|
||||||
self.object = self.model.objects.get(id=1)
|
|
||||||
self.model_name = self.model.__name__
|
|
||||||
|
|
||||||
def assertField(self, field_name, field_class, must_have={}, must_not_have={}):
|
def assertField(self, field_name, field_class, must_have={}, must_not_have={}):
|
||||||
'''
|
'''
|
||||||
@ -31,14 +27,14 @@ class ModelTestCaseMixin:
|
|||||||
except FieldDoesNotExist:
|
except FieldDoesNotExist:
|
||||||
self.fail(
|
self.fail(
|
||||||
'Expecting %s to have a field named \'%s\'' % (
|
'Expecting %s to have a field named \'%s\'' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name
|
field_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
type(field), field_class,
|
type(field), field_class,
|
||||||
msg='Expecting type of %s.%s to be %s' % (
|
msg='Expecting type of %s.%s to be %s' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
field_class.__name__
|
field_class.__name__
|
||||||
)
|
)
|
||||||
@ -48,7 +44,7 @@ class ModelTestCaseMixin:
|
|||||||
if value:
|
if value:
|
||||||
self.assertEqual(getattr(field, key), value,
|
self.assertEqual(getattr(field, key), value,
|
||||||
msg='Expeting %s.%s.%s to be \'%s\'' % (
|
msg='Expeting %s.%s.%s to be \'%s\'' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
key,
|
key,
|
||||||
value
|
value
|
||||||
@ -57,7 +53,7 @@ class ModelTestCaseMixin:
|
|||||||
else:
|
else:
|
||||||
self.assertTrue(hasattr(field, key),
|
self.assertTrue(hasattr(field, key),
|
||||||
msg='Expeting %s.%s to have \'%s\'' % (
|
msg='Expeting %s.%s to have \'%s\'' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
key
|
key
|
||||||
)
|
)
|
||||||
@ -67,7 +63,7 @@ class ModelTestCaseMixin:
|
|||||||
if value:
|
if value:
|
||||||
self.assertTrue(getattr(field, key) != value,
|
self.assertTrue(getattr(field, key) != value,
|
||||||
msg='Expeting %s.%s.%s to not be \'%s\'' % (
|
msg='Expeting %s.%s.%s to not be \'%s\'' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
key,
|
key,
|
||||||
value
|
value
|
||||||
@ -76,7 +72,7 @@ class ModelTestCaseMixin:
|
|||||||
else:
|
else:
|
||||||
self.assertFalse(hasattr(field, value),
|
self.assertFalse(hasattr(field, value),
|
||||||
msg='Expeting %s.%s to not have \'%s\'' % (
|
msg='Expeting %s.%s to not have \'%s\'' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
key
|
key
|
||||||
)
|
)
|
||||||
@ -94,7 +90,7 @@ class ModelTestCaseMixin:
|
|||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
field.max_length in range(min_length, max_length),
|
field.max_length in range(min_length, max_length),
|
||||||
msg='Expeting %s.%s field max_length to be in the range of %d and %d' % (
|
msg='Expeting %s.%s field max_length to be in the range of %d and %d' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
min_length,
|
min_length,
|
||||||
max_length
|
max_length
|
||||||
@ -116,7 +112,7 @@ class ModelTestCaseMixin:
|
|||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
len(field.validators) >= 1,
|
len(field.validators) >= 1,
|
||||||
msg='Expecting the first valiator of %s.%s to check the minimum' % (
|
msg='Expecting the first valiator of %s.%s to check the minimum' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name
|
field_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -124,7 +120,7 @@ class ModelTestCaseMixin:
|
|||||||
field.validators[0].limit_value,
|
field.validators[0].limit_value,
|
||||||
min_value,
|
min_value,
|
||||||
msg='Expecting the min value of %s.%s min to be at least %d' % (
|
msg='Expecting the min value of %s.%s min to be at least %d' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
min_value
|
min_value
|
||||||
)
|
)
|
||||||
@ -136,7 +132,7 @@ class ModelTestCaseMixin:
|
|||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
len(field.validators) >= index+1,
|
len(field.validators) >= index+1,
|
||||||
msg='Expecting the second valiator of %s.%s to check the maximum' % (
|
msg='Expecting the second valiator of %s.%s to check the maximum' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name
|
field_name
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@ -144,14 +140,14 @@ class ModelTestCaseMixin:
|
|||||||
field.validators[1].limit_value,
|
field.validators[1].limit_value,
|
||||||
max_value,
|
max_value,
|
||||||
msg='Expecting the max value of %s.%s min to be at most %d' % (
|
msg='Expecting the max value of %s.%s min to be at most %d' % (
|
||||||
self.model_name,
|
self.model.__name__,
|
||||||
field_name,
|
field_name,
|
||||||
max_value
|
max_value
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class SubmittableTestCase(ModelTestCaseMixin):
|
class SubmittableTestCase(ModelTestCase):
|
||||||
related_name = None
|
related_name = None
|
||||||
nullable = False
|
nullable = False
|
||||||
|
|
||||||
|
@ -5,12 +5,12 @@ from django.db import models
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
from lostplaces_app.models import Place, Taggable, MapablePoint
|
from lostplaces_app.models import Place, Taggable, MapablePoint
|
||||||
from lostplaces_app.tests.models import ModelTestCaseMixin
|
from lostplaces_app.tests.models import ModelTestCase
|
||||||
|
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
|
|
||||||
|
|
||||||
class TaggableTestCase(ModelTestCaseMixin, TestCase):
|
class TaggableTestCase(ModelTestCase):
|
||||||
|
|
||||||
model = Taggable
|
model = Taggable
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ class TaggableTestCase(ModelTestCaseMixin, TestCase):
|
|||||||
self.assertField('tags', TaggableManager)
|
self.assertField('tags', TaggableManager)
|
||||||
|
|
||||||
|
|
||||||
class MapablePointTestCase(ModelTestCaseMixin, TestCase):
|
class MapablePointTestCase(ModelTestCase):
|
||||||
|
|
||||||
model = MapablePoint
|
model = MapablePoint
|
||||||
|
|
||||||
|
@ -5,10 +5,10 @@ from django.db import models
|
|||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from lostplaces_app.models import Voucher
|
from lostplaces_app.models import Voucher
|
||||||
from lostplaces_app.tests.models import ModelTestCaseMixin
|
from lostplaces_app.tests.models import ModelTestCase
|
||||||
|
|
||||||
|
|
||||||
class VoucheTestCase(ModelTestCaseMixin, TestCase):
|
class VoucheTestCase(ModelTestCase):
|
||||||
model = Voucher
|
model = Voucher
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
Loading…
Reference in New Issue
Block a user