From 27520c7ca482e67892ce1ef3d2251e4458be5126 Mon Sep 17 00:00:00 2001 From: reverend Date: Sun, 13 Sep 2020 18:37:21 +0200 Subject: [PATCH] Refactored ModelTestCaseMixin --- .../lostplaces_app/tests/models/__init__.py | 36 +++++++++---------- .../tests/models/test_abstract_models.py | 6 ++-- .../tests/models/test_voucher_model.py | 4 +-- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lostplaces/lostplaces_app/tests/models/__init__.py b/lostplaces/lostplaces_app/tests/models/__init__.py index c24309e..a204585 100644 --- a/lostplaces/lostplaces_app/tests/models/__init__.py +++ b/lostplaces/lostplaces_app/tests/models/__init__.py @@ -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 diff --git a/lostplaces/lostplaces_app/tests/models/test_abstract_models.py b/lostplaces/lostplaces_app/tests/models/test_abstract_models.py index e4a6d70..8bd8967 100644 --- a/lostplaces/lostplaces_app/tests/models/test_abstract_models.py +++ b/lostplaces/lostplaces_app/tests/models/test_abstract_models.py @@ -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 diff --git a/lostplaces/lostplaces_app/tests/models/test_voucher_model.py b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py index 5c22f02..0841853 100644 --- a/lostplaces/lostplaces_app/tests/models/test_voucher_model.py +++ b/lostplaces/lostplaces_app/tests/models/test_voucher_model.py @@ -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