From b286dc261ff94c15ba365cd91f8d13e4bf79c544 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Fri, 31 Mar 2023 11:02:07 -0400 Subject: [PATCH 1/2] Add data and assertions to migration tests --- sortedm2m_tests/test_migrations.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sortedm2m_tests/test_migrations.py b/sortedm2m_tests/test_migrations.py index 82de930..2c5ce53 100644 --- a/sortedm2m_tests/test_migrations.py +++ b/sortedm2m_tests/test_migrations.py @@ -45,6 +45,8 @@ def test_alter_field_m2m(self): def test_alter_field_m2m_to_sorted(self): project_state = self.set_up_test_model("test_alflmm", second_model=True) + Pony = project_state.apps.get_model("test_alflmm", "Pony") + Pony.objects.create(weight=2.5) project_state = self.apply_operations( "test_alflmm", @@ -59,6 +61,12 @@ def test_alter_field_m2m_to_sorted(self): ) Pony = project_state.apps.get_model("test_alflmm", "Pony") self.assertIsInstance(Pony._meta.get_field("stables"), models.ManyToManyField) + pony = Pony.objects.first() + Stable = project_state.apps.get_model("test_alflmm", "Stable") + pony.stables.add( + Stable.objects.create(), + Stable.objects.create() + ) project_state = self.apply_operations( "test_alflmm", @@ -74,6 +82,8 @@ def test_alter_field_m2m_to_sorted(self): ], ) Pony = project_state.apps.get_model("test_alflmm", "Pony") + pony = Pony.objects.first() + assert len(set(pony.stables.all())) == 2 self.assertIsInstance(Pony._meta.get_field("stables"), SortedManyToManyField) def test_alter_field_sortedm2m_to_m2m(self): From b06d4aa9e2e0ce8f9ceda4c17b43d4a1e9bd417e Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 12 Apr 2023 21:36:39 -0400 Subject: [PATCH 2/2] Add data in the migration tests --- sortedm2m_tests/test_migrations.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sortedm2m_tests/test_migrations.py b/sortedm2m_tests/test_migrations.py index 2c5ce53..c635d7f 100644 --- a/sortedm2m_tests/test_migrations.py +++ b/sortedm2m_tests/test_migrations.py @@ -46,7 +46,6 @@ def test_alter_field_m2m(self): def test_alter_field_m2m_to_sorted(self): project_state = self.set_up_test_model("test_alflmm", second_model=True) Pony = project_state.apps.get_model("test_alflmm", "Pony") - Pony.objects.create(weight=2.5) project_state = self.apply_operations( "test_alflmm", @@ -61,7 +60,9 @@ def test_alter_field_m2m_to_sorted(self): ) Pony = project_state.apps.get_model("test_alflmm", "Pony") self.assertIsInstance(Pony._meta.get_field("stables"), models.ManyToManyField) - pony = Pony.objects.first() + + # Create data to check after migration + pony = Pony.objects.create(weight=2.5) Stable = project_state.apps.get_model("test_alflmm", "Stable") pony.stables.add( Stable.objects.create(), @@ -103,6 +104,15 @@ def test_alter_field_sortedm2m_to_m2m(self): Pony = project_state.apps.get_model("test_alflmm", "Pony") self.assertIsInstance(Pony._meta.get_field("stables"), SortedManyToManyField) + # Create data to check after migration + pony = Pony.objects.create(weight=2.5) + Stable = project_state.apps.get_model("test_alflmm", "Stable") + pony.stables.add( + Stable.objects.create(), + Stable.objects.create() + ) + stable_ids = [stable.id for stable in pony.stables.all()] + project_state = self.apply_operations( "test_alflmm", project_state, @@ -119,6 +129,10 @@ def test_alter_field_sortedm2m_to_m2m(self): Pony = project_state.apps.get_model("test_alflmm", "Pony") self.assertIsInstance(Pony._meta.get_field("stables"), models.ManyToManyField) + # Check that data is still there and ordered + pony = Pony.objects.first() + assert [stable.id for stable in pony.stables.all()] == stable_ids + def test_unapply_alter_field_m2m_to_sortedm2m(self): project_state = self.set_up_test_model("test_alflmm", second_model=True)