flectra/addons/product/tests/test_variants.py
2018-01-16 02:34:37 -08:00

118 lines
5.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
from . import common
class TestVariants(common.TestProductCommon):
def setUp(self):
res = super(TestVariants, self).setUp()
self.size_attr = self.env['product.attribute'].create({'name': 'Size'})
self.size_attr_value_s = self.env['product.attribute.value'].create({'name': 'S', 'attribute_id': self.size_attr.id})
self.size_attr_value_m = self.env['product.attribute.value'].create({'name': 'M', 'attribute_id': self.size_attr.id})
self.size_attr_value_l = self.env['product.attribute.value'].create({'name': 'L', 'attribute_id': self.size_attr.id})
return res
def test_variants_creation_mono(self):
test_template = self.env['product.template'].create({
'name': 'Sofa',
'uom_id': self.uom_unit.id,
'uom_po_id': self.uom_unit.id,
'attribute_line_ids': [(0, 0, {
'attribute_id': self.size_attr.id,
'value_ids': [(4, self.size_attr_value_s.id)],
})]
})
# produced variants: one variant, because mono value
self.assertEqual(len(test_template.product_variant_ids), 1)
self.assertEqual(test_template.product_variant_ids.attribute_value_ids, self.size_attr_value_s)
def test_variants_creation_mono_double(self):
test_template = self.env['product.template'].create({
'name': 'Sofa',
'uom_id': self.uom_unit.id,
'uom_po_id': self.uom_unit.id,
'attribute_line_ids': [(0, 0, {
'attribute_id': self.prod_att_1.id,
'value_ids': [(4, self.prod_attr1_v2.id)],
}), (0, 0, {
'attribute_id': self.size_attr.id,
'value_ids': [(4, self.size_attr_value_s.id)],
})]
})
# produced variants: one variant, because only 1 combination is possible
self.assertEqual(len(test_template.product_variant_ids), 1)
self.assertEqual(test_template.product_variant_ids.attribute_value_ids, self.size_attr_value_s + self.prod_attr1_v2)
def test_variants_creation_mono_multi(self):
test_template = self.env['product.template'].create({
'name': 'Sofa',
'uom_id': self.uom_unit.id,
'uom_po_id': self.uom_unit.id,
'attribute_line_ids': [(0, 0, {
'attribute_id': self.prod_att_1.id,
'value_ids': [(4, self.prod_attr1_v2.id)],
}), (0, 0, {
'attribute_id': self.size_attr.id,
'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id)],
})]
})
# produced variants: two variants, simple matrix
self.assertEqual(len(test_template.product_variant_ids), 2)
for value in self.size_attr_value_s + self.size_attr_value_m:
products = self.env['product.product'].search([
('product_tmpl_id', '=', test_template.id),
('attribute_value_ids', 'in', value.id),
('attribute_value_ids', 'in', self.prod_attr1_v2.id)
])
self.assertEqual(len(products), 1)
def test_variants_creation_matrix(self):
test_template = self.env['product.template'].create({
'name': 'Sofa',
'uom_id': self.uom_unit.id,
'uom_po_id': self.uom_unit.id,
'attribute_line_ids': [(0, 0, {
'attribute_id': self.prod_att_1.id,
'value_ids': [(4, self.prod_attr1_v1.id), (4, self.prod_attr1_v2.id)],
}), (0, 0, {
'attribute_id': self.size_attr.id,
'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id), (4, self.size_attr_value_l.id)],
})]
})
# produced variants: value matrix : 2x3 values
self.assertEqual(len(test_template.product_variant_ids), 6)
for value_1 in self.prod_attr1_v1 + self.prod_attr1_v2:
for value_2 in self.size_attr_value_m + self.size_attr_value_m + self.size_attr_value_l:
products = self.env['product.product'].search([
('product_tmpl_id', '=', test_template.id),
('attribute_value_ids', 'in', value_1.id),
('attribute_value_ids', 'in', value_2.id)
])
self.assertEqual(len(products), 1)
def test_variants_creation_multi_update(self):
test_template = self.env['product.template'].create({
'name': 'Sofa',
'uom_id': self.uom_unit.id,
'uom_po_id': self.uom_unit.id,
'attribute_line_ids': [(0, 0, {
'attribute_id': self.prod_att_1.id,
'value_ids': [(4, self.prod_attr1_v1.id), (4, self.prod_attr1_v2.id)],
}), (0, 0, {
'attribute_id': self.size_attr.id,
'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id)],
})]
})
size_attribute_line = test_template.attribute_line_ids.filtered(lambda line: line.attribute_id == self.size_attr)
test_template.write({
'attribute_line_ids': [(1, size_attribute_line.id, {
'value_ids': [(4, self.size_attr_value_l.id)],
})]
})