Opened 6 years ago

Closed 6 years ago

#29948 closed Bug (fixed)

Single empty TransactionTestCase fails with IntegrityError

Reported by: Matt Westcott Owned by: nobody
Component: Testing framework Version: dev
Severity: Release blocker Keywords:
Cc: Romain Garrigues Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

https://github.com/django/django/commit/b3b1d3d45fc066367f4fcacf0b06f72fcd00a9c6 (#25251) appears to have introduced a regression where a TransactionTestCase consisting of a single empty test fails on teardown with an IntegrityError.

To reproduce:

  • start a new project (django-admin startproject testproject ; cd testproject)
  • add a new app (./manage.py startapp foo, add 'foo' to INSTALLED_APPS)
  • change foo/tests.py to the following:
from django.test import TransactionTestCase


class TestFoo(TransactionTestCase):
    def test_foo(self):
        pass
  • ./manage.py test now fails with:
Creating test database for alias 'default'…
System check identified no issues (0 silenced).
.E
======================================================================
ERROR: test_foo (foo.tests.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/sqlite3/base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py", line 211, in __call__
    self._post_teardown()
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py", line 926, in _post_teardown
    self._fixture_teardown()
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/test/testcases.py", line 967, in _fixture_teardown
    connections[db_name]._test_serialized_contents
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/base/creation.py", line 133, in deserialize_db_from_string
    obj.save()
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/core/serializers/base.py", line 223, in save
    models.Model.save_base(self.object, using=using, raw=True, **kwargs)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py", line 763, in save_base
    force_update, using, update_fields,
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py", line 854, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/base.py", line 892, in _do_insert
    using=using, raw=raw)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/query.py", line 1185, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/models/sql/compiler.py", line 1323, in execute_sql
    cursor.execute(sql, params)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/db/backends/sqlite3/base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: django_content_type.app_label, django_content_type.model

----------------------------------------------------------------------
Ran 1 test in 0.039s

FAILED (errors=1)
Destroying test database for alias 'default'…

On a commit prior to b3b1d3d45fc066367f4fcacf0b06f72fcd00a9c6 this succeeds as expected:

Creating test database for alias 'default'…
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.018s

OK
Destroying test database for alias 'default'…

Change History (4)

comment:1 by Tim Graham, 6 years ago

Cc: Romain Garrigues added
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 by Romain Garrigues, 6 years ago

Sorry about the regression. Will have a look.

comment:4 by Tim Graham, 6 years ago

Has patch: set
Patch needs improvement: set

PR but causes test failures and doesn't have a new test.

comment:5 by Tim Graham, 6 years ago

Resolution: fixed
Status: newclosed

The commit that caused this regression is reverted in 4c7c608a1deee37055d4a2b8a71e34def04a2a1f.

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