Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Django Fixtures load in incorrect order when testing?

I am testing my application and I am running into an issue and I'm not sure why. I'm loading fixtures for my tests and the fixtures have foreign keys that rely on each other. They must be loaded in a certain order or it won't work.

The fixtures I'm loading are:

["test_company_data", "test_rate_index", 'test_rate_description']

Company data is the first one. test_rate_index has a foreign key to company, and test_rate_description has a foreign key to a model declared in test_rate_index. (as an aside, different test need different fixtures which is why I'm not just shoving everything in one)

If I use django's standard procedure for loading tests, the tests do not load in the proper order.

class TestPackages(test.TestCase):
    fixtures = ["test_company_data", "test_rate_index", "test_rate_description",]

I get the message

DoesNotExist: RateDescription matching query does not exist.

But if I reverse the order of my fixtures (which makes no sense) it works:

fixtures = ["test_rate_description", "test_company_data", "test_rate_index",]

Django's documentation states that the fixtures load in the order they are declared, but this doesn't seem to be the case.

As a workaround, instead of using django's

    call_command('loaddata', *fixtures, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                         })

I'm using a different function in the setUp method that loads the fixtures one at a time.

def load_fixtures(fixtures):
    for fixture in fixtures:
        call_command('loaddata', fixture, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                            })

Is there something I'm doing incorrectly or not understanding that is causing my fixtures not to be loaded in the proper order when trying to use the standard method?

like image 565
Aaron Avatar asked Aug 26 '10 14:08

Aaron


1 Answers

Django's documentation states that the fixtures load in the order they are declared, but this doesn't seem to be the case.

This is certainly strange. Fixtures are getting loaded in the proper order when I tested one of my projects (Django 1.2.1, Python 2.6.2, Postgresql 8.3.11).

Here is what I'd do to troubleshoot.

DoesNotExist: RateDescription matching query does not exist.

  1. Are you getting this error when loading a fixture or when executing a test? Can you find the fixture/code that is raising this? Increase verbosity if need be.

  2. Can you try firing a loaddata command from the command line? Call it three times, passing the name of one fixture for each call in the proper expected sequence. And see if the fixtures get loaded.

  3. I know you'd probably have done this already but can you make sure that the first and second fixtures do not contain any RateDescription data?

like image 172
Manoj Govindan Avatar answered Sep 21 '22 23:09

Manoj Govindan