Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django 1.8 tests with models and migrations

I use django 1.8 I have a model with reference to django.contrib.contenttypes.ContentType:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

    def __str__(self):              # __unicode__ on Python 2
        return self.tag

And I have a tests.py:

class TestObj(models.Model):
    value = models.IntegerField(default=42, null=False)

    def __str__(self):
        text = "%s-%i" % ("name", self.value)
        return text


class MyUnitTest(TestCase):
    def setUp(self):
        TestObj.objects.create(value=40)
        TestObj.objects.create()

    def test_my_test(self):
        obj1 = TestObj.objects.get(value=40)
        obj2 = TestObj.objects.get(value=42)
        self.assertEqual(obj1.value, 40)
        self.assertEqual(obj2.value, 42)

When I try to create DB without migrations (and "test" too), I get an error with an invalid reference:

./manage.py syncdb
...
django.db.utils.ProgrammingError: ERROR: reference "django_content_type" does not exist

But if I create a migration (./manage.py makemigrations myobj), migration contains only a models.py model (TaggedItem):

Migrations for 'myobj':
  0001_initial.py:
    - Create model TaggedItem

After I try to test my app and I get an error with reference myob_testobj does not exist:

./manage.py test -v3
Creating test database for alias 'default' ('testdb_49308_4288843')...
Operations to perform:
  Synchronize unmigrated apps: staticfiles
  Apply all migrations: myobj, contenttypes, sessions
Synchronizing apps without migrations:
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application myobj
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Loading 'initial_data' fixtures...
Checking 'mypro' for fixtures...
No fixture 'initial_data' in 'mypro'.
Installed 0 object(s) from 0 fixture(s)
Running migrations:
  Rendering model states... DONE (0.010s)
  Applying contenttypes.0001_initial... OK (0.133s)
  Applying contenttypes.0002_remove_content_type_name... OK (0.017s)
  Applying myobj.0001_initial... OK (0.217s)
  Applying sessions.0001_initial... OK (0.233s)
Running post-migrate handlers for application contenttypes
Adding content type 'contenttypes | contenttype'
Running post-migrate handlers for application sessions
Adding content type 'sessions | session'
Running post-migrate handlers for application myobj
Adding content type 'myobj | taggeditem'
Adding content type 'myobj | testobj'
Traceback
...
django.db.utils.ProgrammingError: ERROR: reference "myobj_testobj" does not exist

If I don't use a migration and run test, table myobj_testobj was created but I get an error with no reference to "django_content_type"

/manage.py test -v3
Creating test database for alias 'default' ('testdb_773982_6463361')...
Operations to perform:
  Synchronize unmigrated apps: staticfiles, myobj
  Apply all migrations: contenttypes, sessions
Synchronizing apps without migrations:
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application myobj
  Creating tables...
    Creating table myobj_taggeditem
    **Creating table myobj_testobj**
    Running deferred SQL...
Traceback
...
django.db.utils.ProgrammingError: ERROR: reference "django_content_type" does not exist

How do I use tests with models? Thank you.

like image 317
Shi3A Avatar asked Nov 21 '22 20:11

Shi3A


1 Answers

Django cant discover model TestObj because your test.py is not an app and there is no any installed app that describes TestObj in settings.INSTALLED_APPS. You need to add model TestObj to module myobj.models.

After that run python manage.py makemigrations myobj and then migration for TestObj will be created. So you can run your tests.

like image 194
norecces Avatar answered Nov 24 '22 12:11

norecces