mardi 13 septembre 2016

Django reusable app testing failed: relation "auth_user" does not exist

I am currently working on a Django reusable app. One of my models need an OneToOneField

class MyModel(TimeStampedModel):
    author = models.OneToOneField(settings.AUTH_USER_MODEL)

When I run my runtest.py file that looks like the following:

import os
import sys

try:
    from django.conf import settings
    from django.test.utils import get_runner

    settings.configure(
        DEBUG=True,
        USE_TZ=True,
        DATABASES = {
            'default': {
                'ENGINE': 'django.db.backends.postgresql_psycopg2',
                'NAME': 'openrecipe_db',
                'USER': 'test_user',
                'PASSWORD': 'mypassword',
                'HOST': '',
                'PORT': '',
            }
        },
        ROOT_URLCONF="openrecipes.urls",
        INSTALLED_APPS=[
            "django.contrib.auth",
            "django.contrib.contenttypes",
            "django.contrib.sites",
            "openrecipes",
        ],
        SITE_ID=1,
        MIDDLEWARE_CLASSES=(),
    )

    try:
        import django
        setup = django.setup
    except AttributeError:
        pass
    else:
        setup()

except ImportError:
    import traceback
    traceback.print_exc()
    msg = "To fix this error, run: pip install -r requirements/development.txt"
    raise ImportError(msg)


def run_tests(*test_args, **kwargs):
    if not test_args:
        test_args = ['tests']

    # Run tests
    TestRunner = get_runner(settings)
    test_runner = TestRunner(verbosity=kwargs.get('verbosity', 2))

    failures = test_runner.run_tests(test_args)

    if failures:
        sys.exit(bool(failures))


if __name__ == '__main__':
    run_tests(*sys.argv[1:])

Then I get the following error:

$ python runtests.py 
    Creating test database for alias 'default' ('test_openrecipe_db')...
    Operations to perform:
      Synchronize unmigrated apps: openrecipes
      Apply all migrations: auth, contenttypes, sites
    Synchronizing apps without migrations:
      Creating tables...
        Creating table openrecipes_recipemodel
        Creating table openrecipes_ingredientmodel
        Running deferred SQL...
    Traceback (most recent call last):
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
        return self.cursor.execute(sql)
    psycopg2.ProgrammingError: relation "auth_user" does not exist


    The above exception was the direct cause of the following exception:

    Traceback (most recent call last):
      File "runtests.py", line 63, in <module>
        run_tests(*sys.argv[1:])
      File "runtests.py", line 56, in run_tests
        failures = test_runner.run_tests(test_args)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/test/runner.py", line 549, in run_tests
        old_config = self.setup_databases()
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/test/runner.py", line 499, in setup_databases
        self.parallel, **kwargs
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/test/runner.py", line 743, in setup_databases
        serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/db/backends/base/creation.py", line 70, in create_test_db
        run_syncdb=True,
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/core/management/__init__.py", line 130, in call_command
        return command.execute(*args, **defaults)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
        output = self.handle(*args, **options)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 173, in handle
        self.sync_apps(connection, executor.loader.unmigrated_apps)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 316, in sync_apps
        cursor.execute(statement)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
        return self.cursor.execute(sql, params)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
        raise value.with_traceback(tb)
      File "/home/arch/.environments/openrecipes_dev/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
        return self.cursor.execute(sql)
    django.db.utils.ProgrammingError: relation "auth_user" does not exist

Is it possible that at first the User model gets migratet and then everything else?

My current workaround is that I first run the test without the OneToOneField and also keeping my db: test_runner = TestRunner(verbosity=kwargs.get('verbosity', 2), keepdb=True) Then I add in my settings MIGRATION_MODULES = {"openrecipes": None},, enable the OneToOneField and run my tests again this time it works. But I want to get rid of this workaround I dont want to save my test db.

Aucun commentaire:

Enregistrer un commentaire