Search code examples
djangoautomated-testsintegration-testingdjango-testing

Django test client, not creating models (--keepdb option being used)


I'm trying to setup some models in the test database and then post to a custom form which includes a file upload.

Nothing seems to be persisting in the database, and I'm unsure about why the test when I perform the POST is sending back a 200 response? With follow=False, shouldn't it be a 302?

Also, when I try to look for the model in the database, it finds nothing.

And when I look at the database when using the --keepdb option, nothing is there either?

What am I doing wrong?

class ImportTestCase(TestCase):
    remote_data_url = "http://test_data.csv"
    local_data_path = "test_data.csv"
    c = Client()
    password = "password"

    def setUp(self):
        utils.download_file_if_not_exists(self.remote_data_url, self.local_data_path)
        self.my_admin = User.objects.create_superuser('jonny', '[email protected]', self.password)
        self.c.login(username=self.my_admin.username, password=self.password)

    def test_create_organisation(self):
        self.o = Organization.objects.create(**{'name': 'TEST ORG'})

    def test_create_station(self):
        self.s = Station.objects.create(**{'name': 'Player', 'organization_id': 1})

    def test_create_window(self):
        self.w = Window.objects.create(**{'station_id': 1})

    def test_create_component(self):
        self.c = Component.objects.create(**{
            'type': 'Player',
            'window_id': 1,
            'start': datetime.datetime.now(),
            'end': datetime.datetime.now(),
            'json': "",
            'layer': 0}
                                          )

    def test_csv_import(self):
        """Testing that standard csv imports work"""
        self.assertTrue(os.path.exists(self.local_data_path))
        with open(self.local_data_path) as fp:
            response = self.c.post('/schedule/schedule-import/create/', {
                'component': self.c,
                'csv': fp,
                'date_from': datetime.datetime.now(),
                'date_to': datetime.datetime.now()
            }, follow=False)

        self.assertEqual(response.status_code, 200)

    def test_record_exists(self):
        new_component = Component.objects.all()
        self.assertTrue(len(new_component) > 0)

And the test results

Using existing test database for alias 'default'...
.....[]
F
======================================================================
FAIL: test_record_exists (schedule.tests.ImportTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "tests.py", line 64, in test_record_exists
    self.assertTrue(len(new_component) > 0)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 6 tests in 1.250s

FAILED (failures=1)

Solution

  • The --keepdb option means that the database is kept. That means it's quicker to run the tests again because you don't have to recreate the table.s

    However, each test method in a TestCase class is run in a transaction which is rolled back after the method has finished. Using --keepdb doesn't change this.

    This means that your object created in test_create_component will not be seen by the test_record_exists test. You can either create the object inside the test_record_exists method, or in the setUp method or setUpTestData classmethod.