Debian Codegraph @

addons/website_sale_comparison/tests/test_website_sale_comparison.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import logging
4 from collections import OrderedDict
5
6 from lxml import etree
7
8 from odoo.fields import Command
9 from odoo.tests import HttpCase, TransactionCase, loaded_demo_data, tagged
10
11
12 _logger = logging.getLogger(__name__)
13
14
15 @tagged('-at_install', 'post_install')
16 class TestWebsiteSaleComparison(TransactionCase):
17
18 def test_01_website_sale_comparison_remove(self):
19 """ This tour makes sure the product page still works after the module
20 `website_sale_comparison` has been removed.
21
22 Technically it tests the removal of copied views by the base method
23 `_remove_copied_views`. The problematic view that has to be removed is
24 `product_attributes_body` because it has a reference to `add_to_compare`.
25 """
26 Website0 = self.env['website'].with_context(website_id=None)
27 Website1 = self.env['website'].with_context(website_id=1)
28
29 # Create a generic inherited view, with a key not starting with
30 # `website_sale_comparison` otherwise the unlink will work just based on
31 # the key, but we want to test also for `MODULE_UNINSTALL_FLAG`.
32 product_attributes_body = Website0.viewref('website_sale_comparison.product_attributes_body')
33 test_view_key = 'my_test.my_key'
34 self.env['ir.ui.view'].with_context(website_id=None).create({
35 'name': 'test inherited view',
36 'key': test_view_key,
37 'inherit_id': product_attributes_body.id,
38 'arch': '<div/>',
39 })
40
41 # Retrieve the generic view
42 product = Website0.viewref('website_sale.product')
43 # Trigger COW to create specific views of the whole tree
44 product.with_context(website_id=1).write({'name': 'Trigger COW'})
45
46 # Verify initial state: the specific views exist
47 self.assertEqual(Website1.viewref('website_sale.product').website_id.id, 1)
48 self.assertEqual(Website1.viewref('website_sale_comparison.product_attributes_body').website_id.id, 1)
49 self.assertEqual(Website1.viewref(test_view_key).website_id.id, 1)
50
51 # Remove the module (use `module_uninstall` because it is enough to test
52 # what we want here, no need/can't use `button_immediate_uninstall`
53 # because it would commit the test transaction)
54 website_sale_comparison = self.env['ir.module.module'].search([('name', '=', 'website_sale_comparison')])
55 website_sale_comparison.module_uninstall()
56
57 # Check that the generic view is correctly removed
58 self.assertFalse(Website0.viewref('website_sale_comparison.product_attributes_body', raise_if_not_found=False))
59 # Check that the specific view is correctly removed
60 self.assertFalse(Website1.viewref('website_sale_comparison.product_attributes_body', raise_if_not_found=False))
61
62 # Check that the generic inherited view is correctly removed
63 self.assertFalse(Website0.viewref(test_view_key, raise_if_not_found=False))
64 # Check that the specific inherited view is correctly removed
65 self.assertFalse(Website1.viewref(test_view_key, raise_if_not_found=False))
66
67
68 @tagged('post_install', '-at_install')
69 class TestWebsiteSaleComparisonUi(HttpCase):
70
71 @classmethod
72 def setUpClass(cls):
73 super().setUpClass()
74
75 cls.attribute_varieties = cls.env['product.attribute'].create({
76 'name': 'Grape Varieties',
77 'sequence': 2,
78 'value_ids': [
79 Command.create({
80 'name': n,
81 'sequence': i,
82 }) for i, n in enumerate(['Cabernet Sauvignon', 'Merlot', 'Cabernet Franc', 'Petit Verdot'])
83 ],
84 })
85 cls.attribute_vintage = cls.env['product.attribute'].create({
86 'name': 'Vintage',
87 'sequence': 1,
88 'value_ids': [
89 Command.create({
90 'name': n,
91 'sequence': i,
92 }) for i, n in enumerate(['2018', '2017', '2016', '2015'])
93 ],
94 })
95 cls.values_varieties = cls.attribute_varieties.value_ids
96 cls.values_vintage = cls.attribute_vintage.value_ids
97 cls.template_margaux = cls.env['product.template'].create({
98 'name': "Château Margaux",
99 'website_published': True,
100 'list_price': 0,
101 'attribute_line_ids': [
102 Command.create({
103 'attribute_id': cls.attribute_vintage.id,
104 'value_ids': [Command.set(cls.values_vintage.ids)]
105 })
106 ]
107 })
108 cls.attribute_line_vintage = cls.template_margaux.attribute_line_ids
109 cls.attribute_line_varieties = cls.env['product.template.attribute.line'].create([{
110 'product_tmpl_id': cls.template_margaux.id,
111 'attribute_id': cls.attribute_varieties.id,
112 'value_ids': [(6, 0, v.ids)],
113 } for v in cls.values_varieties])
114 cls.variants_margaux = cls.template_margaux._get_possible_variants_sorted()
115
116 for variant, price in zip(cls.variants_margaux, [487.32, 394.05, 532.44, 1047.84]):
117 variant.product_template_attribute_value_ids.filtered(lambda ptav: ptav.attribute_id == cls.attribute_vintage).price_extra = price
118
119 def test_01_admin_tour_product_comparison(self):
120 attribute = self.env['product.attribute'].create({
121 'name': 'Color',
122 'sequence': 10,
123 'display_type': 'color',
124 'value_ids': [
125 Command.create({
126 'name': 'Red',
127 }),
128 Command.create({
129 'name': 'Pink',
130 }),
131 Command.create({
132 'name': 'Blue'
133 })
134 ]
135 })
136 self.env['product.template'].create([{
137 'name': 'Color T-Shirt',
138 'list_price': 20.0,
139 'website_sequence': 1,
140 'is_published': True,
141 'type': 'service',
142 'invoice_policy': 'delivery',
143 'attribute_line_ids': [
144 Command.create({
145 'attribute_id': attribute.id,
146 'value_ids': attribute.value_ids,
147 })
148 ]
149 }, {
150 'name': 'Color Pants',
151 'list_price': 20.0,
152 'website_sequence': 1,
153 'is_published': True,
154 'type': 'service',
155 'invoice_policy': 'delivery',
156 'attribute_line_ids': [
157 Command.create({
158 'attribute_id': attribute.id,
159 'value_ids': attribute.value_ids,
160 })
161 ]
162 }, {
163 'name': 'Color Shoes',
164 'list_price': 20.0,
165 'website_sequence': 1,
166 'is_published': True,
167 'type': 'service',
168 'invoice_policy': 'delivery',
169 'attribute_line_ids': [
170 Command.create({
171 'attribute_id': attribute.id,
172 'value_ids': attribute.value_ids,
173 })
174 ]
175 }])
176 self.start_tour("/", 'product_comparison', login='admin')
177
178 def test_02_attribute_multiple_lines(self):
179 # Case product page with "Product attributes table" disabled (website_sale standard case)
180 self.env['website'].viewref('website_sale_comparison.product_attributes_body').active = False
181 res = self.url_open('/shop/%d' % self.template_margaux.id)
182 self.assertEqual(res.status_code, 200)
183 root = etree.fromstring(res.content, etree.HTMLParser())
184
185 tr_varieties_simple_att = root.xpath('//div[@id="product_attributes_simple"]//tr')[0]
186 text = etree.tostring(tr_varieties_simple_att, encoding='unicode', method='text')
187 self.assertEqual(text.replace(' ', '').replace('\n', ''), "GrapeVarieties:CabernetSauvignon,Merlot,CabernetFranc,PetitVerdot")
188
189 # Case product page with "Product attributes table" enabled
190 self.env['website'].viewref('website_sale_comparison.product_attributes_body').active = True
191 res = self.url_open('/shop/%d' % self.template_margaux.id)
192 self.assertEqual(res.status_code, 200)
193 root = etree.fromstring(res.content, etree.HTMLParser())
194
195 tr_vintage = root.xpath('//div[@id="product_specifications"]//tr')[0]
196 text_vintage = etree.tostring(tr_vintage, encoding='unicode', method='text')
197 self.assertEqual(text_vintage.replace(' ', '').replace('\n', ''), "Vintage2018,2017,2016,2015")
198
199 tr_varieties = root.xpath('//div[@id="product_specifications"]//tr')[1]
200 text_varieties = etree.tostring(tr_varieties, encoding='unicode', method='text')
201 self.assertEqual(text_varieties.replace(' ', '').replace('\n', ''), "GrapeVarietiesCabernetSauvignon,Merlot,CabernetFranc,PetitVerdot")
202
203 # Case compare page
204 res = self.url_open('/shop/compare?products=%s' % ','.join(str(id) for id in self.variants_margaux.ids))
205 self.assertEqual(res.status_code, 200)
206 root = etree.fromstring(res.content, etree.HTMLParser())
207
208 table = root.xpath('//div[@id="o_comparelist_table"]')[0]
209
210 products = table.xpath('//div[@id="o_comparelist_product_name"]/a/h6')
211 self.assertEqual(len(products), 4)
212 for product, name in zip(products, ['ChâteauMargaux(2018)', 'ChâteauMargaux(2017)', 'ChâteauMargaux(2016)', 'ChâteauMargaux(2015)']):
213 text = etree.tostring(product, encoding='unicode', method='text')
214 self.assertEqual(text.replace(' ', '').replace('\n', ''), name)
215
216 attribute_vintage = table.xpath('//div[@id="o_comparelist_attribute"]')[0]
217 text_vintage = etree.tostring(attribute_vintage, encoding='unicode', method='text')
218 self.assertEqual(text_vintage.replace(' ', '').replace('\n', ''), "Vintage2018")
219
220 attribute_varieties = table.xpath('//div[@id="o_comparelist_attribute"]')[4]
221 text_varieties = etree.tostring(attribute_varieties, encoding='unicode', method='text')
222 self.assertEqual(text_varieties.replace(' ', '').replace('\n', ''), "GrapeVarietiesCabernetSauvignon,Merlot,CabernetFranc,PetitVerdot")
223
224 def test_03_category_order(self):
225 """Test that categories are shown in the correct order when the
226 attributes are in a different order."""
227 category_vintage = self.env['product.attribute.category'].create({
228 'name': 'Vintage',
229 'sequence': 2,
230 })
231 category_varieties = self.env['product.attribute.category'].create({
232 'name': 'Varieties',
233 'sequence': 1,
234 })
235 self.attribute_vintage.category_id = category_vintage
236 self.attribute_varieties.category_id = category_varieties
237
238 prep_categories = self.template_margaux.valid_product_template_attribute_line_ids._prepare_categories_for_display()
239 self.assertEqual(prep_categories, OrderedDict([
240 (category_varieties, self.attribute_line_varieties),
241 (category_vintage, self.attribute_line_vintage),
242 ]))
243
244 variant_margaux = self.template_margaux.product_variant_id
245 variant_ptavs = variant_margaux.product_template_attribute_value_ids
246
247 prep_categories = self.variants_margaux[0]._prepare_categories_for_display()
248 self.assertEqual(prep_categories, OrderedDict([
249 (category_varieties, OrderedDict([
250 (self.attribute_varieties, OrderedDict([
251 (variant_margaux, variant_ptavs.filtered(
252 lambda ptav: ptav.attribute_id == self.attribute_varieties
253 ))
254 ]))
255 ])),
256 (category_vintage, OrderedDict([
257 (self.attribute_vintage, OrderedDict([
258 (variant_margaux, variant_ptavs.filtered(
259 lambda ptav: ptav.attribute_id == self.attribute_vintage
260 ))
261 ]))
262 ])),
263 ]))