|
2 | 2 |
|
3 | 3 | from django.core.exceptions import ValidationError |
4 | 4 | from django.db import connection |
| 5 | +from django.db.migrations.autodetector import MigrationAutodetector |
| 6 | +from django.db.migrations.loader import MigrationLoader |
| 7 | +from django.db.migrations.questioner import NonInteractiveMigrationQuestioner |
5 | 8 | from django.test import TestCase |
6 | 9 |
|
7 | 10 | from ..models import Book, OrganizationRadiusSettings, Project, Shelf |
@@ -180,3 +183,57 @@ def test_fallback_decimal_field(self): |
180 | 183 | book.save(update_fields=["price"]) |
181 | 184 | book.refresh_from_db(fields=["price"]) |
182 | 185 | self.assertEqual(book.price, 56) |
| 186 | + |
| 187 | + def test_fallback_field_deconstruct(self): |
| 188 | + with self.subTest("FallbackBooleanChoiceField"): |
| 189 | + field = OrganizationRadiusSettings._meta.get_field("is_active") |
| 190 | + name, path, args, kwargs = field.deconstruct() |
| 191 | + self.assertNotIn("fallback", kwargs) |
| 192 | + with self.subTest("FallbackCharField"): |
| 193 | + field = OrganizationRadiusSettings._meta.get_field("greeting_text") |
| 194 | + name, path, args, kwargs = field.deconstruct() |
| 195 | + self.assertNotIn("fallback", kwargs) |
| 196 | + with self.subTest("FallbackDecimalField"): |
| 197 | + field = Book._meta.get_field("price") |
| 198 | + name, path, args, kwargs = field.deconstruct() |
| 199 | + self.assertNotIn("fallback", kwargs) |
| 200 | + with self.subTest("FallbackPositiveIntegerField"): |
| 201 | + field = Shelf._meta.get_field("books_count") |
| 202 | + name, path, args, kwargs = field.deconstruct() |
| 203 | + self.assertNotIn("fallback", kwargs) |
| 204 | + with self.subTest("Plain field without fallback"): |
| 205 | + field = Shelf._meta.get_field("name") |
| 206 | + name, path, args, kwargs = field.deconstruct() |
| 207 | + self.assertNotIn("fallback", kwargs) |
| 208 | + |
| 209 | + def test_fallback_field_no_migration_on_fallback_change(self): |
| 210 | + loader = MigrationLoader(None, ignore_no_migrations=True) |
| 211 | + current_state = loader.project_state() |
| 212 | + recorded_state = current_state.clone() |
| 213 | + |
| 214 | + new_fallback_by_field = { |
| 215 | + "is_active": True, |
| 216 | + "price": 99.0, |
| 217 | + "books_count": 999, |
| 218 | + } |
| 219 | + field_specs = [ |
| 220 | + ("test_project", "organizationradiussettings", "is_active"), |
| 221 | + ("test_project", "book", "price"), |
| 222 | + ("test_project", "shelf", "books_count"), |
| 223 | + ] |
| 224 | + for app_label, model_name, field_name in field_specs: |
| 225 | + live_field = current_state.models[(app_label, model_name)].fields[ |
| 226 | + field_name |
| 227 | + ] |
| 228 | + name, path, orig_args, orig_kwargs = live_field.deconstruct() |
| 229 | + orig_kwargs["fallback"] = new_fallback_by_field[field_name] |
| 230 | + recorded_state.models[(app_label, model_name)].fields[field_name] = ( |
| 231 | + live_field.__class__(*orig_args, **orig_kwargs) |
| 232 | + ) |
| 233 | + |
| 234 | + changes = MigrationAutodetector( |
| 235 | + recorded_state, |
| 236 | + current_state, |
| 237 | + NonInteractiveMigrationQuestioner(), |
| 238 | + ).changes(graph=loader.graph) |
| 239 | + self.assertEqual(changes, {}) |
0 commit comments