Is there a way to connect to django test database using connections
?
I have tried
cursor = connections['test_name_of_main_db'].cursor()
and also specified the test db name in settings but I still receive errors:
Traceback (most recent call last):
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 176, in ensure_defaults
conn = self.databases[alias]
KeyError: 'auto_tests'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/tets/healthware_server/component/medical/tests.py", line 72, in test_model_observation
cursor = connections['auto_tests'].cursor()
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 208, in __getitem__ self.ensure_defaults(alias)
File "/home/tets/4d/lib/python3.5/site-packages/django/db/utils.py", line 178, in ensure_defaults
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
django.db.utils.ConnectionDoesNotExist: The connection auto_tests doesn't exist
According to the docs:
The default test database names are created by prepending
test_
to the value of each NAME in DATABASES.
But in your stacktrace the error says:
django.db.utils.ConnectionDoesNotExist: The connection auto_tests doesn't exist
Are you sure you are using the right alias? According to the error, it is looking for a database named auto_tests
.
As another debugging step, you could print all the aliases during the test to see which ones are available
for alias in connections:
print(alias)
Of course, you could also try to use the default database and see if that works for you during the tests:
# note that it is 'connection' and not 'connections'
from django.db import connection
cursor = connection.cursor()