Search code examples
djangounit-testingdjango-modelsdjango-testingdjango-tests

Django unit testing: How should one test abstract models?


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?


Solution

  • 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)