2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-08-06 12:01:41 +00:00

Pricing bug fix (#4422)

* Control when a new PartPricing object can be created

- Prevent this when calling from an on_delete signal
- There is an edge case where deleting a part triggers a series of on_delete signals inside an atomic transaction
- When the new PartPricing object is created,

* Add unit testing:

- Ensure PartPricing gets created when a new StockItem is added
- Part.delete() works without error
- PartPricing instances are deleted also

* style fixes
This commit is contained in:
Oliver
2023-02-26 16:36:11 +11:00
committed by GitHub
parent 0c5dc2865c
commit b657fb4405
7 changed files with 72 additions and 15 deletions

View File

@@ -448,3 +448,40 @@ class PartPricingTests(InvenTreeTestCase):
from django_q.models import OrmQ
self.assertEqual(OrmQ.objects.count(), 101)
def test_delete_part_with_stock_items(self):
"""Test deleting a part instance with stock items.
This is to test a specific edge condition which was discovered that caused an IntegrityError.
Ref: https://github.com/inventree/InvenTree/issues/4419
Essentially a series of on_delete listeners caused a new PartPricing object to be created,
but it pointed to a Part instance which was slated to be deleted inside an atomic transaction.
"""
p = part.models.Part.objects.create(
name="my part",
description="my part description",
active=False,
)
# Create some stock items
for _idx in range(3):
stock.models.StockItem.objects.create(
part=p,
quantity=10,
purchase_price=Money(10, 'USD')
)
# Check that a PartPricing object exists
self.assertTrue(part.models.PartPricing.objects.filter(part=p).exists())
# Delete the part
p.delete()
# Check that the PartPricing object has been deleted
self.assertFalse(part.models.PartPricing.objects.filter(part=p).exists())
# Try to update pricing (should fail gracefully as the Part has been deleted)
p.schedule_pricing_update(create=False)
self.assertFalse(part.models.PartPricing.objects.filter(part=p).exists())