diff --git a/InvenTree/build/migrations/0032_auto_20211014_0632.py b/InvenTree/build/migrations/0032_auto_20211014_0632.py index 6c84c24526..3dac2b30c6 100644 --- a/InvenTree/build/migrations/0032_auto_20211014_0632.py +++ b/InvenTree/build/migrations/0032_auto_20211014_0632.py @@ -10,12 +10,8 @@ def build_refs(apps, schema_editor): Rebuild the integer "reference fields" for existing Build objects """ - print("\n - Rebuilding reference field for BuildOrder model...") - BuildOrder = apps.get_model('build', 'build') - n = BuildOrder.objects.count() - for build in BuildOrder.objects.all(): ref = 0 @@ -31,9 +27,6 @@ def build_refs(apps, schema_editor): build.reference_int = ref build.save() - print(f" - Updated {n} BuildOrder objects") - print(f" - COMPLETE! -") - def unbuild_refs(apps, schema_editor): """ Provided only for reverse migration compatibility diff --git a/InvenTree/build/test_build.py b/InvenTree/build/test_build.py index a0874d0979..df6253362e 100644 --- a/InvenTree/build/test_build.py +++ b/InvenTree/build/test_build.py @@ -118,6 +118,26 @@ class BuildTest(TestCase): self.stock_3_1 = StockItem.objects.create(part=self.sub_part_3, quantity=1000) + def test_ref_int(self): + """ + Test the "integer reference" field used for natural sorting + """ + + for ii in range(10): + build = Build( + reference=f"{ii}_abcde", + quantity=1, + part=self.assembly, + title="Making some parts" + ) + + self.assertEqual(build.reference_int, 0) + + build.save() + + # After saving, the integer reference should have been updated + self.assertEqual(build.reference_int, ii) + def test_init(self): # Perform some basic tests before we start the ball rolling diff --git a/InvenTree/order/migrations/0052_auto_20211014_0631.py b/InvenTree/order/migrations/0052_auto_20211014_0631.py index 91ae28aadc..b400437d20 100644 --- a/InvenTree/order/migrations/0052_auto_20211014_0631.py +++ b/InvenTree/order/migrations/0052_auto_20211014_0631.py @@ -9,12 +9,8 @@ def build_refs(apps, schema_editor): Rebuild the integer "reference fields" for existing Build objects """ - print("\n - Rebuilding reference field for PurchaseOrder model...") - PurchaseOrder = apps.get_model('order', 'purchaseorder') - n = PurchaseOrder.objects.count() - for order in PurchaseOrder.objects.all(): ref = 0 @@ -30,13 +26,8 @@ def build_refs(apps, schema_editor): order.reference_int = ref order.save() - print(f" - Updated {n} PurchaseOrder objects") - print("\n - Rebuilding reference field for SalesOrder model...") - SalesOrder = apps.get_model('order', 'salesorder') - n = SalesOrder.objects.count() - for order in SalesOrder.objects.all(): ref = 0 @@ -52,9 +43,6 @@ def build_refs(apps, schema_editor): order.reference_int = ref order.save() - print(f" - Updated {n} SalesOrder objects") - print(f" - COMPLETE! -") - def unbuild_refs(apps, schema_editor): """ diff --git a/InvenTree/order/test_migrations.py b/InvenTree/order/test_migrations.py new file mode 100644 index 0000000000..b7db1f1b70 --- /dev/null +++ b/InvenTree/order/test_migrations.py @@ -0,0 +1,59 @@ +""" +Unit tests for the 'order' model data migrations +""" + +from django_test_migrations.contrib.unittest_case import MigratorTestCase + +from InvenTree import helpers + + +class TestForwardMigrations(MigratorTestCase): + """ + Test entire schema migration + """ + + migrate_from = ('order', helpers.getOldestMigrationFile('order')) + migrate_to = ('order', helpers.getNewestMigrationFile('order')) + + def prepare(self): + """ + Create initial data set + """ + + # Create a purchase order from a supplier + Company = self.old_state.apps.get_model('company', 'company') + + supplier = Company.objects.create( + name='Supplier A', + description='A great supplier!', + is_supplier=True + ) + + PurchaseOrder = self.old_state.apps.get_model('order', 'purchaseorder') + + # Create some orders + for ii in range(10): + + order = PurchaseOrder.objects.create( + supplier=supplier, + reference=f"{ii}-abcde", + description="Just a test order" + ) + + # Initially, the 'reference_int' field is unavailable + with self.assertRaises(AttributeError): + print(order.reference_int) + + def test_ref_field(self): + """ + Test that the 'reference_int' field has been created and is filled out correctly + """ + + PurchaseOrder = self.new_state.apps.get_model('order', 'purchaseorder') + + for ii in range(10): + + order = PurchaseOrder.objects.get(reference=f"{ii}-abcde") + + # The integer reference field must have been correctly updated + self.assertEqual(order.reference_int, ii)