mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-01 21:16:46 +00:00
* Squashed commit of the following: commit 52d7ff0f650bbcfa2d93ac96562b44269d3812a7 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 23:03:20 2024 +0100 fixed lookup commit 0d076eaea89dce24f08af247479b3b4dff1b4df3 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 23:03:08 2024 +0100 switched to pathlib for lookup commit 473e75eda205793769946e923748356ffd7e5b4b Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:52:30 2024 +0100 fix wrong url response commit fd74f8d703399c19cb3616ea3b2656a50cd7a6e5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 21:14:38 2024 +0100 switched to ruff for import sorting commit f83fedbbb8de261ff8c706e179519e58e7a91064 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 21:03:14 2024 +0100 switched to single quotes everywhere commit a92442e60e23be0ff5dcf42d222b0d95823ecb9b Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:58:23 2024 +0100 added autofixes commit cc66c93136fcae8a701810a4f4f38ef3b570be61 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:56:47 2024 +0100 enable autoformat commit 1f343606ec1f2a99acf8a37b9900d78a8fb37282 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:42:14 2024 +0100 Squashed commit of the following: commit f5cf7b2e7872fc19633321713965763d1890b495 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:36:57 2024 +0100 fixed reqs commit 9d845bee98befa4e53c2ac3c783bd704369e3ad2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:32:35 2024 +0100 disable autofix/format commit aff5f271484c3500df7ddde043767c008ce4af21 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:50 2024 +0100 adjust checks commit 47271cf1efa848ec8374a0d83b5646d06fffa6e7 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:22 2024 +0100 reorder order of operations commit e1bf178b40b3f0d2d59ba92209156c43095959d2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:01:09 2024 +0100 adapted ruff settings to better fit code base commit ad7d88a6f4f15c9552522131c4e207256fc2bbf6 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:59:45 2024 +0100 auto fixed docstring commit a2e54a760e17932dbbc2de0dec23906107f2cda9 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:46:35 2024 +0100 fix getattr useage commit cb80c73bc6c0be7f5d2ed3cc9b2ac03fdefd5c41 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:25:09 2024 +0100 fix requirements file commit b7780bbd21a32007f3b0ce495b519bf59bb19bf5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:42:28 2024 +0100 fix removed sections commit 71f1681f55c15f62c16c1d7f30a745adc496db97 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:41:21 2024 +0100 fix djlint syntax commit a0bcf1bccef8a8ffd482f38e2063bc9066e1d759 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:35:28 2024 +0100 remove flake8 from code base commit 22475b31cc06919785be046e007915e43f356793 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:34:56 2024 +0100 remove flake8 from code base commit 0413350f14773ac6161473e0cfb069713c13c691 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:39 2024 +0100 moved ruff section commit d90c48a0bf98befdfacbbb093ee56cdb28afb40d Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:24 2024 +0100 move djlint config to pyproject commit c5ce55d5119bf2e35e429986f62f875c86178ae1 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:20:39 2024 +0100 added isort again commit 42a41d23afc280d4ee6f0e640148abc6f460f05a Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:19:02 2024 +0100 move config section commit 85692331816348cb1145570340d1f6488a8265cc Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:17:52 2024 +0100 fix codespell error commit 2897c6704d1311a800ce5aa47878d96d6980b377 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 17:29:21 2024 +0100 replaced flake8 with ruff mostly for speed improvements * enable docstring checks * fix docstrings * fixed D417 Missing argument description * Squashed commit of the following: commit d3b795824b5d6d1c0eda67150b45b5cd672b3f6b Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:56:17 2024 +0100 fixed source path commit 0bac0c19b88897a19d5c995e4ff50427718b827e Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:47:53 2024 +0100 fixed req commit 9f61f01d9cc01f1fb7123102f3658c890469b8ce Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 22:45:18 2024 +0100 added missing toml req commit 91b71ed24a6761b629768d0ad8829fec2819a966 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:49:50 2024 +0100 moved isort config commit 12460b04196b12d0272d40552402476d5492fea5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:43:22 2024 +0100 remove flake8 section from setup.cfg commit f5cf7b2e7872fc19633321713965763d1890b495 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:36:57 2024 +0100 fixed reqs commit 9d845bee98befa4e53c2ac3c783bd704369e3ad2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:32:35 2024 +0100 disable autofix/format commit aff5f271484c3500df7ddde043767c008ce4af21 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:50 2024 +0100 adjust checks commit 47271cf1efa848ec8374a0d83b5646d06fffa6e7 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:28:22 2024 +0100 reorder order of operations commit e1bf178b40b3f0d2d59ba92209156c43095959d2 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 20:01:09 2024 +0100 adapted ruff settings to better fit code base commit ad7d88a6f4f15c9552522131c4e207256fc2bbf6 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:59:45 2024 +0100 auto fixed docstring commit a2e54a760e17932dbbc2de0dec23906107f2cda9 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:46:35 2024 +0100 fix getattr useage commit cb80c73bc6c0be7f5d2ed3cc9b2ac03fdefd5c41 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 19:25:09 2024 +0100 fix requirements file commit b7780bbd21a32007f3b0ce495b519bf59bb19bf5 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:42:28 2024 +0100 fix removed sections commit 71f1681f55c15f62c16c1d7f30a745adc496db97 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:41:21 2024 +0100 fix djlint syntax commit a0bcf1bccef8a8ffd482f38e2063bc9066e1d759 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:35:28 2024 +0100 remove flake8 from code base commit 22475b31cc06919785be046e007915e43f356793 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:34:56 2024 +0100 remove flake8 from code base commit 0413350f14773ac6161473e0cfb069713c13c691 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:39 2024 +0100 moved ruff section commit d90c48a0bf98befdfacbbb093ee56cdb28afb40d Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:24:24 2024 +0100 move djlint config to pyproject commit c5ce55d5119bf2e35e429986f62f875c86178ae1 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:20:39 2024 +0100 added isort again commit 42a41d23afc280d4ee6f0e640148abc6f460f05a Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:19:02 2024 +0100 move config section commit 85692331816348cb1145570340d1f6488a8265cc Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 18:17:52 2024 +0100 fix codespell error commit 2897c6704d1311a800ce5aa47878d96d6980b377 Author: Matthias Mair <code@mjmair.com> Date: Sun Jan 7 17:29:21 2024 +0100 replaced flake8 with ruff mostly for speed improvements * fix pyproject * make docstrings more uniform * auto-format * fix order * revert url change
451 lines
15 KiB
Python
451 lines
15 KiB
Python
"""Unit tests for Part pricing calculations."""
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from djmoney.contrib.exchange.models import convert_money
|
|
from djmoney.money import Money
|
|
|
|
import common.models
|
|
import common.settings
|
|
import company.models
|
|
import order.models
|
|
import part.models
|
|
import stock.models
|
|
from InvenTree.status_codes import PurchaseOrderStatus
|
|
from InvenTree.unit_test import InvenTreeTestCase
|
|
|
|
|
|
class PartPricingTests(InvenTreeTestCase):
|
|
"""Unit tests for part pricing calculations."""
|
|
|
|
def setUp(self):
|
|
"""Setup routines."""
|
|
super().setUp()
|
|
|
|
self.generate_exchange_rates()
|
|
|
|
# Create a new part for performing pricing calculations
|
|
# We will use 'metres' for the UOM here
|
|
# Some SupplierPart instances will have different units!
|
|
self.part = part.models.Part.objects.create(
|
|
name='PP',
|
|
description='A part with pricing, measured in metres',
|
|
assembly=True,
|
|
units='m',
|
|
)
|
|
|
|
def create_price_breaks(self):
|
|
"""Create some price breaks for the part, in various currencies."""
|
|
# First supplier part (CAD)
|
|
self.supplier_1 = company.models.Company.objects.create(
|
|
name='Supplier 1', is_supplier=True
|
|
)
|
|
|
|
self.sp_1 = company.models.SupplierPart.objects.create(
|
|
supplier=self.supplier_1,
|
|
part=self.part,
|
|
SKU='SUP_1',
|
|
pack_quantity='200 cm',
|
|
)
|
|
|
|
# Native pack quantity should be 2m
|
|
self.assertEqual(self.sp_1.pack_quantity_native, 2)
|
|
|
|
company.models.SupplierPriceBreak.objects.create(
|
|
part=self.sp_1, quantity=1, price=10.4, price_currency='CAD'
|
|
)
|
|
|
|
# Second supplier part (AUD)
|
|
self.supplier_2 = company.models.Company.objects.create(
|
|
name='Supplier 2', is_supplier=True
|
|
)
|
|
|
|
self.sp_2 = company.models.SupplierPart.objects.create(
|
|
supplier=self.supplier_2, part=self.part, SKU='SUP_2', pack_quantity='2.5'
|
|
)
|
|
|
|
# Native pack quantity should be 2.5m
|
|
self.assertEqual(self.sp_2.pack_quantity_native, 2.5)
|
|
|
|
self.sp_3 = company.models.SupplierPart.objects.create(
|
|
supplier=self.supplier_2,
|
|
part=self.part,
|
|
SKU='SUP_3',
|
|
pack_quantity='10 inches',
|
|
)
|
|
|
|
# Native pack quantity should be 0.254m
|
|
self.assertEqual(self.sp_3.pack_quantity_native, 0.254)
|
|
|
|
company.models.SupplierPriceBreak.objects.create(
|
|
part=self.sp_2, quantity=5, price=7.555, price_currency='AUD'
|
|
)
|
|
|
|
# Third supplier part (GBP)
|
|
company.models.SupplierPriceBreak.objects.create(
|
|
part=self.sp_2, quantity=10, price=4.55, price_currency='GBP'
|
|
)
|
|
|
|
def test_pricing_data(self):
|
|
"""Test link between Part and PartPricing model."""
|
|
# Initially there is no associated Pricing data
|
|
with self.assertRaises(ObjectDoesNotExist):
|
|
pricing = self.part.pricing_data
|
|
|
|
# Accessing in this manner should create the associated PartPricing instance
|
|
pricing = self.part.pricing
|
|
|
|
self.assertEqual(pricing.part, self.part)
|
|
|
|
# Default values should be null
|
|
self.assertIsNone(pricing.bom_cost_min)
|
|
self.assertIsNone(pricing.bom_cost_max)
|
|
|
|
self.assertIsNone(pricing.internal_cost_min)
|
|
self.assertIsNone(pricing.internal_cost_max)
|
|
|
|
self.assertIsNone(pricing.overall_min)
|
|
self.assertIsNone(pricing.overall_max)
|
|
|
|
def test_invalid_rate(self):
|
|
"""Ensure that conversion behaves properly with missing rates."""
|
|
...
|
|
|
|
def test_simple(self):
|
|
"""Tests for hard-coded values."""
|
|
pricing = self.part.pricing
|
|
|
|
# Add internal pricing
|
|
pricing.internal_cost_min = Money(1, 'USD')
|
|
pricing.internal_cost_max = Money(4, 'USD')
|
|
pricing.save()
|
|
|
|
self.assertEqual(pricing.overall_min, Money('1', 'USD'))
|
|
self.assertEqual(pricing.overall_max, Money('4', 'USD'))
|
|
|
|
# Add supplier pricing
|
|
pricing.supplier_price_min = Money(10, 'AUD')
|
|
pricing.supplier_price_max = Money(15, 'CAD')
|
|
pricing.save()
|
|
|
|
# Minimum pricing should not have changed
|
|
self.assertEqual(pricing.overall_min, Money('1', 'USD'))
|
|
|
|
# Maximum price has changed, and was specified in a different currency
|
|
self.assertEqual(pricing.overall_max, Money('8.823529', 'USD'))
|
|
|
|
# Add BOM cost
|
|
pricing.bom_cost_min = Money(0.1, 'GBP')
|
|
pricing.bom_cost_max = Money(25, 'USD')
|
|
pricing.save()
|
|
|
|
self.assertEqual(pricing.overall_min, Money('0.111111', 'USD'))
|
|
self.assertEqual(pricing.overall_max, Money('25', 'USD'))
|
|
|
|
def test_supplier_part_pricing(self):
|
|
"""Test for supplier part pricing."""
|
|
pricing = self.part.pricing
|
|
|
|
# Initially, no information (not yet calculated)
|
|
self.assertIsNone(pricing.supplier_price_min)
|
|
self.assertIsNone(pricing.supplier_price_max)
|
|
self.assertIsNone(pricing.overall_min)
|
|
self.assertIsNone(pricing.overall_max)
|
|
|
|
# Creating price breaks will cause the pricing to be updated
|
|
self.create_price_breaks()
|
|
|
|
pricing.update_pricing()
|
|
|
|
self.assertAlmostEqual(float(pricing.overall_min.amount), 2.015, places=2)
|
|
self.assertAlmostEqual(float(pricing.overall_max.amount), 3.06, places=2)
|
|
|
|
# Delete all supplier parts and re-calculate
|
|
self.part.supplier_parts.all().delete()
|
|
pricing.update_pricing()
|
|
pricing.refresh_from_db()
|
|
|
|
self.assertIsNone(pricing.supplier_price_min)
|
|
self.assertIsNone(pricing.supplier_price_max)
|
|
|
|
def test_internal_pricing(self):
|
|
"""Tests for internal price breaks."""
|
|
# Ensure internal pricing is enabled
|
|
common.models.InvenTreeSetting.set_setting('PART_INTERNAL_PRICE', True, None)
|
|
|
|
pricing = self.part.pricing
|
|
|
|
# Initially, no internal price breaks
|
|
self.assertIsNone(pricing.internal_cost_min)
|
|
self.assertIsNone(pricing.internal_cost_max)
|
|
|
|
currency = common.settings.currency_code_default()
|
|
|
|
for ii in range(5):
|
|
# Let's add some internal price breaks
|
|
part.models.PartInternalPriceBreak.objects.create(
|
|
part=self.part, quantity=ii + 1, price=10 - ii, price_currency=currency
|
|
)
|
|
|
|
pricing.update_internal_cost()
|
|
|
|
# Expected money value
|
|
m_expected = Money(10 - ii, currency)
|
|
|
|
# Minimum cost should keep decreasing as we add more items
|
|
self.assertEqual(pricing.internal_cost_min, m_expected)
|
|
self.assertEqual(pricing.overall_min, m_expected)
|
|
|
|
# Maximum cost should stay the same
|
|
self.assertEqual(pricing.internal_cost_max, Money(10, currency))
|
|
self.assertEqual(pricing.overall_max, Money(10, currency))
|
|
|
|
def test_stock_item_pricing(self):
|
|
"""Test for stock item pricing data."""
|
|
# Create a part
|
|
p = part.models.Part.objects.create(
|
|
name='Test part for pricing',
|
|
description='hello world, this is a part description',
|
|
)
|
|
|
|
# Create some stock items
|
|
prices = [(10, 'AUD'), (5, 'USD'), (2, 'CAD')]
|
|
|
|
for price, currency in prices:
|
|
stock.models.StockItem.objects.create(
|
|
part=p,
|
|
quantity=10,
|
|
purchase_price=price,
|
|
purchase_price_currency=currency,
|
|
)
|
|
|
|
# Ensure that initially, stock item pricing is disabled
|
|
common.models.InvenTreeSetting.set_setting(
|
|
'PRICING_USE_STOCK_PRICING', False, None
|
|
)
|
|
|
|
pricing = p.pricing
|
|
pricing.update_pricing()
|
|
|
|
# Check that stock item pricing data is not used
|
|
self.assertIsNone(pricing.purchase_cost_min)
|
|
self.assertIsNone(pricing.purchase_cost_max)
|
|
self.assertIsNone(pricing.overall_min)
|
|
self.assertIsNone(pricing.overall_max)
|
|
|
|
# Turn on stock pricing
|
|
common.models.InvenTreeSetting.set_setting(
|
|
'PRICING_USE_STOCK_PRICING', True, None
|
|
)
|
|
|
|
pricing.update_pricing()
|
|
|
|
self.assertIsNotNone(pricing.purchase_cost_min)
|
|
self.assertIsNotNone(pricing.purchase_cost_max)
|
|
|
|
self.assertEqual(pricing.overall_min, Money(1.176471, 'USD'))
|
|
self.assertEqual(pricing.overall_max, Money(6.666667, 'USD'))
|
|
|
|
def test_bom_pricing(self):
|
|
"""Unit test for BOM pricing calculations."""
|
|
pricing = self.part.pricing
|
|
|
|
self.assertIsNone(pricing.bom_cost_min)
|
|
self.assertIsNone(pricing.bom_cost_max)
|
|
|
|
currency = 'AUD'
|
|
|
|
for ii in range(10):
|
|
# Create a new part for the BOM
|
|
sub_part = part.models.Part.objects.create(
|
|
name=f'Sub Part {ii}',
|
|
description='A sub part for use in a BOM',
|
|
component=True,
|
|
assembly=False,
|
|
)
|
|
|
|
# Create some overall pricing
|
|
sub_part_pricing = sub_part.pricing
|
|
|
|
# Manually override internal price
|
|
sub_part_pricing.internal_cost_min = Money(2 * (ii + 1), currency)
|
|
sub_part_pricing.internal_cost_max = Money(3 * (ii + 1), currency)
|
|
sub_part_pricing.save()
|
|
|
|
part.models.BomItem.objects.create(
|
|
part=self.part, sub_part=sub_part, quantity=5
|
|
)
|
|
|
|
pricing.update_bom_cost()
|
|
|
|
# Check that the values have been updated correctly
|
|
self.assertEqual(pricing.currency, 'USD')
|
|
|
|
# Final overall pricing checks
|
|
self.assertEqual(pricing.overall_min, Money('366.666665', 'USD'))
|
|
self.assertEqual(pricing.overall_max, Money('550', 'USD'))
|
|
|
|
def test_purchase_pricing(self):
|
|
"""Unit tests for historical purchase pricing."""
|
|
self.create_price_breaks()
|
|
|
|
pricing = self.part.pricing
|
|
|
|
# Pre-calculation, pricing should be null
|
|
|
|
self.assertIsNone(pricing.purchase_cost_min)
|
|
self.assertIsNone(pricing.purchase_cost_max)
|
|
|
|
# Generate some purchase orders
|
|
po = order.models.PurchaseOrder.objects.create(
|
|
supplier=self.supplier_2, reference='PO-009'
|
|
)
|
|
|
|
# Add some line items to the order
|
|
|
|
# $5 AUD each @ 2.5m per unit = $2 AUD per metre
|
|
line_1 = po.add_line_item(
|
|
self.sp_2, quantity=10, purchase_price=Money(5, 'AUD')
|
|
)
|
|
|
|
# $3 CAD each @ 10 inches per unit = $0.3 CAD per inch = $11.81 CAD per metre
|
|
line_2 = po.add_line_item(self.sp_3, quantity=5, purchase_price=Money(3, 'CAD'))
|
|
|
|
pricing.update_purchase_cost()
|
|
|
|
# Cost is still null, as the order is not complete
|
|
self.assertIsNone(pricing.purchase_cost_min)
|
|
self.assertIsNone(pricing.purchase_cost_max)
|
|
|
|
po.status = PurchaseOrderStatus.COMPLETE.value
|
|
po.save()
|
|
|
|
pricing.update_purchase_cost()
|
|
|
|
# Cost is still null, as the lines have not been received
|
|
self.assertIsNone(pricing.purchase_cost_min)
|
|
self.assertIsNone(pricing.purchase_cost_max)
|
|
|
|
# Mark items as received
|
|
line_1.received = 4
|
|
line_1.save()
|
|
|
|
line_2.received = 5
|
|
line_2.save()
|
|
|
|
pricing.update_purchase_cost()
|
|
|
|
min_cost_aud = convert_money(pricing.purchase_cost_min, 'AUD')
|
|
max_cost_cad = convert_money(pricing.purchase_cost_max, 'CAD')
|
|
|
|
# Min cost in AUD = $2 AUD per metre
|
|
self.assertAlmostEqual(float(min_cost_aud.amount), 2, places=2)
|
|
|
|
# Min cost in USD
|
|
self.assertAlmostEqual(
|
|
float(pricing.purchase_cost_min.amount), 1.3333, places=2
|
|
)
|
|
|
|
# Max cost in CAD = $11.81 CAD per metre
|
|
self.assertAlmostEqual(float(max_cost_cad.amount), 11.81, places=2)
|
|
|
|
# Max cost in USD
|
|
self.assertAlmostEqual(float(pricing.purchase_cost_max.amount), 6.95, places=2)
|
|
|
|
def test_delete_with_pricing(self):
|
|
"""Test for deleting a part which has pricing information."""
|
|
# Create some pricing data
|
|
self.create_price_breaks()
|
|
|
|
# Check that pricing does exist
|
|
pricing = self.part.pricing
|
|
|
|
pricing.update_pricing()
|
|
pricing.save()
|
|
|
|
self.assertIsNotNone(pricing.overall_min)
|
|
self.assertIsNotNone(pricing.overall_max)
|
|
|
|
self.part.active = False
|
|
self.part.save()
|
|
|
|
# Remove the part from the database
|
|
self.part.delete()
|
|
|
|
# Check that the pricing was removed also
|
|
with self.assertRaises(part.models.PartPricing.DoesNotExist):
|
|
pricing.refresh_from_db()
|
|
|
|
def test_delete_without_pricing(self):
|
|
"""Test that we can delete a part which does not have pricing information."""
|
|
pricing = self.part.pricing
|
|
|
|
self.assertIsNone(pricing.pk)
|
|
|
|
self.part.active = False
|
|
self.part.save()
|
|
|
|
self.part.delete()
|
|
|
|
# Check that part was actually deleted
|
|
with self.assertRaises(part.models.Part.DoesNotExist):
|
|
self.part.refresh_from_db()
|
|
|
|
def test_check_missing_pricing(self):
|
|
"""Tests for check_missing_pricing background task.
|
|
|
|
Calling the check_missing_pricing task should:
|
|
- Create PartPricing objects where there are none
|
|
- Schedule pricing calculations for the newly created PartPricing objects
|
|
"""
|
|
from part.tasks import check_missing_pricing
|
|
|
|
# Create some parts
|
|
for ii in range(100):
|
|
part.models.Part.objects.create(
|
|
name=f'Part_{ii}', description='A test part'
|
|
)
|
|
|
|
# Ensure there is no pricing data
|
|
part.models.PartPricing.objects.all().delete()
|
|
|
|
check_missing_pricing()
|
|
|
|
# Check that PartPricing objects have been created
|
|
self.assertEqual(part.models.PartPricing.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')
|
|
)
|
|
|
|
# Manually schedule a pricing update (does not happen automatically in testing)
|
|
p.schedule_pricing_update(create=True, test=True)
|
|
|
|
# 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, test=True)
|
|
self.assertFalse(part.models.PartPricing.objects.filter(part=p).exists())
|