Debian Codegraph @

addons/website_sale_collect/tests/test_sale_order.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.exceptions import ValidationError
5 from odoo.tests import tagged
6
7 from odoo.addons.website_sale_collect.tests.common import ClickAndCollectCommon
8
9
10 @tagged('post_install', '-at_install')
11 class TestSaleOrder(ClickAndCollectCommon):
12
13 @classmethod
14 def setUpClass(cls):
15 super().setUpClass()
16 cls.product_2 = cls._create_product()
17
18 def test_warehouse_is_updated_when_changing_delivery_line(self):
19 self.warehouse_2 = self._create_warehouse()
20 self.website.warehouse_id = self.warehouse
21 so = self._create_in_store_delivery_order(warehouse_id=self.warehouse_2.id)
22 so._set_delivery_method(self.free_delivery)
23 self.assertEqual(so.warehouse_id, self.warehouse)
24
25 def test_setting_pickup_location_assigns_warehouse(self):
26 so = self._create_in_store_delivery_order()
27 so._set_pickup_location('{"id":' + str(self.warehouse.id) + '}')
28 self.assertEqual(so.warehouse_id, self.warehouse)
29
30 def test_warehouse_is_not_reset_on_public_user_checkout(self):
31 warehouse_2 = self._create_warehouse()
32 so = self._create_in_store_delivery_order(partner_id=self.public_user.id)
33 so._set_pickup_location('{"id":' + str(warehouse_2.id) + '}')
34 # change the partner_id as would happen in a checkout
35 so.partner_id = self.partner.id
36 self.assertEqual(so.warehouse_id, warehouse_2)
37
38 def test_warehouse_is_computed_based_on_pickup_location(self):
39 warehouse_2 = self._create_warehouse()
40 so = self._create_in_store_delivery_order(pickup_location_data={'id': warehouse_2.id})
41 self.assertEqual(so.warehouse_id, warehouse_2)
42
43 def test_fiscal_position_id_is_computed_from_pickup_location_partner(self):
44 fp_be = self.env['account.fiscal.position'].create({
45 'name': "Test BE fiscal position",
46 'country_id': self.country_be.id,
47 'auto_apply': True,
48 })
49 self.default_partner.country_id = self.country_us
50 self.warehouse.partner_id.country_id = self.country_be
51 so = self._create_in_store_delivery_order(
52 partner_shipping_id=self.default_partner.id,
53 pickup_location_data={'id': self.warehouse.id},
54 )
55 self.assertEqual(so.fiscal_position_id, fp_be)
56
57 def test_setting_pickup_location_assigns_correct_fiscal_position(self):
58 fp_be = self.env['account.fiscal.position'].create({
59 'name': "Test BE fiscal position",
60 'country_id': self.country_be.id,
61 'auto_apply': True,
62 })
63 so = self._create_in_store_delivery_order()
64 self.default_partner.country_id = self.country_be
65 warehouse = self._create_warehouse()
66 warehouse.partner_id = self.default_partner
67 so._set_pickup_location('{"id":' + str(warehouse.id) + '}')
68 self.assertEqual(so.fiscal_position_id, fp_be)
69
70 def test_selecting_not_in_store_dm_resets_fiscal_position(self):
71 fp_us = self.env['account.fiscal.position'].create({
72 'name': "Test US fiscal position",
73 'country_id': self.country_us.id,
74 'auto_apply': True,
75 })
76 so = self._create_in_store_delivery_order()
77 so.fiscal_position_id = fp_us
78 so._set_delivery_method(self.free_delivery)
79 self.assertNotEqual(so.fiscal_position_id, fp_us)
80
81 def test_free_qty_calculated_from_order_wh_if_dm_is_in_store(self):
82 self.warehouse_2 = self._create_warehouse()
83 self.website.warehouse_id = self.warehouse_2
84 so = self._create_in_store_delivery_order()
85 so.warehouse_id = self.warehouse
86 free_qty = so._get_free_qty(self.storable_product)
87 self.assertEqual(free_qty, 10)
88
89 def test_prevent_buying_out_of_stock_products(self):
90 cart = self._create_in_store_delivery_order(order_line=[Command.create({
91 'product_id': self.product_2.id,
92 'product_uom_qty': 5.0,
93 })])
94 cart.warehouse_id = self.warehouse
95 with self.assertRaises(ValidationError):
96 cart._check_cart_is_ready_to_be_paid()
97
98 def test_product_in_stock_is_available(self):
99 cart = self._create_in_store_delivery_order(
100 order_line=[
101 Command.create({
102 'product_id': self.storable_product.id,
103 'product_uom_qty': 5.0,
104 })
105 ]
106 )
107 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
108 self.assertFalse(insufficient_stock_data)
109
110 def test_product_out_of_stock_continue_selling_is_available(self):
111 self.product_2.allow_out_of_stock_order = True
112 cart = self._create_in_store_delivery_order(
113 order_line=[
114 Command.create(
115 {
116 'product_id': self.product_2.id,
117 'product_uom_qty': 5.0,
118 }
119 )
120 ]
121 )
122 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
123 self.assertFalse(insufficient_stock_data)
124
125 def test_product_insufficient_stock_is_unavailable(self):
126 cart = self._create_in_store_delivery_order(
127 order_line=[
128 Command.create({
129 'product_id': self.storable_product.id,
130 'product_uom_qty': 15.0,
131 })
132 ]
133 )
134 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
135 self.assertEqual(insufficient_stock_data[cart.order_line], 10)
136
137 def test_insufficient_stock_with_mixed_uom_order_lines(self):
138 """Test that the insufficient stock is correctly computed when the order lines
139 use different UoMs."""
140 pack_of_6_id = self.ref('uom.product_uom_pack_6')
141 # 1 pack of 6 + 5 units = 11 units in the cart
142 cart = self._create_in_store_delivery_order(
143 order_line=[
144 Command.create(
145 {
146 'product_id': self.storable_product.id,
147 'product_uom_qty': 1.0,
148 'product_uom_id': pack_of_6_id,
149 }
150 ),
151 Command.create(
152 {
153 'product_id': self.storable_product.id,
154 'product_uom_qty': 5.0,
155 'product_uom_id': self.storable_product.uom_id.id,
156 }
157 ),
158 ]
159 )
160 # 10 units available, 11 requested, so 1 unit short
161 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
162 ol_unit = cart.order_line.filtered(
163 lambda l: l.product_uom_id == self.storable_product.uom_id
164 )
165 # only 4 units are available for the second order line instead of 5
166 self.assertEqual(insufficient_stock_data[ol_unit], 4)
167
168 def test_product_in_stock_with_mixed_uom_order_lines_is_available(self):
169 """Test that if there is enough stock for all order lines the insufficient stock is
170 empty."""
171 pack_of_6_id = self.ref('uom.product_uom_pack_6')
172 # 1 pack of 6 + 4 units = 10 units in the cart
173 cart = self._create_in_store_delivery_order(order_line=[
174 Command.create({
175 'product_id': self.storable_product.id,
176 'product_uom_qty': 4.0,
177 'product_uom_id': self.storable_product.uom_id.id,
178 }),
179 Command.create({
180 'product_id': self.storable_product.id,
181 'product_uom_qty': 1.0,
182 'product_uom_id': pack_of_6_id,
183 }),
184 ])
185 # 10 units available, 10 requested
186 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
187 self.assertFalse(insufficient_stock_data)
188
189 def test_out_of_stock_product_is_unavailable(self):
190 cart = self._create_in_store_delivery_order(
191 order_line=[
192 Command.create({
193 'product_id': self.product_2.id,
194 'product_uom_qty': 5.0,
195 }),
196 ]
197 )
198 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse.id)
199 self.assertIn(cart.order_line, insufficient_stock_data)
200
201 def test_product_in_different_warehouse_is_unavailable(self):
202 self.warehouse_2 = self._create_warehouse()
203 cart = self._create_in_store_delivery_order(
204 order_line=[
205 Command.create({
206 'product_id': self.storable_product.id,
207 'product_uom_qty': 5.0,
208 })
209 ]
210 )
211 insufficient_stock_data = cart._get_insufficient_stock_data(self.warehouse_2.id)
212 self.assertIn(cart.order_line, insufficient_stock_data)