In my Django Project I have an app called 'core' which contains all my reusable model mixins / abstract models (behaviors.py), models (models.py), views (views.py) and helpers functions (utils.py):
core/
__init__.py
behaviors.py
models.py
utils.py
views.py
I now want to write tests for these files. For models, utils and views I just wrote unit tests like I'm used to.
I'm now uncertain how I should test the abstract models contained in behaviors.py. For example, I have this model mixin:
import uuid as uuid_lib
from django.db import models
class UniversallyUniqueIdentifiable(models.Model):
uuid = models.UUIDField(
db_index=True,
default=uuid_lib.uuid4,
editable=False
)
class Meta:
abstract = True
How can you test an abstract model? In one of the articles I used to learn about fat models, the author just tests the models in which he used the abstract models. But that feels not very DRY to me since that would mean I would have to test the adding of the UUID in each model I use it in. Is there a better way to do it?
Anjaneyulu Batta 's answer is amazing but not so readable and might be less maintainable if the Django team changes the way connection
behaves internally.
What I would do:
Example: one abstract class Parallelogram
and one model using it called Square
.
from unittest import TestCase
from tetrahedrons.models import Parallelogram, Square
class ParallelogramAbstractModelTest(TestCase):
def test_has_four_sides(self):
...
def test_parallel_opposite_sides(self):
...
class SquareModelTest(TestCase):
def test_subclasses_mobel_base(self):
self.assertTrue(issubclass(Square, Parallelogram))
def test_equal_sides(self):
...
try below code
from django.db import connection
from django.db.models.base import ModelBase
from django.test import TestCase
from .models import UniversallyUniqueIdentifiable
import uuid
class TestUniversallyUniqueIdentifiable(TestCase):
model = UniversallyUniqueIdentifiable
def setUp(self):
# Create a dummy model
self.model = ModelBase(
'__TestModel__' + self.model.__name__, (self.model,),
{'__module__': self.model.__module__}
)
# Create the schema for our test model
with connection.schema_editor() as schema_editor:
schema_editor.create_model(self.model)
def test_mytest_case(self):
self.model.objects.create(uuid=uuid.uuid4())
self.assertEqual(self.model.objects.count(), 1)
def tearDown(self):
# Delete the schema for the test model
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(self.model)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.