Pull requests

#157 Open
Repository
tony-zhu
Branch
default
Repository
andrewgodwin
Branch
default

Fix loading initial_data for unit tests (#1327)

Bitbucket cannot automatically merge this request.

The commits that make up this pull request have been removed.

Bitbucket cannot automatically merge this request due to conflicts.

Review the conflicts on the Overview tab. You can then either decline the request or merge it manually on your local system using the following commands:

hg update default
hg pull -r default https://bitbucket.org/tony-zhu/south
hg merge e31544fbf7b7
hg commit -m 'Merged in tony-zhu/south (pull request #157)'
Author
  1. Tony Zhu
Reviewers
Description

The django db.backends.creation.BaseDatabaseCreation.create_test_db calls syncdb with load_initial_data=False

Then call the flush and uses its side affect to load the initial data.

But the flush command is hacked by South to do nothing.

Added the load initial data to the fake flush command to solve the problem.

  • Learn about pull requests

Comments (4)

  1. Tony Zhu author

    The Django unit test calls the "create_test_db" method in "django/db/backends/creation.py" (line 309 in Django 1.6.1) to create the test database.

    This method calls two commands "syncdb" and "flush" to initialize the test database.

    The comments before calling the "flush" command says:

            # We need to then do a flush to ensure that any data installed by
            # custom SQL has been removed. The only test data should come from
            # test fixtures, or autogenerated from post_syncdb triggers.
            # This has the side effect of loading initial data (which was
            # intentionally skipped in the syncdb).
            call_command('flush',
                verbosity=max(verbosity - 1, 0),
                interactive=False,
                database=self.connection.alias)
    

    South hacked the "flush" command to do nothing which skips the "load_initial_data" step for the unit tests. That is what I fixed in this commit.

    This bug did not show up until 0.8.2 because South's "syncdb" command did not handle the "load_initial_data" option correctly and loaded the fixtures in the syncdb step.

  2. hjkelly

    I just ran into this problem a few hours ago and thought I was losing my mind — I couldn't get my only initial_data.json to load in tests.

    The patch below did fix the problem for me on South 0.8.4.