Skip to content

Commit c89fe44

Browse files
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 a13f568) Co-authored-by: Oliver <oliver.henry.walters@gmail.com>
1 parent 8baafed commit c89fe44

File tree

4 files changed

+74
-26
lines changed

4 files changed

+74
-26
lines changed

src/backend/InvenTree/generic/states/fields.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def contribute_to_class(self, cls, name):
100100
"""Add the _custom_key field to the model."""
101101
cls._meta.supports_custom_status = True
102102

103-
if not hasattr(self, '_custom_key_field'):
103+
if not hasattr(self, '_custom_key_field') and not hasattr(
104+
cls, f'{name}_custom_key'
105+
):
104106
self.add_field(cls, name)
105107

106108
super().contribute_to_class(cls, name)

src/backend/InvenTree/order/migrations/0105_auto_20241128_0431.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by Django 4.2.16 on 2024-11-28 04:31
22

3-
from django.db import migrations
3+
from django.db import migrations, connection
44

55

66
def update_shipment_date(apps, schema_editor):
@@ -18,9 +18,15 @@ def update_shipment_date(apps, schema_editor):
1818
shipment_date__isnull=True
1919
)
2020

21-
updated_orders = 0
21+
update_count = 0
22+
23+
cursor = connection.cursor()
2224

2325
for order in orders:
26+
27+
# Check that the shipment date is actually null here
28+
assert order.shipment_date is None, f"SalesOrder {order.pk} has non-null shipment_date"
29+
2430
# Find the latest shipment date for any associated allocations
2531
shipments = order.shipments.filter(shipment_date__isnull=False)
2632
latest_shipment = shipments.order_by('-shipment_date').first()
@@ -29,13 +35,21 @@ def update_shipment_date(apps, schema_editor):
2935
continue
3036

3137
# Update the order with the new shipment date
32-
order.shipment_date = latest_shipment.shipment_date
33-
order.save()
38+
shipment_date = latest_shipment.shipment_date
39+
40+
# Raw SQL to prevent some weird migration "order of operations" issues
41+
# Reference: https://github.com/inventree/InvenTree/pull/8814
42+
query = f"UPDATE order_salesorder SET shipment_date = '{shipment_date}' WHERE id = {order.pk}"
43+
cursor.execute(query)
44+
45+
# Fetch the updated object, check that the shipment date has been updated
46+
order.refresh_from_db()
47+
assert order.shipment_date is not None, f"SalesOrder {order.pk} still has missing shipment_date"
48+
49+
update_count += 1
3450

35-
updated_orders += 1
36-
37-
if updated_orders > 0:
38-
print(f"Updated {updated_orders} SalesOrder objects with missing shipment_date")
51+
if update_count > 0:
52+
print(f"Updated {update_count} SalesOrder shipment dates")
3953

4054

4155
class Migration(migrations.Migration):

src/backend/InvenTree/order/test_migrations.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,52 @@ def test_po_migration(self):
198198
# so = SalesOrder.objects.get(reference=f"{ii}-xyz")
199199
# self.assertEqual(so.extra_lines, 1)
200200
# self.assertEqual(so.lines.count(), 1)
201+
202+
203+
class TestShipmentDateMigration(MigratorTestCase):
204+
"""Test data migration which fixes empty 'shipment date' on SalesOrder model.
205+
206+
Ref: 0105_auto_20241128_0431.py
207+
"""
208+
209+
migrate_from = ('order', '0100_remove_returnorderattachment_order_and_more')
210+
migrate_to = ('order', '0105_auto_20241128_0431')
211+
212+
def prepare(self):
213+
"""Create initial SalesOrder dataset."""
214+
Company = self.old_state.apps.get_model('company', 'company')
215+
SalesOrder = self.old_state.apps.get_model('order', 'salesorder')
216+
SalesOrderShipment = self.old_state.apps.get_model(
217+
'order', 'salesordershipment'
218+
)
219+
220+
# Create a customer
221+
customer = Company.objects.create(
222+
name='Customer A',
223+
description='A great customer!',
224+
is_customer=True,
225+
is_supplier=False,
226+
)
227+
228+
# Create a SalesOrder (Completed, but missing shipment date)
229+
order = SalesOrder.objects.create(
230+
customer=customer,
231+
reference='SO-999',
232+
description='A test sales order',
233+
shipment_date=None,
234+
status=SalesOrderStatus.COMPLETE,
235+
)
236+
237+
# Add a shipment
238+
SalesOrderShipment.objects.create(order=order, shipment_date='2024-11-28')
239+
240+
self.assertEqual(order.shipments.count(), 1)
241+
self.assertIsNone(order.shipment_date)
242+
243+
def test_migration(self):
244+
"""Test that the migration has correctly updated the SalesOrder objects."""
245+
SalesOrder = self.new_state.apps.get_model('order', 'salesorder')
246+
247+
order = SalesOrder.objects.get(reference='SO-999')
248+
self.assertIsNotNone(order.shipment_date)
249+
self.assertEqual(order.shipment_date.isoformat(), '2024-11-28')

src/frontend/tests/pui_login.spec.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ test('Login - Basic Test', async ({ page }) => {
88
// Check that the username is provided
99
await page.getByText(user.username);
1010

11-
await expect(page).toHaveTitle(/^InvenTree/);
12-
13-
// Go to the dashboard
14-
await page.goto(baseUrl);
15-
await page.waitForURL('**/platform');
16-
17-
await page.getByText('InvenTree Demo Server -').waitFor();
18-
19-
// Check that the username is provided
20-
await page.getByText(user.username);
21-
22-
await expect(page).toHaveTitle(/^InvenTree/);
23-
24-
// Go to the dashboard
25-
await page.goto(baseUrl);
26-
await page.waitForURL('**/platform');
27-
2811
// Logout (via menu)
2912
await page.getByRole('button', { name: 'Ally Access' }).click();
3013
await page.getByRole('menuitem', { name: 'Logout' }).click();

0 commit comments

Comments
 (0)