Refs #9804 -- Fixed test for sequence reset of M2M with inherited through model.

This commit is contained in:
Tim Graham 2018-07-11 16:37:56 -04:00
parent 8a03445885
commit 4d98b9d729
3 changed files with 28 additions and 28 deletions

View File

@ -40,25 +40,6 @@ class Group(models.Model):
return self.name return self.name
# A set of models that use a non-abstract inherited model as the 'through' model.
class A(models.Model):
a_text = models.CharField(max_length=20)
class ThroughBase(models.Model):
a = models.ForeignKey(A, models.CASCADE)
b = models.ForeignKey('B', models.CASCADE)
class Through(ThroughBase):
extra = models.CharField(max_length=20)
class B(models.Model):
b_text = models.CharField(max_length=20)
a_list = models.ManyToManyField(A, through=Through)
# Using to_field on the through model # Using to_field on the through model
class Car(models.Model): class Car(models.Model):
make = models.CharField(max_length=20, unique=True, null=True) make = models.CharField(max_length=20, unique=True, null=True)

View File

@ -4,3 +4,17 @@ from django.db import models
class Person(models.Model): class Person(models.Model):
first_name = models.CharField(max_length=20) first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20) last_name = models.CharField(max_length=20)
# A set of models that use a non-abstract inherited 'through' model.
class ThroughBase(models.Model):
person = models.ForeignKey(Person, models.CASCADE)
b = models.ForeignKey('B', models.CASCADE)
class Through(ThroughBase):
extra = models.CharField(max_length=20)
class B(models.Model):
people = models.ManyToManyField(Person, through=Through)

View File

@ -17,7 +17,7 @@ from django.test.runner import DiscoverRunner
from django.test.testcases import connections_support_transactions from django.test.testcases import connections_support_transactions
from django.test.utils import dependency_ordered from django.test.utils import dependency_ordered
from .models import Person from .models import B, Person, Through
class DependencyOrderingTests(unittest.TestCase): class DependencyOrderingTests(unittest.TestCase):
@ -327,26 +327,31 @@ class SetupDatabasesTests(unittest.TestCase):
) )
@skipUnlessDBFeature('supports_sequence_reset')
class AutoIncrementResetTest(TransactionTestCase): class AutoIncrementResetTest(TransactionTestCase):
""" """
Here we test creating the same model two times in different test methods, Creating the same models in different test methods receive the same PK
and check that both times they get "1" as their PK value. That is, we test values since the sequences are reset before each test method.
that AutoField values start from 1 for each transactional test case.
""" """
available_apps = ['test_runner'] available_apps = ['test_runner']
reset_sequences = True reset_sequences = True
@skipUnlessDBFeature('supports_sequence_reset') def _test(self):
def test_autoincrement_reset1(self): # Regular model
p = Person.objects.create(first_name='Jack', last_name='Smith') p = Person.objects.create(first_name='Jack', last_name='Smith')
self.assertEqual(p.pk, 1) self.assertEqual(p.pk, 1)
# Many-to-many through model
b = B.objects.create()
t = Through.objects.create(person=p, b=b)
self.assertEqual(t.pk, 1)
def test_autoincrement_reset1(self):
self._test()
@skipUnlessDBFeature('supports_sequence_reset')
def test_autoincrement_reset2(self): def test_autoincrement_reset2(self):
p = Person.objects.create(first_name='Jack', last_name='Smith') self._test()
self.assertEqual(p.pk, 1)
class EmptyDefaultDatabaseTest(unittest.TestCase): class EmptyDefaultDatabaseTest(unittest.TestCase):