#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.e5c256ef8addf36dbf0bd45b895ef338%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to