mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add some unit tests
- Saving a model automatically updates the reference_int field - Data migrations are correctly applied
This commit is contained in:
parent
d0f60766e0
commit
2c9bbb051a
@ -10,12 +10,8 @@ def build_refs(apps, schema_editor):
|
|||||||
Rebuild the integer "reference fields" for existing Build objects
|
Rebuild the integer "reference fields" for existing Build objects
|
||||||
"""
|
"""
|
||||||
|
|
||||||
print("\n - Rebuilding reference field for BuildOrder model...")
|
|
||||||
|
|
||||||
BuildOrder = apps.get_model('build', 'build')
|
BuildOrder = apps.get_model('build', 'build')
|
||||||
|
|
||||||
n = BuildOrder.objects.count()
|
|
||||||
|
|
||||||
for build in BuildOrder.objects.all():
|
for build in BuildOrder.objects.all():
|
||||||
|
|
||||||
ref = 0
|
ref = 0
|
||||||
@ -31,9 +27,6 @@ def build_refs(apps, schema_editor):
|
|||||||
build.reference_int = ref
|
build.reference_int = ref
|
||||||
build.save()
|
build.save()
|
||||||
|
|
||||||
print(f" - Updated {n} BuildOrder objects")
|
|
||||||
print(f" - COMPLETE! -")
|
|
||||||
|
|
||||||
def unbuild_refs(apps, schema_editor):
|
def unbuild_refs(apps, schema_editor):
|
||||||
"""
|
"""
|
||||||
Provided only for reverse migration compatibility
|
Provided only for reverse migration compatibility
|
||||||
|
@ -118,6 +118,26 @@ class BuildTest(TestCase):
|
|||||||
|
|
||||||
self.stock_3_1 = StockItem.objects.create(part=self.sub_part_3, quantity=1000)
|
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):
|
def test_init(self):
|
||||||
# Perform some basic tests before we start the ball rolling
|
# Perform some basic tests before we start the ball rolling
|
||||||
|
|
||||||
|
@ -9,12 +9,8 @@ def build_refs(apps, schema_editor):
|
|||||||
Rebuild the integer "reference fields" for existing Build objects
|
Rebuild the integer "reference fields" for existing Build objects
|
||||||
"""
|
"""
|
||||||
|
|
||||||
print("\n - Rebuilding reference field for PurchaseOrder model...")
|
|
||||||
|
|
||||||
PurchaseOrder = apps.get_model('order', 'purchaseorder')
|
PurchaseOrder = apps.get_model('order', 'purchaseorder')
|
||||||
|
|
||||||
n = PurchaseOrder.objects.count()
|
|
||||||
|
|
||||||
for order in PurchaseOrder.objects.all():
|
for order in PurchaseOrder.objects.all():
|
||||||
|
|
||||||
ref = 0
|
ref = 0
|
||||||
@ -30,13 +26,8 @@ def build_refs(apps, schema_editor):
|
|||||||
order.reference_int = ref
|
order.reference_int = ref
|
||||||
order.save()
|
order.save()
|
||||||
|
|
||||||
print(f" - Updated {n} PurchaseOrder objects")
|
|
||||||
print("\n - Rebuilding reference field for SalesOrder model...")
|
|
||||||
|
|
||||||
SalesOrder = apps.get_model('order', 'salesorder')
|
SalesOrder = apps.get_model('order', 'salesorder')
|
||||||
|
|
||||||
n = SalesOrder.objects.count()
|
|
||||||
|
|
||||||
for order in SalesOrder.objects.all():
|
for order in SalesOrder.objects.all():
|
||||||
|
|
||||||
ref = 0
|
ref = 0
|
||||||
@ -52,9 +43,6 @@ def build_refs(apps, schema_editor):
|
|||||||
order.reference_int = ref
|
order.reference_int = ref
|
||||||
order.save()
|
order.save()
|
||||||
|
|
||||||
print(f" - Updated {n} SalesOrder objects")
|
|
||||||
print(f" - COMPLETE! -")
|
|
||||||
|
|
||||||
|
|
||||||
def unbuild_refs(apps, schema_editor):
|
def unbuild_refs(apps, schema_editor):
|
||||||
"""
|
"""
|
||||||
|
59
InvenTree/order/test_migrations.py
Normal file
59
InvenTree/order/test_migrations.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user