Do Django Fixtures load in incorrect order when testing?
Asked Answered
R

1

7

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?

Rippy answered 26/8, 2010 at 14:3 Comment(1)
can you post what the models from your fixtures look like?Managua
G
1

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?

Gladysglagolitic answered 26/8, 2010 at 16:44 Comment(1)
Hi Manoj, Thanks for your debugging suggestions. When I ran from the command line, I discovered that the fixtures were bombing because "creation_time" may not be null". After adding the creation_time to the fixtures they seem to run as I would expect. However, it still makes no sense to me why they would install seemingly ok in one order but not another. All my tests passed and the tests rely on that data being there.Rippy

© 2022 - 2024 — McMap. All rights reserved.