Debian Codegraph @

addons/point_of_sale/tests/test_point_of_sale.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from odoo.fields import Command
4 from odoo.tests.common import TransactionCase
5 from odoo.exceptions import UserError
6
7
8 class TestPointOfSale(TransactionCase):
9 def setUp(self):
10 super(TestPointOfSale, self).setUp()
11
12 # ignore pre-existing pricelists for the purpose of this test
13 self.env["product.pricelist"].search([]).write({"active": False})
14
15 self.currency = self.env.ref("base.USD")
16 self.company1 = self.env["res.company"].create({
17 "name": "company 1",
18 "currency_id": self.currency.id
19 })
20 self.company2 = self.env["res.company"].create({
21 "name": "company 2",
22 "currency_id": self.currency.id
23 })
24 self.company2_pricelist = self.env["product.pricelist"].create({
25 "name": "company 2 pricelist",
26 "currency_id": self.currency.id,
27 "company_id": self.company2.id,
28 "sequence": 1, # force this pricelist to be first
29 })
30 self.bank_journal = self.env['account.journal'].create({
31 'name': 'Bank',
32 'type': 'bank',
33 'company_id': self.company1.id,
34 'code': 'BNK',
35 'sequence': 11,
36 })
37
38 self.env.user.company_id = self.company1
39
40 def test_no_default_pricelist(self):
41 """ Verify that the default pricelist isn't automatically set in the config """
42 company1_pricelist = self.env["product.pricelist"].create({
43 "name": "company 1 pricelist",
44 "currency_id": self.currency.id,
45 "company_id": self.company1.id,
46 "sequence": 2,
47 })
48
49 # make sure this doesn't pick a pricelist as default
50 new_config = self.env["pos.config"].create({
51 "name": "usd config", "available_pricelist_ids": [(6, 0, [company1_pricelist.id])]
52 })
53
54 self.assertEqual(new_config.pricelist_id, self.env['product.pricelist'],
55 "POS config incorrectly has pricelist %s" % new_config.pricelist_id.display_name)
56
57 def test_product_combo_variants(self):
58 # Create product and combo
59 product = self.env['product.product'].create({
60 'name': 'Test Product 1',
61 'list_price': 100,
62 'taxes_id': False,
63 'available_in_pos': True,
64 })
65
66 product_combo = self.env["product.combo"].create(
67 {
68 "name": "Product combo",
69 "combo_item_ids": [
70 Command.create({
71 "product_id": product.id,
72 "extra_price": 0,
73 }),
74 ],
75 }
76 )
77 # Add attribute and values, simulating variant creation
78 size_attribute = self.env['product.attribute'].create({'name': 'Size'})
79 attribute_value_1 = self.env['product.attribute.value'].create({'name': 'Large', 'attribute_id': size_attribute.id})
80 attribute_value_2 = self.env['product.attribute.value'].create({'name': 'Small', 'attribute_id': size_attribute.id})
81 original_product_id = product.id
82 product_template = product.product_tmpl_id
83 product.product_tmpl_id.with_context(create_product_product=True).write({
84 'attribute_line_ids': [(0, 0, {
85 'attribute_id': size_attribute.id,
86 'value_ids': [(6, 0, [attribute_value_1.id, attribute_value_2.id])],
87 })],
88 })
89 # Check that original product should not be in combo anymore (replace by variants)
90 self.assertTrue(original_product_id not in product_combo.combo_item_ids.mapped('product_id').ids, "Original product should not be in combo")
91
92 def test_pos_bill_digits(self):
93 coin = self.env["pos.bill"].create({
94 "name": "0.005 not rounded",
95 "value": 0.005
96 })
97 self.assertEqual(coin.value, 0.005)
98
99 def test_pos_config_creates_warehouse(self):
100 warehouse = self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)])
101 if warehouse:
102 warehouse.write({'active': False, 'name': 'Archived ' + warehouse[0].name})
103 pos_config = self.env['pos.config'].create({
104 'name': 'Shop',
105 'module_pos_restaurant': False,
106 })
107 self.assertEqual(pos_config.warehouse_id.code, 'Sho')
108
109 def test_session_filter_local_data(self):
110 product1 = self.env['product.template'].create({
111 'name': 'product1'
112 })
113 product2 = self.env['product.template'].create({
114 'name': 'product2'
115 })
116 product3 = self.env['product.template'].create({
117 'name': 'product3'
118 })
119 config = self.env["pos.config"].create({
120 "name": "shop"
121 })
122 session = self.env['pos.session'].create({'name': 'Test Session', 'config_id': config.id})
123
124 # Delete one product and archive another one
125 products_to_display = [product1.id, product2.id, product3.id]
126 product1.write({'active': False})
127 product2.unlink()
128 models_to_filter = {'product.template': products_to_display}
129 products_to_display = list(set(products_to_display) - set(session.filter_local_data(models_to_filter)['product.template']))
130 self.assertEqual(products_to_display, [product3.id])
131
132 # No change
133 product4 = self.env['product.template'].create({
134 'name': 'product4'
135 })
136 products_to_display = [product3.id, product4.id]
137 models_to_filter = {'product.template': products_to_display}
138 products_to_display = list(set(products_to_display) - set(session.filter_local_data(models_to_filter)['product.template']))
139 self.assertEqual(sorted(products_to_display), sorted([product3.id, product4.id]))
140
141 # Delete all products
142 products_to_display = [product3.id, product4.id]
143 product3.unlink()
144 product4.unlink()
145 models_to_filter = {'product.template': products_to_display}
146 products_to_display = list(set(products_to_display) - set(session.filter_local_data(models_to_filter)['product.template']))
147 self.assertEqual(products_to_display, [])
148
149 # Cannot archive config while session is active
150 with self.assertRaises(UserError):
151 config.write({'active': False})