Using a persistent database in Django unittests

I have a large read-only Wordnet PostgreSQL database that I would like to use with Django unittests. In particular, I have an application called "wordnet" that wraps this Wordnet database. Unfortunately, by default, the Django unittest framework uses an empty in-memory SQLite database for all applications.

How to use my PostgreSQL database only for wordnet application and other applications within unittests?

I am familiar with Django database routers , and I think they might be the solution. So I created the following in my routers.py:

NEEDS_REAL_DB_APPS = ( 'wordnet', 'auth', 'contenttypes', ) REAL_DB = 'default' class UseRealDBRouter(object): def db_for_read(self, model, **hints): if model._meta.app_label in NEEDS_REAL_DB_APPS: return REAL_DB return None def db_for_write(self, model, **hints): if model._meta.app_label in NEEDS_REAL_DB_APPS: return REAL_DB return None def allow_relation(self, obj1, obj2, **hints): if obj1._meta.app_label in NEEDS_REAL_DB_APPS and obj2._meta.app_label in NEEDS_REAL_DB_APPS: return True return None def allow_syncdb(self, db, model): if db == REAL_DB: return model._meta.app_label in NEEDS_REAL_DB_APPS elif model._meta.app_label in NEEDS_REAL_DB_APPS: return False return None 

And my test.py looks like this:

 from django.test import TestCase from wordnet import models as wn_models class Tests(TestCase): def test_wordnet(self): q = wn_models.Word.objects.all() self.assertEqual(q.count(), 86547) 

However, when I run my unittest (for example, manage.py test myapp.Tests.test_wordnet ), the check still fails, returning 0 to count all the words, indicating that it still does not use the β€œreal” database. What am I doing wrong?

+4
source share
1 answer

You should not use this database for testing.

How about the first reset of your production database: look here

and then upload it to test instruments: check this

+2
source

All Articles