[Django] #25290: Inconsistency between database and object instances when using setUpTestData with related objects

9 views
Skip to first unread message

Django

unread,
Aug 19, 2015, 2:58:45 AM8/19/15
to django-...@googlegroups.com
#25290: Inconsistency between database and object instances when using
setUpTestData with related objects
---------------------------------------------+------------------------
Reporter: MarkusH | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
Given the following models:

{{{#!python
from django.db import models


class RelObj(models.Model):
def __repr__(self):
return '<RelObj %d at id %x>' % (self.pk, id(self))


class Obj(models.Model):
relobj = models.OneToOneField(RelObj)
name = models.CharField(max_length=10, unique=True)
val = models.IntegerField()

def __repr__(self):
return '<Obj %d at id %x>' % (self.pk, id(self))
}}}

And these tests:

{{{#!python
from django.db.utils import IntegrityError
from django.test import TestCase

from .models import Obj, RelObj


class ModelTests(TestCase):

@classmethod
def setUpTestData(cls):
cls.relobj = RelObj.objects.create()
cls.obj = Obj.objects.create(relobj=cls.relobj, name='foo', val=1)

def test_1(self):
self.assertEqual(self.obj.relobj, self.relobj)
self.assertEqual(self.relobj.obj, self.obj)
self.assertEqual(self.obj.name, 'foo')
self.assertEqual(self.obj.val, 1)

def test_2(self):
with self.assertRaisesRegex(IntegrityError, 'app_obj_name_key'):
Obj.objects.create(relobj=self.relobj, name='foo', val=2)

def test_3(self):
self.assertEqual(self.obj.relobj, self.relobj)
self.assertEqual(self.relobj.obj, self.obj)
self.assertEqual(self.obj.name, 'foo')
self.assertEqual(self.obj.val, 1)
}}}

I get the following traceback on PostgreSQL

{{{#!python
Creating test database for alias 'default'...
..F
======================================================================
FAIL: test_3 (app.tests.ModelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/markus/Coding/django-test-app/app/tests.py", line 26, in
test_3
self.assertEqual(self.relobj.obj, self.obj)
AssertionError: <app.models.Obj object at 0x7f579e2afe10> != <Obj 1 at id
7f579e2afc18>

----------------------------------------------------------------------
Ran 3 tests in 0.004s

FAILED (failures=1)
Destroying test database for alias 'default'...
}}}

The problem seems to be within the `Obj` creation in `test_2` which
assigns `self.relobj` / `cls.relobj` the instance of the newly created
object that couldn't be saved.

I'm on Django 1.9.dev20150818235245.

I know that I could use `refresh_from_db()`, but that seems counter
productive, given the intention of `setUpTestData()`. Using `setUp()`
instead of `setUpTestData()` also works.

--
Ticket URL: <https://code.djangoproject.com/ticket/25290>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 19, 2015, 3:14:41 PM8/19/15
to django-...@googlegroups.com
#25290: Inconsistency between database and object instances when using
setUpTestData with related objects
-----------------------------------+--------------------------------------

Reporter: MarkusH | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by timgraham):

I'm not sure what we could do here besides adding a documentation warning
that you shouldn't modify objects created in `setUpTestData()` in your
test methods. Modifications to in-memory objects from setup work done at
the class level will inevitably persist between test methods.

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:1>

Django

unread,
Aug 20, 2015, 12:51:25 AM8/20/15
to django-...@googlegroups.com
#25290: Inconsistency between database and object instances when using
setUpTestData with related objects
-----------------------------------+--------------------------------------

Reporter: MarkusH | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.8
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by MarkusH):

Yeah, I see that. The "solution" I came up with now is

{{{#!python
def test_2(self):
try:


with self.assertRaisesRegex(IntegrityError, 'app_obj_name_key'):
Obj.objects.create(relobj=self.relobj, name='foo', val=2)

finally:
self.relobj = self.obj
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:2>

Django

unread,
Aug 20, 2015, 5:33:13 PM8/20/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
--------------------------------------+------------------------------------
Reporter: MarkusH | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by timgraham):

* type: Bug => Cleanup/optimization
* component: Testing framework => Documentation
* easy: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:3>

Django

unread,
Oct 10, 2015, 10:51:02 AM10/10/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner:
Type: | Anjaliunni96
Cleanup/optimization | Status: assigned

Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anjaliunni96):

* status: new => assigned
* owner: nobody => Anjaliunni96


--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:4>

Django

unread,
Oct 29, 2015, 5:15:10 PM10/29/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner:
Type: | Anjaliunni96
Cleanup/optimization | Status: assigned
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/5509 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:5>

Django

unread,
Nov 7, 2015, 2:33:43 PM11/7/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner:
Type: | Anjaliunni96
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"81006b9657534a21c3eadb2bc4a87c46db54c099" 81006b9]:
{{{
#!CommitTicketReference repository=""
revision="81006b9657534a21c3eadb2bc4a87c46db54c099"
Fixed #25290 -- Warned against modifying objects created in
setUpTestData() in tests.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:6>

Django

unread,
Nov 7, 2015, 2:36:36 PM11/7/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner:
Type: | Anjaliunni96
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"1eb2ff3ff75b0f08276e0c3e10c99ed111572d8c" 1eb2ff3]:
{{{
#!CommitTicketReference repository=""
revision="1eb2ff3ff75b0f08276e0c3e10c99ed111572d8c"
[1.9.x] Fixed #25290 -- Warned against modifying objects created in
setUpTestData() in tests.

Backport of 81006b9657534a21c3eadb2bc4a87c46db54c099 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:8>

Django

unread,
Nov 7, 2015, 2:36:36 PM11/7/15
to django-...@googlegroups.com
#25290: Warn against modifying objects created in setUpTestData
-------------------------------------+-------------------------------------
Reporter: MarkusH | Owner:
Type: | Anjaliunni96
Cleanup/optimization | Status: closed
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"bd55e8063544e0f7990bab7d9c981a6dc22c1654" bd55e80]:
{{{
#!CommitTicketReference repository=""
revision="bd55e8063544e0f7990bab7d9c981a6dc22c1654"
[1.8.x] Fixed #25290 -- Warned against modifying objects created in
setUpTestData() in tests.

Backport of 81006b9657534a21c3eadb2bc4a87c46db54c099 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25290#comment:7>

Reply all
Reply to author
Forward
0 new messages