From 53964f59576303dec7b9f740ace6dc8ccfa7e303 Mon Sep 17 00:00:00 2001 From: "matthieu.saison" Date: Tue, 14 Nov 2023 15:09:55 +0100 Subject: [PATCH] update & add test product --- .../routers/quotation.py | 19 +++--- shopinvader_api_quotation/tests/__init__.py | 1 + .../tests/test_product.py | 65 +++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 shopinvader_api_quotation/tests/test_product.py diff --git a/shopinvader_api_quotation/routers/quotation.py b/shopinvader_api_quotation/routers/quotation.py index 8eb6670ac5..7eb35c8b10 100644 --- a/shopinvader_api_quotation/routers/quotation.py +++ b/shopinvader_api_quotation/routers/quotation.py @@ -28,7 +28,7 @@ def get( quotation_id: int | None = None, ) -> Sale | None: return Sale.from_sale_order( - env["shopinvader_api_sale.sales_router.helper"] + env["shopinvader_api_quotation.quotations_router.helper"] .new({"partner": partner}) ._get(quotation_id) ) @@ -41,7 +41,7 @@ def confirm_quotation( quotation_id: int | None = None, ) -> None: order = ( - env["shopinvader_api_sale.sales_router.helper"] + env["shopinvader_api_quotation.quotations_router.helper"] .new({"partner": partner}) ._confirm(quotation_id) ) @@ -56,7 +56,7 @@ def search_quotation( partner: Annotated[ResPartner, Depends(authenticated_partner)], ) -> PagedCollection[Sale]: count, orders = ( - env["shopinvader_api_sale.sales_router.helper"] + env["shopinvader_api_quotation.quotations_router.helper"] .new({"partner": partner}) ._search(paging, params) ) @@ -74,17 +74,15 @@ def update_quotation( quotation_id: int, ) -> Sale: order = ( - env["shopinvader_api_sale.sales_router.helper"] + env["shopinvader_api_quotation.quotations_router.helper"] .new({"partner": partner}) - ._get(quotation_id) + ._update(quotation_id, data) ) - - vals = data.to_sale_order_vals() - order.write(vals) return Sale.from_sale_order(order) class ShopinvaderApiSaleSalesRouterHelper(models.AbstractModel): + _name = "shopinvader_api_quotation.quotations_router.helper" _inherit = "shopinvader_api_sale.sales_router.helper" def _get_domain_adapter(self): @@ -97,3 +95,8 @@ def _confirm(self, quotation): order = self._get(quotation) order.action_confirm() return order + + def _update(self, quotation, data): + order = self._get(quotation) + order.write(data.to_sale_order_vals()) + return order diff --git a/shopinvader_api_quotation/tests/__init__.py b/shopinvader_api_quotation/tests/__init__.py index eb758d8b7f..d2d808daa4 100644 --- a/shopinvader_api_quotation/tests/__init__.py +++ b/shopinvader_api_quotation/tests/__init__.py @@ -1 +1,2 @@ from . import test_shopinvader_api_quotation +from . import test_product diff --git a/shopinvader_api_quotation/tests/test_product.py b/shopinvader_api_quotation/tests/test_product.py new file mode 100644 index 0000000000..8a40c891d0 --- /dev/null +++ b/shopinvader_api_quotation/tests/test_product.py @@ -0,0 +1,65 @@ +# Copyright 2021 Camptocamp (https://www.camptocamp.com). +# @author Iván Todorovich +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import SavepointCase + + +class TestProduct(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.attribute = cls.env.ref("product.product_attribute_2") + cls.value_1 = cls.env.ref("product.product_attribute_value_3") + cls.value_2 = cls.env.ref("product.product_attribute_value_4") + cls.product_template = cls.env["product.template"].create( + { + "name": "Test Product", + } + ) + + def test_simple_product(self): + # Back and forth flow, setting and reading on template and single variant + self.assertFalse(self.product_template.shop_only_quotation) + self.product_template.shop_only_quotation = True + self.assertTrue(self.product_template.product_variant_ids.shop_only_quotation) + self.product_template.product_variant_ids.shop_only_quotation = False + self.assertFalse(self.product_template.shop_only_quotation) + + def test_multi_product(self): + # Configure product with multiple variants + self.product_template.attribute_line_ids = [ + ( + 0, + 0, + { + "attribute_id": self.attribute.id, + "value_ids": [(6, 0, [self.value_1.id, self.value_2.id])], + }, + ) + ] + self.assertEqual(len(self.product_template.product_variant_ids), 2) + template = self.product_template + product_1 = self.product_template.product_variant_ids[0] + product_2 = self.product_template.product_variant_ids[1] + # Set on the template should set all variants + template.shop_only_quotation = True + self.assertTrue(product_1.shop_only_quotation) + self.assertTrue(product_2.shop_only_quotation) + # Set a variant to false should set the template to false + # It's only true if it's true for all variants + product_1.shop_only_quotation = False + self.assertFalse(template.shop_only_quotation) + self.assertTrue(product_2.shop_only_quotation, "Other variant shouldn't change") + # Set false on template should set all variants to false + template.shop_only_quotation = False + self.assertFalse(product_1.shop_only_quotation) + self.assertFalse(product_2.shop_only_quotation) + # Create a new variant combination should copy the value from template + template.shop_only_quotation = True + value_3 = self.value_2.copy({"name": "Another color"}) + template.attribute_line_ids.value_ids = [(4, value_3.id)] + self.assertEqual(len(self.product_template.product_variant_ids), 3) + product_3 = self.product_template.product_variant_ids - (product_1 | product_2) + self.assertTrue(product_3.shop_only_quotation)