Ticket #25173: issue25173-reproduction-testcase.diff

File issue25173-reproduction-testcase.diff, 1.6 KB (added by Baptiste Mispelon, 9 years ago)

Reproduction testcase

  • new file tests/issue25173/models.py

    diff --git a/tests/issue25173/__init__.py b/tests/issue25173/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/issue25173/models.py b/tests/issue25173/models.py
    new file mode 100644
    index 0000000..b1d5ee3
    - +  
     1from django.db import models
     2
     3
     4class Foo(models.Model):
     5    def __str__(self):
     6        return str(self.pk)
     7
     8
     9class Bar(models.Model):
     10    foo = models.ForeignKey(Foo)
     11
     12    def __str__(self):
     13        return str(self.pk)
     14
     15
     16class Baz(Bar):
     17    def __str__(self):
     18        return str(self.pk)
  • new file tests/issue25173/tests.py

    diff --git a/tests/issue25173/tests.py b/tests/issue25173/tests.py
    new file mode 100644
    index 0000000..6dcb30a
    - +  
     1from django.test import TestCase
     2
     3
     4from .models import Foo, Bar, Baz
     5
     6
     7class ReproductionTestCase(TestCase):
     8    @classmethod
     9    def setUpTestData(cls):
     10        foo = Foo.objects.create()
     11        baz = Baz.objects.create(foo=foo)
     12
     13    def test_workaround(self):
     14        # This one works
     15        with self.assertNumQueries(1):
     16            bar = Bar.objects.select_related('baz', 'foo').first()
     17            baz = bar.baz
     18
     19        with self.assertNumQueries(0):
     20            self.assertEqual(repr(baz.bar_ptr.foo), '<Foo: 1>')
     21
     22    def test_issue(self):
     23        # This one doesn't
     24        with self.assertNumQueries(1):
     25            bar = Bar.objects.select_related('baz', 'foo').first()
     26            baz = bar.baz
     27
     28        with self.assertNumQueries(0):
     29            self.assertEqual(repr(baz.foo), '<Foo: 1>')
Back to Top