Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#24658 closed Bug (fixed)

Schema tests fail when run in isolation

Reported by: Claude Paroz Owned by: vladiibine
Component: Testing framework Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Because tables are deleted in tearDown, when running an individual test, the tables are still existing and any create_model operation fail.

Change History (8)

comment:1 Changed 4 years ago by vladiibine

Owner: changed from nobody to vladiibine
Status: newassigned

comment:2 Changed 4 years ago by vladiibine

I've tried to test your scenario, but I think more information.

I dont know if I understood correctly, but I tried to run just one individual test, and use the SchemaEditor.create_model method to create a new model class, and instantiate it.

Here's my code, what works (I can create a dynamic model within the test and instantiate it - no errors)

from django.test import TestCase

from .models import MyModel

class MyTestCase(TestCase):
    def test_1(self):
        from django.db import connection

        with connection.schema_editor() as editor:
            other_model = self.create_model(editor)
            my_model = MyModel.objects.create(name='foo')

        other_model_instance = other_model.objects.create(name='bar')

    def test_2(self):
        my_model = MyModel(name='baz')

    def tearDown(self):
        print '>>>inside tearDown'
        # super(MyTestCase, self).tearDown()

    def create_model(self, editor):
        from django.db import models

        class OtherModel(models.Model):
            name = models.CharField(max_length=30, blank=True)

        editor.create_model(OtherModel)

        return OtherModel

Then from the command line, I ran the test suites with all 3 of these commands:

$ ./manage.py test core.tests.MyTestCase.test_1

$ ./manage.py test core.tests.MyTestCase.test_1 core.tests.MyTestCase.test_2

$ ./manage.py test

Please give more details on how you're getting this issue.

comment:3 Changed 4 years ago by vladiibine

Resolution: needsinfo
Status: assignedclosed

comment:4 Changed 4 years ago by vladiibine

Also, I ran this on both the 1.8 branch, and the master on the commit f043434174db3432eb63c341573c1ea89ef59b91, with Python version Python 2.7.5+

I'm releasing this ticket, so that someone else can take a look. If you provide more information, I'll check it out from time to time.

comment:5 Changed 4 years ago by Simon Charette

Claude, could you more details on which tests fail when run in isolation.

I couldn't reproduce with:

./runtests.py schema.tests.SchemaTests --settings=test_postgres

or

./runtests.py schema.tests.SchemaTests.test_creation_deletion

or

./runtests.py schema.tests.SchemaTests.test_creation_deletion schema.tests.SchemaTests.test_fk --settings=test_postgres

comment:6 Changed 4 years ago by Claude Paroz

I found the issue. The problem happens only with tests using the Note model, and the cause is that the Note model misses the apps = new_apps Meta attribute. I'll fix that ASAP.

comment:7 Changed 4 years ago by Claude Paroz <claude@…>

Resolution: needsinfofixed

In f54c0ec:

Fixed #24658 -- Added missing Meta attribute in schema tests

Without that, the Note model would be initially created and then
the tests using that model failed when run in isolation.

comment:8 Changed 4 years ago by Claude Paroz <claude@…>

In ff6e8681:

[1.8.x] Fixed #24658 -- Added missing Meta attribute in schema tests

Without that, the Note model would be initially created and then
the tests using that model failed when run in isolation.
Backport of f54c0ec06e from master.

Note: See TracTickets for help on using tickets.
Back to Top