1. Andrew Godwin
  2. south
  3. Pull requests

Pull requests

#157 Open
Repository
Deleted repository
Branch
default (e31544fbf7b7)
Repository
south
Branch
default

Fix loading initial_data for unit tests (#1327)

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.