Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

[FIX] Readonly fields not being stored. #97

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions product_configurator_wizard/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-

from . import test_wizard
from . import test_wizard_attrs
53 changes: 42 additions & 11 deletions product_configurator_wizard/tests/test_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def get_attr_values(self, attr_val_ext_ids=None):

return attr_vals

def get_wizard_write_dict(self, wizard, attr_values):
def get_wizard_write_dict(self, wizard, attr_values, remove_values=None):
"""Turn a series of attribute.value objects to a dictionary meant for
writing values to the product.configurator wizard"""

write_dict = {}
if remove_values is None:
remove_values = []

multi_attr_ids = wizard.product_tmpl_id.attribute_line_ids.filtered(
lambda x: x.multi).mapped('attribute_id').ids
Expand All @@ -40,6 +42,10 @@ def get_wizard_write_dict(self, wizard, attr_values):
continue
write_dict.update({field_name: val.id})

for val in remove_values:
field_name = wizard.field_prefix + str(val.attribute_id.id)
write_dict.update({field_name: False})

return write_dict

def wizard_write_proceed(self, wizard, attr_vals, value_ids=None):
Expand Down Expand Up @@ -94,30 +100,55 @@ def test_wizard_configuration(self):
self.assertTrue(len(config_variants) == 1,
"Wizard did not create a configurable variant")

def do_reconfigure(self, order_line, attr_vals):
reconfig_action = order_line.reconfigure_product()

wizard = self.env['product.configurator'].browse(
reconfig_action.get('res_id')
)

self.wizard_write_proceed(wizard, attr_vals)

# if wizard only had one step, it would already have completed
if wizard.exists():
# Cycle through steps until wizard ends
while wizard.action_next_step():
pass

def test_reconfiguration(self):
"""Test reconfiguration functionality of the wizard"""
self.test_wizard_configuration()

existing_lines = self.so.order_line

order_line = self.so.order_line.filtered(
lambda l: l.product_id.config_ok
)

reconfig_action = order_line.reconfigure_product()
self.do_reconfigure(order_line,
self.get_attr_values(['diesel', '220d'])
)

wizard = self.env['product.configurator'].browse(
reconfig_action.get('res_id')
)
config_variants = self.env['product.product'].search([
('config_ok', '=', True)
])

attr_vals = self.get_attr_values(['diesel', '220d'])
self.wizard_write_proceed(wizard, attr_vals)
self.assertTrue(len(config_variants) == 2,
"Wizard reconfiguration did not create a new variant")

created_line = self.so.order_line - existing_lines
self.assertTrue(len(created_line) == 0,
"Wizard created an order line on reconfiguration")

# Cycle through steps until wizard ends
while wizard.action_next_step():
pass
# test that running through again with the same values does not
# create another variant
self.do_reconfigure(order_line,
self.get_attr_values(['diesel', '220d'])
)

config_variants = self.env['product.product'].search([
('config_ok', '=', True)
])

self.assertTrue(len(config_variants) == 2,
"Wizard reconfiguration did not create a new variant")
"Wizard reconfiguration created a redundant variant")
201 changes: 201 additions & 0 deletions product_configurator_wizard/tests/test_wizard_attrs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
# -*- coding: utf-8 -*-

from odoo.addons.product_configurator_wizard.tests.test_wizard \
import ConfigurationRules


class ConfigurationAttributes(ConfigurationRules):

def setUp(self):
"""
Product with 3 sizes:
Small or Medium allow Blue or Red, but colour is optional
Large does not allow colour selection
"""
super(ConfigurationAttributes, self).setUp()

self.attr_size = self.env['product.attribute'].create(
{'name': 'Size'})
self.attr_val_small = self.env['product.attribute.value'].create(
{'attribute_id': self.attr_size.id,
'name': 'Small',
}
)
self.attr_val_med = self.env['product.attribute.value'].create(
{'attribute_id': self.attr_size.id,
'name': 'Medium',
}
)
self.attr_val_large = self.env['product.attribute.value'].create(
{'attribute_id': self.attr_size.id,
'name': 'Large (Green)',
}
)
domain_small_med = self.env['product.config.domain'].create(
{'name': 'Small/Med',
'domain_line_ids': [
(0, 0, {'attribute_id': self.attr_size.id,
'condition': 'in',
'operator': 'and',
'value_ids':
[(6, 0,
[self.attr_val_small.id, self.attr_val_med.id]
)]
}),
],
}
)
self.attr_colour = self.env['product.attribute'].create(
{'name': 'Colour'})
self.attr_val_blue = self.env['product.attribute.value'].create(
{'attribute_id': self.attr_colour.id,
'name': 'Blue',
}
)
self.attr_val_red = self.env['product.attribute.value'].create(
{'attribute_id': self.attr_colour.id,
'name': 'Red',
}
)
self.product_temp = self.env['product.template'].create(
{'name': 'Config Product',
'config_ok': True,
'type': 'product',
'categ_id': self.env['ir.model.data'].xmlid_to_res_id(
'product.product_category_5'
),
'attribute_line_ids': [
(0, 0, {'attribute_id': self.attr_size.id,
'value_ids': [
(6, 0, self.attr_size.value_ids.ids),
],
'required': True,
}),
(0, 0, {'attribute_id': self.attr_colour.id,
'value_ids': [
(6, 0, self.attr_colour.value_ids.ids),
],
'required': False,
})
],
}
)
colour_line = self.product_temp.attribute_line_ids.filtered(
lambda a: a.attribute_id == self.attr_colour)
self.env['product.config.line'].create({
'product_tmpl_id': self.product_temp.id,
'attribute_line_id': colour_line.id,
'value_ids': [(6, 0, self.attr_colour.value_ids.ids)],
'domain_id': domain_small_med.id,
})

