django-shop
django-shop copied to clipboard
django-shop breaks my project's testsuite
Traceback below.
The problem seems to be that we are calling connection.cursor()
in shop/models/fields.py
at module level, and that uses the connection to the default database, not the test database. I will investigate this ASAP.
Traceback (most recent call last):
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: database "acceed-shop" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/py/test.py", line 4, in <module>
sys.exit(pytest.main())
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/config.py", line 47, in main
config = _prepareconfig(args, plugins)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/config.py", line 132, in _prepareconfig
pluginmanager=pluginmanager, args=args)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
_MultiCall(methods, kwargs, hook.spec_opts).execute()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 595, in execute
return _wrapped_call(hook_impl.function(*args), self.execute)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 249, in _wrapped_call
wrap_controller.send(call_outcome)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/helpconfig.py", line 32, in pytest_cmdline_parse
config = outcome.get_result()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 278, in get_result
raise ex[1].with_traceback(ex[2])
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 264, in __init__
self.result = func()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
res = hook_impl.function(*args)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/config.py", line 882, in pytest_cmdline_parse
self.parse(args)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/config.py", line 1032, in parse
self._preparse(args, addopts=addopts)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/config.py", line 1003, in _preparse
args=args, parser=self._parser)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 724, in __call__
return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 338, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 333, in <lambda>
_MultiCall(methods, kwargs, hook.spec_opts).execute()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 595, in execute
return _wrapped_call(hook_impl.function(*args), self.execute)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 253, in _wrapped_call
return call_outcome.get_result()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 278, in get_result
raise ex[1].with_traceback(ex[2])
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 264, in __init__
self.result = func()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/_pytest/vendored_packages/pluggy.py", line 596, in execute
res = hook_impl.function(*args)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/pytest_django/plugin.py", line 245, in pytest_load_initial_conftests
_setup_django()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/pytest_django/plugin.py", line 148, in _setup_django
django.setup()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/rene/acceed2/acceed/models.py", line 15, in <module>
from shop.models.defaults.address import BillingAddress, ShippingAddress # noqa
File "/home/rene/django-shop/shop/models/__init__.py", line 2, in <module>
from .notification import Notification, NotificationAttachment
File "/home/rene/django-shop/shop/models/notification.py", line 18, in <module>
from .customer import CustomerModel
File "/home/rene/django-shop/shop/models/customer.py", line 18, in <module>
from shop.models.fields import JSONField
File "/home/rene/django-shop/shop/models/fields.py", line 13, in <module>
with connection.cursor() as cursor:
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/django/db/backends/postgresql/base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/rene/.virtualenvs/acceed-shop/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: FATAL: database "acceed-shop" does not exist
Hi @rfleschenberg , Could you please share your failing tests in order to try to fix it. Thanks
@khchine5: I'd have to share the whole project, which I can't right now. But I will create a reproducible test case ASAP.
BTW, you should be able to reproduce this with any testcase that uses a model with a JSONField, I think. But I will try to send a testcase tomorrow.
To reproduce the issue:
- Activate the django-shop virtualenv
-
pip install psycopg2
Change your settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'djangoshop',
}
}
You should get this:
django.db.utils.OperationalError: FATAL: database "djangoshop" does not exist
This is wrong, because it refers to the default database, not the test database. The tests should not rely on the default database being present.
Can we just document that we require Postgres >= 9.4 and psycopg >= 2.5.4? This would make the code much simpler.
Yes, I think it could be a good idea. but in the other hand ,it could be more complicated for the end developer and to run demo projects.
An other point about testing with a postgres database , Django offers the possibility to specify the database's name when testing. We need also to grand to the database user write access to our testing database. See : https://docs.djangoproject.com/es/1.10/topics/testing/overview/#the-test-database
We can hande this on travis, so we don't have to change anything for our users. We just need to use an environment var or a separate settings module and modify .travis-ci.yml
:)