Django cannot run tests when DATABASES 'default' is empty

I have some problems running tests with setup DATABASES['default'] = {}

.

I got the following error on startup ./manage.py test

$ ./manage.py test --failfast
Creating test database for alias 'mydb'...
E
======================================================================
ERROR: test_admin (myapp.test.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
    self._pre_setup()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
    self._fixture_setup()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
    if not connections_support_transactions():
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
    for conn in connections.all())
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
    for conn in connections.all())
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
    res = instance.__dict__[self.func.__name__] = self.func(instance)
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
    self.connection.leave_transaction_management()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
    if managed == self.get_autocommit():
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
    self.ensure_connection()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
    self.connect()
  File "/home/thomas/.virtualenvs/tmpapp/local/lib/python2.7/site-packages/django/db/utils.py", line 86, in __exit__
    db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
AttributeError: 'DatabaseWrapper' object has no attribute 'Database'

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)
Destroying test database for alias 'mydb'...

      

settings.py

..
DATABASES = {
    'default': {},
    'mydb': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}
DATABASE_ROUTERS = ['myapp.router.MyRouter']

      

router.py

class MyRouter:
    def db_for_read(self, model, **hints):
        return 'mydb'

    def db_for_write(self, model, **hints):
        return 'mydb'

    def allow_migrate(self, db, model):
        if db == 'default':
            return False
        return True

      

test.py

from django.test import TestCase                                                                                                                                       


class MyTest(TestCase):

    def test_admin(self):
        self.assertEqual(1, 1)

      

I did some investigation and found that I self.connect()

was being called to a dummy server associated with an alias 'default'

. Since the dummy backend doesn't implement many functions, the call is aborted. How do I run tests with DATABASES['default'] = {}

?

+3


source to share


1 answer


Use different settings.py files for production / development / testing (best).



And or use Sqlite as default entry (fastest).

0


source







All Articles