diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index eda356f..cb81af2 100644
a
|
b
|
class OperationTests(MigrationTestBase):
|
14 | 14 | both forwards and backwards. |
15 | 15 | """ |
16 | 16 | |
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): |
18 | 18 | """ |
19 | 19 | Creates a test model state and database table. |
20 | 20 | """ |
… |
… |
class OperationTests(MigrationTestBase):
|
38 | 38 | ], |
39 | 39 | )] |
40 | 40 | 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 | )) |
42 | 47 | if related_model: |
43 | 48 | operations.append(migrations.CreateModel( |
44 | 49 | "Rider", |
… |
… |
class OperationTests(MigrationTestBase):
|
47 | 52 | ("pony", models.ForeignKey("Pony")), |
48 | 53 | ], |
49 | 54 | )) |
| 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 | )) |
50 | 69 | project_state = ProjectState() |
51 | 70 | for operation in operations: |
52 | 71 | operation.state_forwards(app_label, project_state) |
… |
… |
class OperationTests(MigrationTestBase):
|
476 | 495 | Tests the RunPython operation |
477 | 496 | """ |
478 | 497 | |
479 | | project_state = self.set_up_test_model("test_runpython") |
| 498 | project_state = self.set_up_test_model("test_runpython", mti_model=True) |
| 499 | |
480 | 500 | # Create the operation |
481 | 501 | operation = migrations.RunPython( |
482 | 502 | """ |
… |
… |
class OperationTests(MigrationTestBase):
|
497 | 517 | # And test reversal fails |
498 | 518 | with self.assertRaises(NotImplementedError): |
499 | 519 | operation.database_backwards("test_runpython", None, new_state, project_state) |
500 | | # Now test we can do it with a callable |
501 | 520 | |
502 | | def inner_method(models, schema_editor): |
| 521 | def create_ponies(models, schema_editor): |
503 | 522 | 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) |
507 | 530 | with connection.schema_editor() as editor: |
508 | 531 | operation.database_forwards("test_runpython", editor, project_state, new_state) |
509 | 532 | self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 4) |
510 | 533 | |
| 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) |
511 | 546 | |
512 | 547 | class MigrateNothingRouter(object): |
513 | 548 | """ |