Ticket #21893: test.diff

File test.diff, 4.1 KB (added by loic84, 19 months ago)
  • tests/migrations/test_operations.py

    diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
    index eda356f..cb81af2 100644
    a b class OperationTests(MigrationTestBase): 
    1414    both forwards and backwards.
    1515    """
    1616
    17     def set_up_test_model(self, app_label, second_model=False, related_model=False):
     17    def set_up_test_model(self, app_label, second_model=False, related_model=False, mti_model=False):
    1818        """
    1919        Creates a test model state and database table.
    2020        """
    class OperationTests(MigrationTestBase): 
    3838            ],
    3939        )]
    4040        if second_model:
    41             operations.append(migrations.CreateModel("Stable", [("id", models.AutoField(primary_key=True))]))
     41            operations.append(migrations.CreateModel(
     42                "Stable",
     43                [
     44                    ("id", models.AutoField(primary_key=True)),
     45                ]
     46            ))
    4247        if related_model:
    4348            operations.append(migrations.CreateModel(
    4449                "Rider",
    class OperationTests(MigrationTestBase): 
    4752                    ("pony", models.ForeignKey("Pony")),
    4853                ],
    4954            ))
     55        if mti_model:
     56            operations.append(migrations.CreateModel(
     57                "ShetlandPony",
     58                [
     59                    ('pony_ptr', models.OneToOneField(
     60                        auto_created=True,
     61                        primary_key=True,
     62                        to_field='id',
     63                        serialize=False,
     64                        to='Pony',
     65                    )),
     66                    ("cuteness", models.IntegerField(default=1)),
     67                ],
     68            ))
    5069        project_state = ProjectState()
    5170        for operation in operations:
    5271            operation.state_forwards(app_label, project_state)
    class OperationTests(MigrationTestBase): 
    476495        Tests the RunPython operation
    477496        """
    478497
    479         project_state = self.set_up_test_model("test_runpython")
     498        project_state = self.set_up_test_model("test_runpython", mti_model=True)
     499
    480500        # Create the operation
    481501        operation = migrations.RunPython(
    482502            """
    class OperationTests(MigrationTestBase): 
    497517        # And test reversal fails
    498518        with self.assertRaises(NotImplementedError):
    499519            operation.database_backwards("test_runpython", None, new_state, project_state)
    500         # Now test we can do it with a callable
    501520
    502         def inner_method(models, schema_editor):
     521        def create_ponies(models, schema_editor):
    503522            Pony = models.get_model("test_runpython", "Pony")
    504             Pony.objects.create(pink=1, weight=3.55)
    505             Pony.objects.create(weight=5)
    506         operation = migrations.RunPython(inner_method)
     523            pony1 = Pony.objects.create(pink=1, weight=3.55)
     524            self.assertIsNot(pony1.pk, None)
     525            pony2 = Pony.objects.create(weight=5)
     526            self.assertIsNot(pony2.pk, None)
     527            self.assertNotEqual(pony1.pk, pony2.pk)
     528
     529        operation = migrations.RunPython(create_ponies)
    507530        with connection.schema_editor() as editor:
    508531            operation.database_forwards("test_runpython", editor, project_state, new_state)
    509532        self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 4)
    510533
     534        def create_shetlandponies(models, schema_editor):
     535            ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
     536            pony1 = ShetlandPony.objects.create()
     537            self.assertIsNot(pony1.pk, None)
     538            pony2 = ShetlandPony.objects.create(weight=5)
     539            self.assertIsNot(pony2.pk, None)
     540            self.assertNotEqual(pony1.pk, pony2.pk)
     541
     542        operation = migrations.RunPython(create_shetlandponies)
     543        with connection.schema_editor() as editor:
     544            operation.database_forwards("test_runpython", editor, project_state, new_state)
     545        self.assertEqual(project_state.render().get_model("test_runpython", "ShetlandPony").objects.count(), 6)
    511546
    512547class MigrateNothingRouter(object):
    513548    """
Back to Top