dimanche 31 janvier 2021

Django testing (TestCase) with multiple database is failing

I use 2 databases on my django app - the first is MySQL and the second is Mongo (using the djongo engine).

I started writing some tests using the TestCase and i want to use sqlite3 engine.

My DBs configuration: (You can see that i have assigned TEST DBs on sqlite]

#settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DEFAULT_NAME,
        'USER': DEFAULT_USER,
        'PASSWORD': DEFAULT_PASSWORD,
        'HOST': 'my-host-here',
        'PORT': '3306',
        'TEST': {
            'NAME': 'test_dev'
        }
    },

    'mongoDB': {
    'ENGINE': 'djongo',
    'CLIENT': {
        'host': 'mongodb+srv://{}:{}@{}/?retryWrites=true&w=majority'.format(
            username,
            password,
            host),
        'username': username,
        'password': password,
        'name': name,
        'authSource': authSoruce,
        'ssl':ssl,
        'authMechanism': 'SCRAM-SHA-1',
        }
    }
}

# If django runs unittests - run on a local sqlite DB
if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
        'SUPPORTS_TRANSACTIONS': True
    }

    DATABASES['mongoDB'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase2',
        'SUPPORTS_TRANSACTIONS': True
    }

When i run the tests, I get failures when Django is trying to create the mongoDB TEST database. error: return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: release_notes_releasenotesformmodel (release_notes_releasenotesformmodel is the only model i have in the mongoDB)

test script:

    databases = '__all__'

    def SetUp(self):
        self.client = Client()
        self.dummy_test = EdisonAbTestsListModel.objects.create(
            test_id=DUMMY_TEST_ID_1
        )

    def run_test(self):
        ## here is my test logic...

As you can see i tried to use the databases = '__all__' in the TestCase config, but it won't help

EDIT: this i the release_notes_releasenotesformmodel model:

    class Meta:
        db_table = 'release_notes'

    _DATABASE = "edison_mongo"

    # Automated fields
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    updated_at = models.DateTimeField(auto_now=True)
    # Model Fields
    '''
        test_id is actually the primary key in to EdisonAbTestsListModel
        Notice: for 'full_rollout' RNs, test_id is populated but has no meaning. 
                for 'full_rollout' RNs The minimum id should match FULL_ROLLOUT_RN_MINIMUM_ID from views.py 
    '''
    test_id = models.IntegerField(primary_key=True)

    release_note_title = models.CharField(max_length=300, blank=True, null=False)
    user_group = models.CharField(max_length=200, blank=True, null=True)

    tl_dr = models.CharField(max_length=10000, blank=True, null=True)
    owner = models.CharField(max_length=200, blank=True, null=True)
    dev_owner = models.CharField(max_length=200, blank=True, null=True)
    data_owner = models.CharField(max_length=200, blank=True, null=True)
    ds_owner = models.CharField(max_length=200, blank=True, null=True)

Aucun commentaire:

Enregistrer un commentaire