idlesign / django-siteprefs

Reusable app for Django introducing site preferences system
https://github.com/idlesign/django-siteprefs
BSD 3-Clause "New" or "Revised" License
17 stars 6 forks source link

pytest-django problem: no such table: siteprefs_preference #18

Open jayvdb opened 4 years ago

jayvdb commented 4 years ago

If migrations are not done first, pytest-django tries to configure django before running migrations, and fails

postgres is similar, with psycopg2.errors.UndefinedTable: relation "siteprefs_preference" does not exist

Workaround atm is to migrate first and then use pytest --reuse-db


exec poetry run py.test -vv --cov
Traceback (most recent call last):
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: siteprefs_preference
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/bin/py.test", line 11, in <module>
    sys.exit(main())
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/config/__init__.py", line 58, in main
    config = _prepareconfig(args, plugins)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/config/__init__.py", line 196, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/helpconfig.py", line 93, in pytest_cmdline_parse
    config = outcome.get_result()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/config/__init__.py", line 675, in pytest_cmdline_parse
    self.parse(args)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/config/__init__.py", line 845, in parse
    self._preparse(args, addopts=addopts)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/_pytest/config/__init__.py", line 809, in _preparse
    early_config=self, args=args, parser=self._parser
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pytest_django/plugin.py", line 325, in pytest_load_initial_conftests
    _setup_django()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/pytest_django/plugin.py", line 227, in _setup_django
    django.setup()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/siteprefs/config.py", line 19, in ready
    autodiscover_siteprefs()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/siteprefs/toolbox.py", line 171, in autodiscover_siteprefs
    Preference.read_prefs(get_prefs())
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/siteprefs/models.py", line 29, in read_prefs
    cls.objects.values().order_by('app', 'name')
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
    self._fetch_all()
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/models/query.py", line 113, in __iter__
    for row in compiler.results_iter(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size):
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1092, in results_iter
    results = self.execute_sql(MULTI, chunked_fetch=chunked_fetch, chunk_size=chunk_size)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1140, in execute_sql
    cursor.execute(sql, params)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "../backend/.cache/pypoetry/virtualenvs/django-backend-Wkjh5T4n-py3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: siteprefs_preference
``
idlesign commented 4 years ago

Yeah, creating required tables is a must. That's described in: https://django-siteprefs.readthedocs.io/en/latest/quickstart.html

Personally I don't bother with pytest-django and use pytest-djangoapp instead.

jayvdb commented 4 years ago

I tried setting up an RPM for pytest-djangoapp a few weeks ago at https://build.opensuse.org/package/show/home:jayvdb:django/python-pytest-djangoapp , but it fails the tests with

[   48s] + PYTHONPATH=:/home/abuild/rpmbuild/BUILDROOT/python-pytest-djangoapp-0.14.0-3.1.x86_64/usr/lib/python3.8/site-packages
[   48s] + PYTHONDONTWRITEBYTECODE=1
[   48s] + py.test-3.8 --ignore=_build.python2 --ignore=_build.python3 --ignore=_build.pypy3 -v
[   49s] ============================= test session starts ==============================
[   49s] platform linux -- Python 3.8.3, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
[   49s] cachedir: .pytest_cache
[   49s] rootdir: /home/abuild/rpmbuild/BUILD/pytest-djangoapp-0.14.0
[   49s] collecting ... collected 0 items / 1 error
[   49s] 
[   49s] ==================================== ERRORS ====================================
[   49s] ________________________ ERROR collecting test session _________________________
[   49s] Defining 'pytest_plugins' in a non-top-level conftest is no longer supported:
[   49s] It affects the entire test suite instead of just below the conftest as expected.
[   49s]   /home/abuild/rpmbuild/BUILD/pytest-djangoapp-0.14.0/pytest_djangoapp/tests/conftest.py
[   49s] Please move it to a top level conftest file at the rootdir:
[   49s]   None
[   49s] For more information, visit:
[   49s]   https://docs.pytest.org/en/latest/deprecations.html#pytest-plugins-in-non-top-level-conftest-files
[   49s] !!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
[   49s] =============================== 1 error in 0.14s ===============================

If that has been fixed, I will retry it.