vendredi 9 novembre 2018

Django Tests use the production database when making queries on the static elements of the class, why does this happen?

I have two Databases, one for use with Django (app_db) and another read-only one that is not stained with Django (remote_db), I have my custom router defined (db_for_read, db_for_write, allow_relation) and selects the right one when used with an app (module) created to manipulate the data, inclusive in the tests. In addition, remote_db to their own migrations.

Distributions Apps and Database:
- app news_module with model class News, NewsCategoryClassifier, use app_db.
- app categories_module with model class Category, use remote_db.

My Setting:
DATABASE_ROUTERS = ['src.core.router.MyCustomRouter']

DATABASES = { 'default': env.db('APP_DB'),

'remote_db': env.db('REMOTE_DB')

}

my test:

class CategoriesClassifierTest(TestCase):
def test_instance_have(self):
    Category.object.create(name='Sports')
    Category.object.create(name='Financials')

    cl = NewsCategoryClassifier()

    self.assertIsInstance(cl, NewsCategoryClassifier)
    self.assertEquals(['Sports', 'Financials'], cl.choices)

This works as expected.

class NewsCategoryClassifier:
    def __init__(self):
        self.choices = Categories.objects.all()

But when I run the test that involves a query on a class attribute I get data from the production database.

    class NewsCategoryClassifier:
        choices = Categories.objects.all()

I would like to get how I can test the data in NewsCategoryClassifier from test_remote_db. I don't know if by creating a custom TestRunner I can come up with my solution.

Aucun commentaire:

Enregistrer un commentaire