diff options
| author | Marten Kenbeek <marten.knbk@gmail.com> | 2015-02-06 14:24:51 +0100 |
|---|---|---|
| committer | Markus Holtermann <info@markusholtermann.eu> | 2015-02-18 19:05:10 +0100 |
| commit | 15dc8d1c9d3697170a2c59ecaa7a2b4ba58f5990 (patch) | |
| tree | 1b17ed7a31ab90aae11bd8082ef214be5fb166db /tests | |
| parent | e9282747a4ba99adc7e0f677b3a897830df51732 (diff) | |
Fixed #24291 - Fixed migration ModelState generation with unused swappable models
Swapped out models don't have a _default_manager unless they have
explicitly defined managers. ModelState.from_model() now accounts for
this case and uses an empty list for managers if no explicit managers
are defined and a model is swapped out.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_state.py | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index ae2712ae04..7069018683 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -4,7 +4,7 @@ from django.db.migrations.operations import DeleteModel, RemoveField from django.db.migrations.state import ( InvalidBasesError, ModelState, ProjectState, get_related_models_recursive, ) -from django.test import SimpleTestCase, TestCase +from django.test import SimpleTestCase, TestCase, override_settings from .models import ( FoodManager, FoodQuerySet, ModelWithCustomBase, NoMigrationFoodManager, @@ -628,6 +628,58 @@ class ModelStateTests(TestCase): with self.assertRaisesMessage(InvalidBasesError, "Cannot resolve bases for [<ModelState: 'app.Model'>]"): project_state.apps + @override_settings(TEST_SWAPPABLE_MODEL='migrations.SomeFakeModel') + def test_create_swappable(self): + """ + Tests making a ProjectState from an Apps with a swappable model + """ + new_apps = Apps(['migrations']) + + class Author(models.Model): + name = models.CharField(max_length=255) + bio = models.TextField() + age = models.IntegerField(blank=True, null=True) + + class Meta: + app_label = 'migrations' + apps = new_apps + swappable = 'TEST_SWAPPABLE_MODEL' + + author_state = ModelState.from_model(Author) + self.assertEqual(author_state.app_label, 'migrations') + self.assertEqual(author_state.name, 'Author') + self.assertEqual([x for x, y in author_state.fields], ['id', 'name', 'bio', 'age']) + self.assertEqual(author_state.fields[1][1].max_length, 255) + self.assertEqual(author_state.fields[2][1].null, False) + self.assertEqual(author_state.fields[3][1].null, True) + self.assertEqual(author_state.options, {'swappable': 'TEST_SWAPPABLE_MODEL'}) + self.assertEqual(author_state.bases, (models.Model, )) + self.assertEqual(author_state.managers, []) + + @override_settings(TEST_SWAPPABLE_MODEL='migrations.SomeFakeModel') + def test_custom_manager_swappable(self): + """ + Tests making a ProjectState from unused models with custom managers + """ + new_apps = Apps(['migrations']) + + class Food(models.Model): + + food_mgr = FoodManager('a', 'b') + food_qs = FoodQuerySet.as_manager() + food_no_mgr = NoMigrationFoodManager('x', 'y') + + class Meta: + app_label = "migrations" + apps = new_apps + swappable = 'TEST_SWAPPABLE_MODEL' + + food_state = ModelState.from_model(Food) + + # The default manager is used in migrations + self.assertEqual([name for name, mgr in food_state.managers], ['food_mgr']) + self.assertEqual(food_state.managers[0][1].args, ('a', 'b', 1, 2)) + class RelatedModelsTests(SimpleTestCase): |
