#157 Open
Deleted repository
default (e31544fbf7b7)

Fix loading initial_data for unit tests (#1327)

  1. Tony Zhu

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.

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).
                verbosity=max(verbosity - 1, 0),

    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.