2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-27 19:16:44 +00:00

SalesOrder migration unit test (#8814) (#8961)

* Unit test for SalesOrder data migration

* make field checks more stable

* Adjust migration  strategy

* Fix for data migration

* Simplify login test for playwright

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
(cherry picked from commit a13f5681a1a65eec9a59892cd53fc6d929703996)

Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
This commit is contained in:
github-actions[bot] 2025-01-27 22:54:12 +11:00 committed by GitHub
parent 8baafed49f
commit c89fe44fea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 74 additions and 26 deletions

View File

@ -100,7 +100,9 @@ class InvenTreeCustomStatusModelField(models.PositiveIntegerField):
"""Add the _custom_key field to the model."""
cls._meta.supports_custom_status = True
if not hasattr(self, '_custom_key_field'):
if not hasattr(self, '_custom_key_field') and not hasattr(
cls, f'{name}_custom_key'
):
self.add_field(cls, name)
super().contribute_to_class(cls, name)

View File

@ -1,6 +1,6 @@
# Generated by Django 4.2.16 on 2024-11-28 04:31
from django.db import migrations
from django.db import migrations, connection
def update_shipment_date(apps, schema_editor):
@ -18,9 +18,15 @@ def update_shipment_date(apps, schema_editor):
shipment_date__isnull=True
)
updated_orders = 0
update_count = 0
cursor = connection.cursor()
for order in orders:
# Check that the shipment date is actually null here
assert order.shipment_date is None, f"SalesOrder {order.pk} has non-null shipment_date"
# Find the latest shipment date for any associated allocations
shipments = order.shipments.filter(shipment_date__isnull=False)
latest_shipment = shipments.order_by('-shipment_date').first()
@ -29,13 +35,21 @@ def update_shipment_date(apps, schema_editor):
continue
# Update the order with the new shipment date
order.shipment_date = latest_shipment.shipment_date
order.save()
shipment_date = latest_shipment.shipment_date
updated_orders += 1
if updated_orders > 0:
print(f"Updated {updated_orders} SalesOrder objects with missing shipment_date")
# Raw SQL to prevent some weird migration "order of operations" issues
# Reference: https://github.com/inventree/InvenTree/pull/8814
query = f"UPDATE order_salesorder SET shipment_date = '{shipment_date}' WHERE id = {order.pk}"
cursor.execute(query)
# Fetch the updated object, check that the shipment date has been updated
order.refresh_from_db()
assert order.shipment_date is not None, f"SalesOrder {order.pk} still has missing shipment_date"
update_count += 1
if update_count > 0:
print(f"Updated {update_count} SalesOrder shipment dates")
class Migration(migrations.Migration):

View File

@ -198,3 +198,52 @@ class TestAdditionalLineMigration(MigratorTestCase):
# so = SalesOrder.objects.get(reference=f"{ii}-xyz")
# self.assertEqual(so.extra_lines, 1)
# self.assertEqual(so.lines.count(), 1)
class TestShipmentDateMigration(MigratorTestCase):
"""Test data migration which fixes empty 'shipment date' on SalesOrder model.
Ref: 0105_auto_20241128_0431.py
"""
migrate_from = ('order', '0100_remove_returnorderattachment_order_and_more')
migrate_to = ('order', '0105_auto_20241128_0431')
def prepare(self):
"""Create initial SalesOrder dataset."""
Company = self.old_state.apps.get_model('company', 'company')
SalesOrder = self.old_state.apps.get_model('order', 'salesorder')
SalesOrderShipment = self.old_state.apps.get_model(
'order', 'salesordershipment'
)
# Create a customer
customer = Company.objects.create(
name='Customer A',
description='A great customer!',
is_customer=True,
is_supplier=False,
)
# Create a SalesOrder (Completed, but missing shipment date)
order = SalesOrder.objects.create(
customer=customer,
reference='SO-999',
description='A test sales order',
shipment_date=None,
status=SalesOrderStatus.COMPLETE,
)
# Add a shipment
SalesOrderShipment.objects.create(order=order, shipment_date='2024-11-28')
self.assertEqual(order.shipments.count(), 1)
self.assertIsNone(order.shipment_date)
def test_migration(self):
"""Test that the migration has correctly updated the SalesOrder objects."""
SalesOrder = self.new_state.apps.get_model('order', 'salesorder')
order = SalesOrder.objects.get(reference='SO-999')
self.assertIsNotNone(order.shipment_date)
self.assertEqual(order.shipment_date.isoformat(), '2024-11-28')

View File

@ -8,23 +8,6 @@ test('Login - Basic Test', async ({ page }) => {
// Check that the username is provided
await page.getByText(user.username);
await expect(page).toHaveTitle(/^InvenTree/);
// Go to the dashboard
await page.goto(baseUrl);
await page.waitForURL('**/platform');
await page.getByText('InvenTree Demo Server -').waitFor();
// Check that the username is provided
await page.getByText(user.username);
await expect(page).toHaveTitle(/^InvenTree/);
// Go to the dashboard
await page.goto(baseUrl);
await page.waitForURL('**/platform');
// Logout (via menu)
await page.getByRole('button', { name: 'Ally Access' }).click();
await page.getByRole('menuitem', { name: 'Logout' }).click();