Commit 2b5062e2 authored by Titouan Soulard's avatar Titouan Soulard

erp5_trade: add test for variated products

parent f7f6c0fc
...@@ -36,6 +36,8 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase): ...@@ -36,6 +36,8 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
""" """
default_stock_entry_date = DateTime(2024, 1, 1) default_stock_entry_date = DateTime(2024, 1, 1)
default_inventory_date = DateTime(2024, 1, 10) default_inventory_date = DateTime(2024, 1, 10)
default_product_portal_type = "Product"
default_product_variation_portal_type = "Product Individual Variation"
def getBusinessTemplateList(self): def getBusinessTemplateList(self):
return ("erp5_core", "erp5_base","erp5_pdm", "erp5_simulation", return ("erp5_core", "erp5_base","erp5_pdm", "erp5_simulation",
...@@ -76,13 +78,30 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase): ...@@ -76,13 +78,30 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
stop_date=self.default_stock_entry_date, stop_date=self.default_stock_entry_date,
) )
for (product_value, quantity, price) in lines: for line in lines:
purchase_packing_list_value.newContent( purchase_packing_list_line_value = purchase_packing_list_value.newContent(
portal_type="Purchase Packing List Line", portal_type="Purchase Packing List Line",
resource_value=product_value,
quantity=quantity,
price=price,
) )
purchase_packing_list_line_variation_list = []
if len(line) == 2:
(product_value, cells) = line
for (variation_value, quantity, price) in cells:
purchase_packing_list_line_value.newContent(
portal_type="Purchase Packing List Cell",
variation_value=variation_value,
quantity=quantity,
price=price,
)
purchase_packing_list_line_variation_list.append(variation_value)
elif len(line) == 3:
(product_value, quantity, price) = line
purchase_packing_list_line_value.setQuantity(quantity)
purchase_packing_list_line_value.setPrice(price)
purchase_packing_list_line_value.setResourceValue(product_value)
purchase_packing_list_line_value.setVariationValueList(purchase_packing_list_line_variation_list)
purchase_packing_list_value.confirm() purchase_packing_list_value.confirm()
self.tic() self.tic()
...@@ -100,20 +119,47 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase): ...@@ -100,20 +119,47 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
destination_value=self.destination_organisation_value, destination_value=self.destination_organisation_value,
start_date=self.default_inventory_date, start_date=self.default_inventory_date,
) )
inventory_line_variation_list = []
for (product_value, quantity, price) in lines: for line in lines:
inventory_value.newContent( inventory_line_value = inventory_value.newContent(
portal_type="Inventory Line", portal_type="Inventory Line",
resource_value=product_value,
inventory=quantity,
price=price,
) )
if len(line) == 2:
(product_value, cells) = line
inventory_line_variation_list = [cell[0] for cell in cells]
inventory_line_cell_range = ["variation/" + variation.getRelativeUrl() for variation in inventory_line_variation_list]
inventory_line_value.setResourceValue(product_value)
inventory_line_value.setVariationValueList(inventory_line_variation_list)
inventory_line_value.setCellRange(inventory_line_cell_range, base_id="movement")
for (variation_value, quantity, price) in cells:
cell_variation_text = "variation/" + variation_value.getRelativeUrl()
inventory_line_value.newCell(
cell_variation_text,
portal_type="Inventory Cell",
variation_value=variation_value,
inventory=quantity,
price=price,
base_id="movement",
)
inventory_line_variation_list.append(variation_value)
elif len(line) == 3:
(product_value, quantity, price) = line
inventory_line_value.edit(
resource_value=product_value,
inventory=quantity,
price=price
)
inventory_value.Inventory_computeOffsetAction() inventory_value.Inventory_computeOffsetAction()
self.tic() self.tic()
return inventory_value return inventory_value
# Used for unvariated products, returns tuple
def _getInventoryAmount(self, product_value): def _getInventoryAmount(self, product_value):
inventory = self.portal.portal_simulation.getInventory( inventory = self.portal.portal_simulation.getInventory(
section_uid=self.destination_organisation_value.getUid(), section_uid=self.destination_organisation_value.getUid(),
...@@ -129,13 +175,28 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase): ...@@ -129,13 +175,28 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
return (inventory, total_price) return (inventory, total_price)
# Used for variated products, returns dict of tuples
def _getInventoryDict(self, product_value):
inventory_dict = {}
inventory_list = self.portal.portal_simulation.getInventoryList(
section_uid=self.destination_organisation_value.getUid(),
node_uid=self.destination_organisation_value.getUid(),
resource_uid=product_value.getUid(),
)
for inventory in inventory_list:
variation = inventory.variation_text.split("/", 1)[1]
inventory_dict[variation] = (inventory.quantity, inventory.total_price)
return inventory_dict
def test_01_addUnvariatedToStock(self): def test_01_addUnvariatedToStock(self):
""" """
For an unvariated product, checks that quantity and price are correct For an unvariated product, checks that quantity and price are correct
after noticing that there are more products than expected in stock. after noticing that there are more products than expected in stock.
""" """
self.unvariated_product = self.portal.product_module.newContent( self.unvariated_product = self.portal.product_module.newContent(
portal_type="Product", portal_type=self.default_product_portal_type,
title="Unvariated product", title="Unvariated product",
) )
self.unvariated_product.setQuantityUnitList(["unit/piece"]) self.unvariated_product.setQuantityUnitList(["unit/piece"])
...@@ -173,3 +234,82 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase): ...@@ -173,3 +234,82 @@ class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
# Note that the price is only taken into account when there actually # Note that the price is only taken into account when there actually
# are movements. Otherwise, it is totally discarded. # are movements. Otherwise, it is totally discarded.
self.assertEqual(310.0, final_price) self.assertEqual(310.0, final_price)
def test_02_addVariatedToStock(self):
"""
For a variated product, checks that quantity and price are correct
after noticing that there are more products than expected in stock.
"""
self.variated_product = self.portal.product_module.newContent(
portal_type=self.default_product_portal_type,
title="Variated product",
)
self.variated_product.setQuantityUnitList(["unit/piece"])
self.variated_product.setIndividualVariationBaseCategoryList(["variation"])
self.variated_product_variation_list = []
for (i, variation_name) in enumerate(["A", "B", "C"]):
variated_product_variation = self.variated_product.newContent(
portal_type=self.default_product_variation_portal_type,
title="Variation " + variation_name,
)
self.variated_product_variation_list.append([variated_product_variation, (i + 1), (i + 1) * 100.0])
self.tic()
self._createPurchasePackingList([
(self.variated_product, self.variated_product_variation_list),
])
# Sanity check: ensure Inventory API reports what's expected
inventory_dict = self._getInventoryDict(self.variated_product)
for variation in self.variated_product_variation_list:
variation_relative_url = variation[0].getRelativeUrl()
# Assert: variation exists in stock
self.assertIn(variation_relative_url, inventory_dict)
(initial_inventory, initial_price) = inventory_dict[variation_relative_url]
# Assert: quantity is correct
self.assertEqual(variation[1], initial_inventory)
# Assert: price is correct
self.assertEqual(variation[1] * variation[2], initial_price)
# We are going to add:
# - 1 variation B at price 100;
# - 2 variations C at price 200.
# Then create an Inventory with the new values.
for (i, variation) in enumerate(self.variated_product_variation_list):
variation[1] += i
variation[2] = i * 100.0
inventory_value = self._createInventory([
(self.variated_product, self.variated_product_variation_list),
])
# Assert: one and only one offset line was created
inventory_offset_line_list = inventory_value.objectValues(portal_type="Inventory Offset Line")
self.assertEqual(1, len(inventory_offset_line_list))
inventory_offset_line = inventory_offset_line_list[0]
inventory_offset_cell_list = inventory_offset_line.objectValues(portal_type="Inventory Offset Cell")
# Assert: two and only two offset cells were created
self.assertEqual(2, len(inventory_offset_cell_list))
# Assert: offset cells were computed correctly
self.assertEqual(1.0, inventory_offset_cell_list[0].getQuantity())
self.assertEqual(100.0, inventory_offset_cell_list[0].getPrice())
self.assertEqual(2.0, inventory_offset_cell_list[1].getQuantity())
self.assertEqual(200.0, inventory_offset_cell_list[1].getPrice())
inventory_dict = self._getInventoryDict(self.variated_product)
for (i, variation) in enumerate(self.variated_product_variation_list):
variation_relative_url = variation[0].getRelativeUrl()
# Assert: variation exists in stock
self.assertIn(variation_relative_url, inventory_dict)
(final_inventory, final_price) = inventory_dict[variation_relative_url]
# Assert: quantity is correct
self.assertEqual(variation[1], final_inventory)
# Assert: price is correct
#self.assertEqual(i * 100.0 + (i + , final_price)
...@@ -43,7 +43,9 @@ ...@@ -43,7 +43,9 @@
<item> <item>
<key> <string>text_content_warning_message</string> </key> <key> <string>text_content_warning_message</string> </key>
<value> <value>
<tuple/> <tuple>
<string>W:311, 24: Unused variable \'final_price\' (unused-variable)</string>
</tuple>
</value> </value>
</item> </item>
<item> <item>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment