i’m starting the TDD development attitude and am writting unit-tests for my django application. I’m aware of fixtures and know that’s the way tests should be executed, but for a given test i do need to execute it on the whole database, and json fixture for 10+ million row database is not something i’d like to handle, moreover, this test is “read-only”.
So the question is how are you setting up your test suites to run on the production database? I imagine it could be as easy as adding the DATABASE_NAME setting in the setUp method of certain test. But the settings.DATABASE_NAME=”prod_db” results in “NameError: global name ‘settings’ is not defined” while running the test. Moreover, there is a risk described in http://code.djangoproject.com/ticket/11987, that you can accidentally delete a production database.
So, how is it possible, or, even better, what is best practice, to run a single test of a test suite on a production database instead of temporary one?
Cheers in advance for any opinions!
First, if you’re running it on the production database, it isn’t much of a “unit” test.
It’s a first-class batch job and needs to be treated like a first-class production batch job.
You cam’t to use the Django
testcommand for looking at production data. It always creates an empty database which is populated from fixtures in the TestCase.You could make your production database processing a proper management command. This has the environment all properly configured so that your command can simply use the Django ORM to process your data.
The alternative is to be sure that you configure your settings. Either
use the
DJANGO_SETTINGS_MODULEenvironment variable or use thesettings.configure()function to create an environment.You can then import the models and do the processing you want to do against the production database.
You can call it “test” if you want to, but you’re looking at production data, so it’s got to be treated like production application with respect to getting the settings file and using the proper ORM configuration.