Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23598 closed Bug (fixed)

The testing framework has leaky scoping when defining models in tests

Reported by: Nicholas A Lourie Owned by: nobody
Component: Testing framework Version: 1.6
Severity: Normal Keywords:
Cc: developer.nick@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

If you define two subclasses of the models.Model class with the same name in two different methods of a TestCase subclass, they should exist in separate scopes and so be two completely distinct classes; however, whichever test method is run first has it's model subclass definition eclipse the others by the same name.

This bug occurs in Django 1.6, but in Django 1.7 this bug is avoided because defining the model in two different scopes raises a RuntimeError with a message along the lines of "RuntimeError: Conflicting 'class' models in application 'test_bug'".

See attached example.

Attachments (1)

django_test_scope_bug.zip (5.5 KB ) - added by Nicholas A Lourie 10 years ago.
a minimal example of the bug in question.

Download all attachments as: .zip

Change History (5)

by Nicholas A Lourie, 10 years ago

Attachment: django_test_scope_bug.zip added

a minimal example of the bug in question.

comment:1 by Nicholas A Lourie, 10 years ago

Cc: developer.nick@… added
Easy pickings: set

comment:2 by Nicholas A Lourie, 10 years ago

Added as easy pickings because I'm guessing that you could just raise the same runtime error that is raised in Django 1.7, though I'm unfamiliar with how that error is raised.

comment:3 by Aymeric Augustin, 10 years ago

Resolution: fixed
Status: newclosed

Indeed the bug was fixed in Django 1.7 by the app-loading refactor -- which I didn't find particularly easy.

That refactor cannot be backported to Django 1.6 because it introduces several backwards incompatibilities.

The specific problem you mention cannot be fixed in Django 1.6 because some apps (eg. django-oscar) rely on the second definition of a model being silently ignored.

comment:4 by Carl Meyer, 10 years ago

Just to clarify for anyone who finds this in future: the issue has nothing to do with scoping in the testing framework specifically, it's about how Django model registration works. (That is, model classes are automatically globally registered, keyed by app-label and class name).

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