Unit testing Django model mixins

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.test import TestCase
from django.db import connection
from django.core.management.color import no_style
from django.db.models.base import ModelBase
 
class ModelMixinTestCase(TestCase):
    """
    Base class for tests of model mixins. To use, subclass and specify
    the mixin class variable. A model using the mixin will be made
    available in self.model.
    """
 
    def setUp(self):
        # Create a dummy model which extends the mixin
        self.model = ModelBase('__TestModel__'+self.mixin.__name__, (self.mixin,),
            { '__module__': self.mixin.__module__ })
 
        # Create the schema for our test model
        self._style = no_style()
        sql, _ = connection.creation.sql_create_model(self.model, self._style)
 
        self._cursor = connection.cursor()
        for statement in sql:
            self._cursor.execute(statement)
 
    def tearDown(self):
        # Delete the schema for the test model
        sql = connection.creation.sql_destroy_model(self.model, (), self._style)
        for statement in sql:
            self._cursor.execute(statement)

More like this

  1. Test runner that installs 'tests' packages as apps by adrian_lc 6 months, 4 weeks ago
  2. Decorator for printing unit test name by tomaszzielinski 4 years, 2 months ago
  3. Django Admin CSV Export Mixin by Ceran 1 year ago
  4. Run a testcase with custom INSTALLED_APPS by vdboor 1 year, 5 months ago
  5. Play nice with ModelAdmin mixins by chris.dickinson 5 years, 3 months ago

Comments

(Forgotten your password?)