|
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,69 @@ 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 | + test_cases = [ |
| 189 | + ("FallbackBooleanChoiceField", OrganizationRadiusSettings, "is_active"), |
| 190 | + ("FallbackCharField", OrganizationRadiusSettings, "greeting_text"), |
| 191 | + ("FallbackDecimalField", Book, "price"), |
| 192 | + ("FallbackPositiveIntegerField", Shelf, "books_count"), |
| 193 | + ("Plain field without fallback", Shelf, "name"), |
| 194 | + ] |
| 195 | + for field_type, model, field_name in test_cases: |
| 196 | + with self.subTest(field_type): |
| 197 | + field = model._meta.get_field(field_name) |
| 198 | + name, path, args, kwargs = field.deconstruct() |
| 199 | + self.assertNotIn("fallback", kwargs) |
| 200 | + |
| 201 | + def test_fallback_field_no_migration_on_fallback_change(self): |
| 202 | + loader = MigrationLoader(None, ignore_no_migrations=True) |
| 203 | + current_state = loader.project_state() |
| 204 | + recorded_state = current_state.clone() |
| 205 | + |
| 206 | + new_fallback_by_field = { |
| 207 | + "is_active": True, |
| 208 | + "price": 99.0, |
| 209 | + "books_count": 999, |
| 210 | + } |
| 211 | + field_specs = [ |
| 212 | + ("test_project", "organizationradiussettings", "is_active"), |
| 213 | + ("test_project", "book", "price"), |
| 214 | + ("test_project", "shelf", "books_count"), |
| 215 | + ] |
| 216 | + for app_label, model_name, field_name in field_specs: |
| 217 | + live_field = current_state.models[(app_label, model_name)].fields[ |
| 218 | + field_name |
| 219 | + ] |
| 220 | + name, path, orig_args, orig_kwargs = live_field.deconstruct() |
| 221 | + orig_kwargs["fallback"] = new_fallback_by_field[field_name] |
| 222 | + recorded_state.models[(app_label, model_name)].fields[field_name] = ( |
| 223 | + live_field.__class__(*orig_args, **orig_kwargs) |
| 224 | + ) |
| 225 | + |
| 226 | + changes = MigrationAutodetector( |
| 227 | + recorded_state, |
| 228 | + current_state, |
| 229 | + NonInteractiveMigrationQuestioner(), |
| 230 | + ).changes(graph=loader.graph) |
| 231 | + self.assertEqual(changes, {}) |
| 232 | + |
| 233 | + def test_fallback_field_clone_preserves_fallback(self): |
| 234 | + test_cases = [ |
| 235 | + ("FallbackBooleanChoiceField", OrganizationRadiusSettings, "is_active"), |
| 236 | + ( |
| 237 | + "FallbackCharChoiceField", |
| 238 | + OrganizationRadiusSettings, |
| 239 | + "is_first_name_required", |
| 240 | + ), |
| 241 | + ("FallbackDecimalField", Book, "price"), |
| 242 | + ("FallbackPositiveIntegerField", Shelf, "books_count"), |
| 243 | + ("FallbackTextField", OrganizationRadiusSettings, "extra_config"), |
| 244 | + ("FallbackURLField", OrganizationRadiusSettings, "password_reset_url"), |
| 245 | + ("FallbackCharField", OrganizationRadiusSettings, "greeting_text"), |
| 246 | + ] |
| 247 | + for field_type, model, field_name in test_cases: |
| 248 | + with self.subTest(field_type): |
| 249 | + field = model._meta.get_field(field_name) |
| 250 | + cloned = field.clone() |
| 251 | + self.assertEqual(cloned.fallback, field.fallback) |
0 commit comments