Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - specify database for TestCase fixtures

I have two databases that my site uses and I have an app that uses both of them. I need to write a TestCase that loads fixtures for both databases. I use a DB router, which works fine in production, but in the testing framework, Django insists on using the 'default' database for all fixtures, even for models that specify the other database. How do I tell Django to run a fixture against another database?

My TestCase is defined list:

class VerifierTestCase(TestCase):
    fixtures = ['zipcodes_test.json', 'all_states.json', 'wtf.json']
    multi_db = True
like image 328
matt snider Avatar asked Nov 03 '10 04:11

matt snider


People also ask

How do I load all fixtures in Django?

By default, Django only loads fixtures into the default database. Use before_scenario to load the fixtures in all of the databases you have configured if your tests rely on the fixtures being loaded in all of them. You can read more about it in the Multiple database docs.

What is assert in Django?

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.


2 Answers

There is actually a bug in Django that causes it to ignore the name-based db-specific pointers if you specify the entire fixture name.

so if you do fixtures = ["mydata.default.yaml", "mydata.myotherdatabase.yaml"]

It will load both fixtures into the default database.

But if you do fixtures = ['mydata']

It will load correctly. This is also true for dbengine specific filenames (e.g. mydata.default.postgresql.sql) as well.

like image 88
zenWeasel Avatar answered Sep 24 '22 13:09

zenWeasel


Fixtures are targeted at specific databases by filename. This is true in TestCase instances as well, as they just call the loaddata command.

See https://docs.djangoproject.com/en/dev/ref/django-admin/#database-specific-fixtures

like image 32
GDorn Avatar answered Sep 24 '22 13:09

GDorn