0

I'm trying to perform unit tests on Django with db mocking by mongomock.
My connection to the DB is made using settings.py:

DATABASES = {
    'dbname': {
        'ENGINE': 'djongo',
        'NAME': 'db1',
        'ENFORCE_SCHEMA': False,
        'CLIENT': {
            'host': DB_HOST,
            'port': DB_PORT,
            'username': DB_USERNAME,
            'password': DB_PASSWORD,
            'authSource': 'admin',
            'authMechanism': 'SCRAM-SHA-1'
        }
    },
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'users',
        'ENFORCE_SCHEMA': False,
        'CLIENT': {
            'host': DB_HOST,
            'port': DB_PORT,
            'username': DB_USERNAME,
            'password': DB_PASSWORD,
            'authSource': 'admin',
            'authMechanism': 'SCRAM-SHA-1'
        }
    }
}

and the test looks like this:

from django.contrib.auth.models import User
class BaseApiTest():
    def test_access_login(self):
        User.objects.create_user(
            username='admin',
            password='admin'
        )
        ....
        self.assertEqual(AccessAttempt.objects.count(), 1)

Here there is an internal connection of Django to the DB. (when its trying to access django.contrib.auth.models.User)
Any ideas?
The goal is to run the tests without a DB instance.
In other words, how can I overwrite the Django connection (self.databases) to the DB using a mongomock connection...

E.Bloch
  • 156
  • 1
  • 5

0 Answers0