From 6ec67f9362b18ce3645c10f13b823d783c3540b1 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Fri, 30 Sep 2016 14:33:19 +0200 Subject: [PATCH] [IMP] sale_order_variant_mgmt: Tests --- sale_order_variant_mgmt/tests/__init__.py | 4 + .../tests/test_sale_order_variant_mgmt.py | 100 ++++++++++++++++++ .../wizard/sale_manage_variant.py | 7 +- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 sale_order_variant_mgmt/tests/__init__.py create mode 100644 sale_order_variant_mgmt/tests/test_sale_order_variant_mgmt.py diff --git a/sale_order_variant_mgmt/tests/__init__.py b/sale_order_variant_mgmt/tests/__init__.py new file mode 100644 index 00000000000..32b795f7c1a --- /dev/null +++ b/sale_order_variant_mgmt/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_sale_order_variant_mgmt diff --git a/sale_order_variant_mgmt/tests/test_sale_order_variant_mgmt.py b/sale_order_variant_mgmt/tests/test_sale_order_variant_mgmt.py new file mode 100644 index 00000000000..4950a92b3df --- /dev/null +++ b/sale_order_variant_mgmt/tests/test_sale_order_variant_mgmt.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Pedro M. Baeza +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp.tests import common + + +class TestSaleOrderVariantMgmt(common.SavepointCase): + @classmethod + def setUpClass(cls): + super(TestSaleOrderVariantMgmt, cls).setUpClass() + cls.partner = cls.env['res.partner'].create({'name': 'Test partner'}) + cls.attribute1 = cls.env['product.attribute'].create({ + 'name': 'Test Attribute 1', + 'value_ids': [ + (0, 0, {'name': 'Value 1'}), + (0, 0, {'name': 'Value 2'}), + ], + }) + cls.attribute2 = cls.env['product.attribute'].create({ + 'name': 'Test Attribute 2', + 'value_ids': [ + (0, 0, {'name': 'Value X'}), + (0, 0, {'name': 'Value Y'}), + ], + }) + cls.product_tmpl = cls.env['product.template'].create({ + 'name': 'Test template', + 'attribute_line_ids': [ + (0, 0, { + 'attribute_id': cls.attribute1.id, + 'value_ids': [(6, 0, cls.attribute1.value_ids.ids)], + }), + (0, 0, { + 'attribute_id': cls.attribute2.id, + 'value_ids': [(6, 0, cls.attribute2.value_ids.ids)], + }), + ], + }) + assert len(cls.product_tmpl.product_variant_ids) == 4 + order = cls.env['sale.order'].new({'partner_id': cls.partner.id}) + order.onchange_partner_id() + cls.order = order.create(order._convert_to_write(order._cache)) + cls.Wizard = cls.env['sale.manage.variant'].with_context( + active_ids=cls.order.ids, active_id=cls.order.id, + active_model=cls.order._name + ) + cls.SaleOrderLine = cls.env['sale.order.line'] + + def test_add_variants(self): + wizard = self.Wizard.new({'product_tmpl_id': self.product_tmpl.id}) + wizard._onchange_product_tmpl_id() + wizard = wizard.create(wizard._convert_to_write(wizard._cache)) + self.assertEqual(len(wizard.variant_line_ids), 4) + wizard.variant_line_ids[0].product_uom_qty = 1 + wizard.variant_line_ids[1].product_uom_qty = 2 + wizard.variant_line_ids[2].product_uom_qty = 3 + wizard.variant_line_ids[3].product_uom_qty = 4 + wizard.button_transfer_to_order() + self.assertEqual(len(self.order.order_line), 4, + "There should be 4 lines in the sale order") + + def test_modify_variants(self): + product1 = self.product_tmpl.product_variant_ids[0] + order_line1 = self.SaleOrderLine.new({ + 'order_id': self.order.id, + 'product_id': product1.id, + 'product_uom_qty': 1, + }) + order_line1.product_id_change() + product2 = self.product_tmpl.product_variant_ids[1] + order_line1 = self.SaleOrderLine.create( + order_line1._convert_to_write(order_line1._cache)) + order_line2 = self.SaleOrderLine.new({ + 'order_id': self.order.id, + 'product_id': product2.id, + 'product_uom_qty': 2, + }) + order_line2.product_id_change() + order_line2 = self.SaleOrderLine.create( + order_line2._convert_to_write(order_line2._cache)) + Wizard2 = self.Wizard.with_context( + default_product_tmpl_id=self.product_tmpl.id, + active_model='sale.order.line', + active_id=order_line1.id, active_ids=order_line1.ids + ) + wizard = Wizard2.create({}) + wizard._onchange_product_tmpl_id() + self.assertEqual( + len(wizard.variant_line_ids.filtered('product_uom_qty')), 2, + "There should be two fields with any quantity in the wizard." + ) + wizard.variant_line_ids.filtered( + lambda x: x.product_id == product1).product_uom_qty = 0 + wizard.variant_line_ids.filtered( + lambda x: x.product_id == product2).product_uom_qty = 10 + wizard.button_transfer_to_order() + self.assertFalse(order_line1.exists(), "Order line not removed.") + self.assertEqual( + order_line2.product_uom_qty, 10, "Order line not change quantity.") diff --git a/sale_order_variant_mgmt/wizard/sale_manage_variant.py b/sale_order_variant_mgmt/wizard/sale_manage_variant.py index 20409014eec..22b257e884d 100644 --- a/sale_order_variant_mgmt/wizard/sale_manage_variant.py +++ b/sale_order_variant_mgmt/wizard/sale_manage_variant.py @@ -16,7 +16,7 @@ class SaleManageVariant(models.TransientModel): # HACK: https://github.com/OCA/server-tools/pull/492#issuecomment-237594285 @api.multi - def onchange(self, values, field_name, field_onchange): + def onchange(self, values, field_name, field_onchange): # pragma: no cover if "variant_line_ids" in field_onchange: for sub in ("product_id", "disabled", "value_x", "value_y", "product_uom_qty"): @@ -65,12 +65,14 @@ def button_transfer_to_order(self): else: sale_order = record OrderLine = self.env['sale.order.line'] + lines2unlink = OrderLine for line in self.variant_line_ids: order_line = sale_order.order_line.filtered( lambda x: x.product_id == line.product_id) if order_line: if not line.product_uom_qty: - order_line.unlink() + # Done this way because there's a side effect removing here + lines2unlink |= order_line else: order_line.product_uom_qty = line.product_uom_qty elif line.product_uom_qty: @@ -83,6 +85,7 @@ def button_transfer_to_order(self): order_line_vals = order_line._convert_to_write( order_line._cache) sale_order.order_line.create(order_line_vals) + lines2unlink.unlink() class SaleManageVariantLine(models.TransientModel):