|
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 |
5 | 6 | from django.test import TestCase |
6 | 7 |
|
7 | 8 | from ..models import Book, OrganizationRadiusSettings, Project, Shelf |
@@ -180,3 +181,69 @@ def test_fallback_decimal_field(self): |
180 | 181 | book.save(update_fields=["price"]) |
181 | 182 | book.refresh_from_db(fields=["price"]) |
182 | 183 | self.assertEqual(book.price, 56) |
| 184 | + |
| 185 | + def test_fallback_field_deconstruct(self): |
| 186 | + with self.subTest("FallbackBooleanChoiceField"): |
| 187 | + field = OrganizationRadiusSettings._meta.get_field("is_active") |
| 188 | + name, path, args, kwargs = field.deconstruct() |
| 189 | + self.assertNotIn("fallback", kwargs) |
| 190 | + with self.subTest("FallbackCharField"): |
| 191 | + field = OrganizationRadiusSettings._meta.get_field("greeting_text") |
| 192 | + name, path, args, kwargs = field.deconstruct() |
| 193 | + self.assertNotIn("fallback", kwargs) |
| 194 | + with self.subTest("FallbackDecimalField"): |
| 195 | + field = Book._meta.get_field("price") |
| 196 | + name, path, args, kwargs = field.deconstruct() |
| 197 | + self.assertNotIn("fallback", kwargs) |
| 198 | + with self.subTest("FallbackPositiveIntegerField"): |
| 199 | + field = Shelf._meta.get_field("books_count") |
| 200 | + name, path, args, kwargs = field.deconstruct() |
| 201 | + self.assertNotIn("fallback", kwargs) |
| 202 | + with self.subTest("Plain field without fallback"): |
| 203 | + field = Shelf._meta.get_field("name") |
| 204 | + name, path, args, kwargs = field.deconstruct() |
| 205 | + self.assertNotIn("fallback", kwargs) |
| 206 | + |
| 207 | + def test_fallback_field_no_migration_on_fallback_change(self): |
| 208 | + import importlib |
| 209 | + |
| 210 | + from django.db.migrations.autodetector import MigrationAutodetector |
| 211 | + from django.db.migrations.loader import MigrationLoader |
| 212 | + from django.db.migrations.questioner import NonInteractiveMigrationQuestioner |
| 213 | + |
| 214 | + loader = MigrationLoader(None, ignore_no_migrations=True) |
| 215 | + current_state = loader.project_state() |
| 216 | + recorded_state = current_state.clone() |
| 217 | + |
| 218 | + new_fallback_by_field = { |
| 219 | + "is_active": True, |
| 220 | + "price": 99.0, |
| 221 | + "books_count": 999, |
| 222 | + } |
| 223 | + field_specs = [ |
| 224 | + ("test_project", "organizationradiussettings", "is_active"), |
| 225 | + ("test_project", "book", "price"), |
| 226 | + ("test_project", "shelf", "books_count"), |
| 227 | + ] |
| 228 | + for app_label, model_name, field_name in field_specs: |
| 229 | + live_field = current_state.models[(app_label, model_name)].fields[ |
| 230 | + field_name |
| 231 | + ] |
| 232 | + _, path, orig_args, orig_kwargs = live_field.deconstruct() |
| 233 | + orig_kwargs["fallback"] = new_fallback_by_field[field_name] |
| 234 | + module_path, class_name = path.rsplit(".", 1) |
| 235 | + cls = getattr(importlib.import_module(module_path), class_name) |
| 236 | + recorded_state.models[(app_label, model_name)].fields[field_name] = cls( |
| 237 | + *orig_args, **orig_kwargs |
| 238 | + ) |
| 239 | + |
| 240 | + changes = MigrationAutodetector( |
| 241 | + recorded_state, |
| 242 | + current_state, |
| 243 | + NonInteractiveMigrationQuestioner(), |
| 244 | + ).changes(graph=loader.graph) |
| 245 | + self.assertEqual( |
| 246 | + changes, |
| 247 | + {}, |
| 248 | + msg="Changing the fallback value should not generate migrations", |
| 249 | + ) |
0 commit comments