Refactored ModelTestCaseMixin

This commit is contained in:
reverend 2020-09-13 18:37:21 +02:00
parent af14cce3f8
commit 27520c7ca4
3 changed files with 21 additions and 25 deletions

View File

@ -5,17 +5,13 @@ from django.test import TestCase
# 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_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={}):
'''
@ -31,14 +27,14 @@ class ModelTestCaseMixin:
except FieldDoesNotExist:
self.fail(
'Expecting %s to have a field named \'%s\'' % (
self.model_name,
self.model.__name__,
field_name
)
)
self.assertEqual(
type(field), field_class,
msg='Expecting type of %s.%s to be %s' % (
self.model_name,
self.model.__name__,
field_name,
field_class.__name__
)
@ -48,7 +44,7 @@ class ModelTestCaseMixin:
if value:
self.assertEqual(getattr(field, key), value,
msg='Expeting %s.%s.%s to be \'%s\'' % (
self.model_name,
self.model.__name__,
field_name,
key,
value
@ -57,7 +53,7 @@ class ModelTestCaseMixin:
else:
self.assertTrue(hasattr(field, key),
msg='Expeting %s.%s to have \'%s\'' % (
self.model_name,
self.model.__name__,
field_name,
key
)
@ -67,7 +63,7 @@ class ModelTestCaseMixin:
if value:
self.assertTrue(getattr(field, key) != value,
msg='Expeting %s.%s.%s to not be \'%s\'' % (
self.model_name,
self.model.__name__,
field_name,
key,
value
@ -76,7 +72,7 @@ class ModelTestCaseMixin:
else:
self.assertFalse(hasattr(field, value),
msg='Expeting %s.%s to not have \'%s\'' % (
self.model_name,
self.model.__name__,
field_name,
key
)
@ -94,7 +90,7 @@ class ModelTestCaseMixin:
self.assertTrue(
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' % (
self.model_name,
self.model.__name__,
field_name,
min_length,
max_length
@ -116,7 +112,7 @@ class ModelTestCaseMixin:
self.assertTrue(
len(field.validators) >= 1,
msg='Expecting the first valiator of %s.%s to check the minimum' % (
self.model_name,
self.model.__name__,
field_name
)
)
@ -124,7 +120,7 @@ class ModelTestCaseMixin:
field.validators[0].limit_value,
min_value,
msg='Expecting the min value of %s.%s min to be at least %d' % (
self.model_name,
self.model.__name__,
field_name,
min_value
)
@ -136,7 +132,7 @@ class ModelTestCaseMixin:
self.assertTrue(
len(field.validators) >= index+1,
msg='Expecting the second valiator of %s.%s to check the maximum' % (
self.model_name,
self.model.__name__,
field_name
)
)
@ -144,14 +140,14 @@ class ModelTestCaseMixin:
field.validators[1].limit_value,
max_value,
msg='Expecting the max value of %s.%s min to be at most %d' % (
self.model_name,
self.model.__name__,
field_name,
max_value
)
)
class SubmittableTestCase(ModelTestCaseMixin):
class SubmittableTestCase(ModelTestCase):
related_name = None
nullable = False

View File

@ -5,12 +5,12 @@ from django.db import models
from django.contrib.auth.models import User
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
class TaggableTestCase(ModelTestCaseMixin, TestCase):
class TaggableTestCase(ModelTestCase):
model = Taggable
@ -18,7 +18,7 @@ class TaggableTestCase(ModelTestCaseMixin, TestCase):
self.assertField('tags', TaggableManager)
class MapablePointTestCase(ModelTestCaseMixin, TestCase):
class MapablePointTestCase(ModelTestCase):
model = MapablePoint

View File

@ -5,10 +5,10 @@ from django.db import models
from django.utils import timezone
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
@classmethod