Django unit testing: How should one test abstract models?

J. *_*ers 2 django unit-testing django-models django-testing django-tests

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
Run Code Online (Sandbox Code Playgroud)

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:

core/
    __init__.py
    behaviors.py  
    models.py
    utils.py
    views.py
Run Code Online (Sandbox Code Playgroud)

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?

Seb*_*che 5

Anjaneyulu Batta的回答令人惊奇,但不那么易读,如果 Django 团队改变connection内部行为方式,那么维护性可能会降低。

我会怎么做:

  1. 通过使用此抽象类的任何模型测试抽象类的通用属性。
  2. 测试这是对抽象类的子类化。
  3. 测试具体属性此模型。
  4. 对任何其他模型重复 2 和 3。

示例:一个抽象类Parallelogram和一个使用它的模型称为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):
        ...
Run Code Online (Sandbox Code Playgroud)