| 58 | author_date_of_birth_autonow = ModelState("testapp", "Author", [ |
| 59 | ("id", models.AutoField(primary_key=True)), |
| 60 | ("date_of_birth", models.DateField(auto_now=True)), |
| 61 | ]) |
| 62 | author_datetime_of_birth_autonow = ModelState("testapp", "Author", [ |
| 63 | ("id", models.AutoField(primary_key=True)), |
| 64 | ("date_of_birth", models.DateTimeField(auto_now=True)), |
| 65 | ]) |
| 639 | def test_add_date_field_with_auto_now_not_asking_for_default(self): |
| 640 | class CustomQuestioner(MigrationQuestioner): |
| 641 | def ask_not_null_addition(self, field_name, model_name): |
| 642 | raise Exception("Should not have prompted for not null addition") |
| 643 | |
| 644 | # Make state |
| 645 | before = self.make_project_state([self.author_empty]) |
| 646 | after = self.make_project_state([self.author_date_of_birth_autonow]) |
| 647 | autodetector = MigrationAutodetector(before, after, CustomQuestioner()) |
| 648 | changes = autodetector._detect_changes() |
| 649 | # Right number/type of migrations? |
| 650 | self.assertNumberMigrations(changes, 'testapp', 1) |
| 651 | self.assertOperationTypes(changes, 'testapp', 0, ["AddField"]) |
| 652 | self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True) |
| 653 | |
| 654 | def test_add_datetime_field_with_auto_now_not_asking_for_default(self): |
| 655 | class CustomQuestioner(MigrationQuestioner): |
| 656 | def ask_not_null_addition(self, field_name, model_name): |
| 657 | raise Exception("Should not have prompted for not null addition") |
| 658 | |
| 659 | # Make state |
| 660 | before = self.make_project_state([self.author_empty]) |
| 661 | after = self.make_project_state([self.author_datetime_of_birth_autonow]) |
| 662 | autodetector = MigrationAutodetector(before, after, CustomQuestioner()) |
| 663 | changes = autodetector._detect_changes() |
| 664 | # Right number/type of migrations? |
| 665 | self.assertNumberMigrations(changes, 'testapp', 1) |
| 666 | self.assertOperationTypes(changes, 'testapp', 0, ["AddField"]) |
| 667 | self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True) |
| 668 | |