def test_configurations_option_or_not_reqd(self):
# Start a new configuration wizard
wizard_obj = self.env['product.configurator'].with_context({
'active_model': 'sale.order',
'active_id': self.so.id
# 'default_order_id': self.so.id
})

wizard = wizard_obj.create({'product_tmpl_id': self.product_temp.id})
wizard.action_next_step()

dynamic_fields = {}
for attribute_line in self.product_temp.attribute_line_ids:
field_name = '%s%s' % (
wizard.field_prefix,
attribute_line.attribute_id.id
)
dynamic_fields[field_name] = [] if attribute_line.multi else False
field_name_colour = '%s%s' % (
wizard.field_prefix,
self.attr_colour.id
)
invisible_name_colour = '%s%s' % (
wizard.invisible_field_prefix,
self.attr_colour.id
)
invisible_name_size = '%s%s' % (
wizard.invisible_field_prefix,
self.attr_size.id
)

# Define small without colour specified
self.wizard_write_proceed(wizard, [self.attr_val_small])
new_variant = self.product_temp.product_variant_ids
self.assertTrue(len(new_variant) == 1 and
set(new_variant.attribute_value_ids.ids) ==
set([self.attr_val_small.id]),
"Wizard did not accurately create a variant with "
"optional value undefined")
config_variants = self.product_temp.product_variant_ids

order_line = self.so.order_line.filtered(
lambda l: l.product_id.config_ok
)

# Redefine to medium without colour
self.do_reconfigure(order_line, [self.attr_val_med])
new_variant = self.product_temp.product_variant_ids - config_variants
self.assertTrue(len(new_variant) == 1 and
set(new_variant.attribute_value_ids.ids) ==
set([self.attr_val_med.id]),
"Wizard did not accurately reconfigure a variant with "
"optional value undefined")
config_variants = self.product_temp.product_variant_ids

# Redefine to medium blue
self.do_reconfigure(order_line, [self.attr_val_blue])
new_variant = self.product_temp.product_variant_ids - config_variants
self.assertTrue(len(new_variant) == 1 and
set(new_variant.attribute_value_ids.ids) ==
set([self.attr_val_med.id, self.attr_val_blue.id]),
"Wizard did not accurately reconfigure a variant with "
"to add an optional value")
config_variants = self.product_temp.product_variant_ids

# Redefine to large - should remove colour, as this is invalid
reconfig_action = order_line.reconfigure_product()
wizard = self.env['product.configurator'].browse(
reconfig_action.get('res_id')
)
attr_large_dict = self.get_wizard_write_dict(wizard,
[self.attr_val_large])
attr_blue_dict = self.get_wizard_write_dict(wizard,
[self.attr_val_blue])
oc_vals = dynamic_fields.copy()
oc_vals.update({'id': wizard.id})
oc_vals.update(dict(attr_blue_dict, **attr_large_dict))
oc_result = wizard.onchange(
oc_vals,
attr_large_dict.keys()[0],
{}
)
self.assertTrue(field_name_colour in oc_result['value'] and
not oc_result['value'][field_name_colour],
"Colour should have been cleared by wizard"
)
self.assertTrue(invisible_name_colour in oc_result['value'] and
not oc_result['value'][invisible_name_colour],
"Invisible Attribute Colour should have been "
"cleared by wizard"
)
self.assertTrue(invisible_name_size in oc_result['value'] and
oc_result['value'][invisible_name_size] ==
self.attr_val_large.id,
"Invisible Attribute Size should have been set "
"by wizard"
)
vals = self.get_wizard_write_dict(wizard, [self.attr_val_large],
remove_values=[self.attr_val_blue])
wizard.write(vals)
wizard.action_next_step()
if wizard.exists():
while wizard.action_next_step():
pass
new_variant = self.product_temp.product_variant_ids - config_variants
self.assertTrue(len(new_variant) == 1 and
set(new_variant.attribute_value_ids.ids) ==
set([self.attr_val_large.id]),
"Wizard did not accurately reconfigure a variant with "
"to remove invalid attribute")
Loading