| | 65 | author_with_biography_non_blank = ModelState("testapp", "Author", [ |
| | 66 | ("id", models.AutoField(primary_key=True)), |
| | 67 | ("name", models.CharField()), |
| | 68 | ("biography", models.TextField()), |
| | 69 | ]) |
| | 70 | author_with_biography_blank = ModelState("testapp", "Author", [ |
| | 71 | ("id", models.AutoField(primary_key=True)), |
| | 72 | ("name", models.CharField(blank=True)), |
| | 73 | ("biography", models.TextField(blank=True)), |
| | 74 | ]) |
| | 1695 | |
| | 1696 | def test_add_blank_textfield_and_charfield(self): |
| | 1697 | """ |
| | 1698 | #23405 - Adding a NOT NULL and blank `CharField` or `TextField` |
| | 1699 | without default should not prompt for a default. |
| | 1700 | """ |
| | 1701 | class CustomQuestioner(MigrationQuestioner): |
| | 1702 | def ask_not_null_addition(self, field_name, model_name): |
| | 1703 | raise Exception("Should not have prompted for not null addition") |
| | 1704 | |
| | 1705 | before = self.make_project_state([self.author_empty]) |
| | 1706 | after = self.make_project_state([self.author_with_biography_blank]) |
| | 1707 | autodetector = MigrationAutodetector(before, after, CustomQuestioner()) |
| | 1708 | changes = autodetector._detect_changes() |
| | 1709 | self.assertNumberMigrations(changes, 'testapp', 1) |
| | 1710 | self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"]) |
| | 1711 | self.assertOperationAttributes(changes, 'testapp', 0, 0) |
| | 1712 | |
| | 1713 | def test_add_non_blank_textfield_and_charfield(self): |
| | 1714 | """ |
| | 1715 | #23405 - Adding a NOT NULL and non-blank `CharField` or `TextField` |
| | 1716 | without default should prompt for a default. |
| | 1717 | """ |
| | 1718 | class CustomQuestioner(MigrationQuestioner): |
| | 1719 | def __init__(self): |
| | 1720 | super(CustomQuestioner, self).__init__() |
| | 1721 | self.ask_method_call_count = 0 |
| | 1722 | |
| | 1723 | def ask_not_null_addition(self, field_name, model_name): |
| | 1724 | self.ask_method_call_count += 1 |
| | 1725 | |
| | 1726 | before = self.make_project_state([self.author_empty]) |
| | 1727 | after = self.make_project_state([self.author_with_biography_non_blank]) |
| | 1728 | questioner_instance = CustomQuestioner() |
| | 1729 | autodetector = MigrationAutodetector(before, after, questioner_instance) |
| | 1730 | changes = autodetector._detect_changes() |
| | 1731 | # need to check for questioner call count |
| | 1732 | self.assertEqual(questioner_instance.ask_method_call_count, 2) |
| | 1733 | self.assertNumberMigrations(changes, 'testapp', 1) |
| | 1734 | self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"]) |
| | 1735 | self.assertOperationAttributes(changes, 'testapp', 0, 0) |