简体   繁体   中英

Django. AttributeError when running migrations on test database before tests in docker

Long story short, I am trying to run tests inside docker container like: ./manage.py test

but getting error:

/app/gauss # ./manage.py test
nosetests --where=tests/ --verbosity=2 --with-coverage --cover-package=gauss.catalogues --cover-package=gauss.requests --cover-package=gauss.core --cover-xml --cover-xml-file=gauss_coverage.xml --with-xunit --xunit-file=gauss_xunit.xml
Creating test database for alias 'default'...
/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py:259: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
  RuntimeWarning
Got an error creating the test database: database "test_gauss" already exists

Type 'yes' if you would like to try deleting the test database 'test_gauss', or 'no' to cancel: yes
Destroying old test database for alias 'default'...
Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)

...

File "/app/gauss/gauss/catalogues/migrations/0001_initial.py", line 13, in <module>
    class Migration(migrations.Migration):
  File "/app/gauss/gauss/catalogues/migrations/0001_initial.py", line 57, in Migration
    ('web_url', models.CharField(blank=True, max_length=100, validators=[gauss.catalogues.validators.validate_url], verbose_name='web_url')),
AttributeError: module 'gauss' has no attribute 'catalogues'

'gauss' is my django-project name and 'catalogoues' is app inside it. Error do not appers when I am running same command on my local computer.

All project structure:

在此处输入图片说明

Project settings:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'django_nose',
    'mptt',
    'sorl.thumbnail',
    'rest_framework',
    'webpack_loader',
    'gauss.catalogues',
    'gauss.ui.general',
    'gauss.requests'
]
...
TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = [
    '--where=tests/',
    '--verbosity=2',
    '--with-coverage',
    '--cover-package=gauss.catalogues',
    '--cover-package=gauss.requests',
    '--cover-package=gauss.core',
    '--cover-xml',
    '--cover-xml-file=gauss_coverage.xml',
    '--with-xunit',
    '--xunit-file=gauss_xunit.xml',
]

Part of migrations file that causing error:

# Generated by Django 2.0.2 on 2018-09-04 12:17

from django.conf import settings
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models
import django.db.models.deletion
import django.db.models.manager
import gauss.catalogues.model_fields
import gauss.catalogues.validators
import mptt.fields


class Migration(migrations.Migration):
..

        migrations.CreateModel(
            name='GSClient',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('deleted', models.BooleanField(default=False)),
                ('added_date', models.DateTimeField(blank=True, null=True)),
                ('name', models.CharField(max_length=100, verbose_name='Название')),
                ('web_url', models.CharField(blank=True, max_length=100, validators=[gauss.catalogues.validators.validate_url],)),
                ('email_domain', models.CharField(max_length=100, validators=[gauss.catalogues.validators.domain_validator], verbose_name='Почтовый домен')),
            ],
            options={
                'db_table': 'gs_catalogues_clients',
            },
        ),

Model that causing error

from django.db import models
from django.core.exceptions import ValidationError
from django.contrib.contenttypes.models import ContentType

from gauss.catalogues import validators, model_fields
from gauss.core import mixins

class GSClient(mixins.DeletedMixin, mixins.GSAddedDateMixin, models.Model):

    name = models.CharField(
        max_length=100,
    )
    web_url = models.CharField(
        max_length=100,
        blank=True,
        validators=[validators.validate_url],
    )
    email_domain = models.CharField(
        max_length=100,
        validators=[validators.domain_validator],
        blank=False,
        null=False,
    )
    structure = models.ForeignKey(
        'catalogues.GSClientStructure',
        null=True,
        related_name="structure_client",
        on_delete=models.SET_NULL,
    )

    class Meta:
        db_table = "gs_catalogues_clients"

I checked all attributes of module 'gauss' from shell and it's has module catalogues:

>>> import gauss
>>> print(gauss)
<module 'gauss' from '/app/gauss/gauss/__init__.py'>
>>> print(gauss.catalogues)
<module 'gauss.catalogues' from '/app/gauss/gauss/catalogues/__init__.py'>
>>> print(dir(gauss))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'catalogues', 'core', 'requests', 'settings', 'settings_local', 'ui']

Ok. I found solution. Problem is in my config

'--where=tests/'

need to be

'--where=gauss/tests/'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM