Skip to content

Commit

Permalink
[IMP] sale_order_variant_mgmt: Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobaeza committed Sep 30, 2016
1 parent 82ec3fd commit 6ec67f9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
4 changes: 4 additions & 0 deletions sale_order_variant_mgmt/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -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
100 changes: 100 additions & 0 deletions sale_order_variant_mgmt/tests/test_sale_order_variant_mgmt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
# 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.")
7 changes: 5 additions & 2 deletions sale_order_variant_mgmt/wizard/sale_manage_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down

0 comments on commit 6ec67f9

Please sign in to comment.