Debian Codegraph @

addons/point_of_sale/static/tests/unit/related_models/orm_serialization.test.js

raw
1 import { expect, test } from "@odoo/hoot";
2 import { getRelatedModelsInstance } from "../data/get_model_definitions";
3 import { makeMockServer } from "@web/../tests/web_test_helpers";
4 import { definePosModels } from "../data/generate_model_definitions";
5
6 definePosModels();
7
8 test("basic", async () => {
9 await makeMockServer();
10 const models = getRelatedModelsInstance(false);
11 const order = models["pos.order"].create({});
12 const line1 = models["pos.order.line"].create({ order_id: order, qty: 1 });
13 const line2 = models["pos.order.line"].create({ order_id: order, qty: 2 });
14 order.amount_total = 10;
15
16 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
17 const result = models.serializeForORM(order);
18 {
19 expect(keepCommandResult).toEqual(result);
20 expect(result.uuid).not.toBeEmpty();
21 expect(result.amount_total).toBe(10);
22 expect(result.lines.length).toBe(2);
23
24 expect(result.lines[0][0]).toBe(0);
25 expect(result.lines[0][1]).toBe(0);
26 expect(result.lines[0][2].id).toBe(undefined);
27 expect(result.lines[0][2].uuid).toBe(line1.uuid);
28 expect(result.lines[0][2].qty).toBe(1);
29
30 expect(result.lines[1][0]).toBe(0);
31 expect(result.lines[1][1]).toBe(0);
32 expect(result.lines[1][2].id).toBe(undefined);
33 expect(result.lines[1][2].uuid).toBe(line2.uuid);
34 expect(result.lines[1][2].qty).toBe(2);
35
36 expect(result.relations_uuid_mapping).toBe(undefined);
37 }
38
39 // Server results with ids
40 models.connectNewData({
41 "pos.order": [{ ...order.raw, id: 1, lines: [11, 12] }],
42 "pos.order.line": [
43 { ...line1.raw, id: 11, order_id: 1 },
44 { ...line2.raw, id: 12, order_id: 1 },
45 ],
46 });
47
48 line1.qty = 99;
49 {
50 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
51 const result = models.serializeForORM(order);
52 expect(keepCommandResult).toEqual(result);
53 expect(result.lines.length).toBe(1);
54 expect(result.lines[0][0]).toBe(1);
55 expect(result.lines[0][1]).toBe(11);
56 expect(result.lines[0][2].qty).toBe(99);
57 }
58
59 // Delete line
60 line1.delete();
61 {
62 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
63 const result = models.serializeForORM(order);
64 expect(keepCommandResult).toEqual(result);
65 expect(result.lines.length).toBe(1);
66 expect(result.lines[0][0]).toBe(3);
67 expect(result.lines[0][1]).toBe(11);
68 }
69 });
70
71 test("serialization of non-dynamic model relationships", async () => {
72 await makeMockServer();
73 const models = getRelatedModelsInstance(false);
74 const order = models["pos.order"].create({});
75
76 const tax1 = models["account.tax"].create({ id: 99 });
77 const tax2 = models["account.tax"].create({ id: 999 });
78 const line = models["pos.order.line"].create({ order_id: order, qty: 1 });
79 line.tax_ids = [["link", tax1, tax2]];
80 {
81 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
82 const result = models.serializeForORM(order);
83 expect(keepCommandResult).toEqual(result);
84 expect(result.lines[0][2].tax_ids).toEqual([99, 999]);
85 }
86
87 const tax3 = models["account.tax"].create({ id: 9999 });
88 line.tax_ids = [["link", tax3]];
89 {
90 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
91 const result = models.serializeForORM(order);
92 expect(keepCommandResult).toEqual(result);
93 expect(result.lines[0][2].tax_ids).toEqual([99, 999, 9999]);
94 }
95
96 line.tax_ids = [["unlink", tax3, tax2]];
97 {
98 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
99 const result = models.serializeForORM(order);
100 expect(keepCommandResult).toEqual(result);
101 expect(result.lines[0][2].tax_ids).toEqual([99]);
102 }
103
104 line.tax_ids = [];
105 {
106 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
107 const result = models.serializeForORM(order);
108 expect(keepCommandResult).toEqual(result);
109 expect(result.lines[0][2].tax_ids).toEqual([]);
110 }
111 });
112
113 test("serialization of dynamic model without uuid", async () => {
114 await makeMockServer();
115 const models = getRelatedModelsInstance(false);
116 let order = models["pos.order"].create({});
117 const orderUUID = order.uuid;
118
119 let line1 = models["pos.order.line"].create({
120 order_id: order,
121 });
122 const line1UUID = line1.uuid;
123
124 models["pos.pack.operation.lot"].create({ pos_order_line_id: line1, lot_name: "lot1" });
125 {
126 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
127 const result = models.serializeForORM(order);
128 expect(keepCommandResult).toEqual(result);
129 expect(result.lines.length).toBe(1);
130 const pack_lot_ids = result.lines[0][2].pack_lot_ids;
131 expect(pack_lot_ids.length).toBe(1);
132 expect(pack_lot_ids[0][0]).toBe(0);
133 expect(pack_lot_ids[0][0]).toBe(0);
134 expect(pack_lot_ids[0][2]).toEqual({ lot_name: "lot1", write_date: false });
135 }
136
137 models.connectNewData({
138 "pos.order": [
139 {
140 id: 1,
141 lines: [11],
142 uuid: order.uuid,
143 },
144 ],
145 "pos.order.line": [
146 {
147 id: 11,
148 pack_lot_ids: [99],
149 uuid: line1UUID,
150 },
151 ],
152 "pos.pack.operation.lot": [
153 {
154 id: 99,
155 lot_name: "lot1",
156 },
157 ],
158 });
159
160 order = models["pos.order"].getBy("uuid", orderUUID);
161 line1 = models["pos.order.line"].getBy("uuid", line1UUID);
162
163 models["pos.pack.operation.lot"].create({ pos_order_line_id: line1, lot_name: "lot2" });
164 {
165 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
166 const result = models.serializeForORM(order);
167 expect(keepCommandResult).toEqual(result);
168 expect(result.lines.length).toBe(1);
169 const pack_lot_ids = result.lines[0][2].pack_lot_ids;
170 expect(pack_lot_ids.length).toBe(1);
171 expect(pack_lot_ids[0][0]).toBe(0);
172 expect(pack_lot_ids[0][1]).toBe(0);
173 expect(pack_lot_ids[0][2].lot_name).toEqual("lot2");
174 }
175
176 models.connectNewData({
177 "pos.order": [
178 {
179 id: 1,
180 lines: [11],
181 uuid: order.uuid,
182 },
183 ],
184 "pos.order.line": [
185 {
186 id: 11,
187 order_id: 1,
188 pack_lot_ids: [99, 999],
189 uuid: line1UUID,
190 },
191 ],
192 "pos.pack.operation.lot": [
193 {
194 id: 99,
195 pos_order_line_id: 11,
196 lot_name: "lot1",
197 },
198 {
199 id: 999,
200 pos_order_line_id: 11,
201 lot_name: "lot2",
202 },
203 ],
204 });
205 order = models["pos.order"].getBy("uuid", orderUUID);
206 order.lines[0].pack_lot_ids[1].delete();
207 order.lines[0].pack_lot_ids[0].delete();
208 {
209 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
210 const result = models.serializeForORM(order);
211 expect(keepCommandResult).toEqual(result);
212 expect(result.lines.length).toBe(1);
213 const pack_lot_ids = result.lines[0][2].pack_lot_ids;
214 expect(pack_lot_ids.length).toBe(2);
215 expect(pack_lot_ids).toEqual([
216 [3, 999],
217 [3, 99],
218 ]);
219 }
220 });
221
222 test("nested lines relationship", async () => {
223 await makeMockServer();
224 const models = getRelatedModelsInstance(false);
225 let order = models["pos.order"].create({});
226 let parentLine = models["pos.order.line"].create({ order_id: order });
227 let line1 = models["pos.order.line"].create({
228 order_id: order,
229 combo_parent_id: parentLine,
230 });
231 let line2 = models["pos.order.line"].create({
232 order_id: order,
233 combo_parent_id: parentLine,
234 });
235
236 {
237 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
238 const result = models.serializeForORM(order);
239 expect(keepCommandResult).toEqual(result);
240 expect(result.lines.length).toBe(3);
241 expect(result.lines[0][2].combo_line_ids).toBeEmpty();
242 expect(result.lines[0][2].combo_parent_id).toBeEmpty();
243 expect(result.lines[1][2].combo_line_ids).toBeEmpty();
244 expect(result.lines[1][2].combo_parent_id).toBeEmpty();
245 expect(result.lines[2][2].combo_line_ids).toBeEmpty();
246 expect(result.lines[2][2].combo_parent_id).toBeEmpty();
247
248 const { relations_uuid_mapping } = result;
249 expect(Object.keys(relations_uuid_mapping["pos.order.line"]).length).toBe(2);
250 expect(relations_uuid_mapping["pos.order.line"][line1.uuid]["combo_parent_id"]).toBe(
251 parentLine.uuid
252 );
253 expect(relations_uuid_mapping["pos.order.line"][line2.uuid]["combo_parent_id"]).toBe(
254 parentLine.uuid
255 );
256 }
257
258 models.connectNewData({
259 "pos.order": [
260 {
261 id: 1,
262 lines: [11, 111],
263 uuid: order.uuid,
264 },
265 ],
266 "pos.order.line": [
267 {
268 id: 1,
269 uuid: parentLine.uuid,
270 },
271 {
272 id: 11,
273 uuid: line1.uuid,
274 combo_parent_id: 1,
275 },
276 {
277 id: 111,
278 uuid: line2.uuid,
279 combo_parent_id: 1,
280 },
281 ],
282 });
283
284 // Update line: the uuid mapping must be present
285 order = models["pos.order"].getBy("uuid", order.uuid);
286 line1 = models["pos.order.line"].getBy("uuid", line1.uuid);
287 line2 = models["pos.order.line"].getBy("uuid", line2.uuid);
288 parentLine = models["pos.order.line"].getBy("uuid", parentLine.uuid);
289
290 line1.qty = 99;
291 {
292 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
293 const result = models.serializeForORM(order);
294 expect(keepCommandResult).toEqual(result);
295 expect(result.lines.length).toBe(1);
296 expect(result.lines[0][0]).toBe(1);
297 expect(result.lines[0][1]).toBe(11);
298 expect(result.lines[0][2].qty).toBe(99);
299 expect(result.relations_uuid_mapping).toBe(undefined);
300 }
301 });
302
303 test("recursive relationship with group of lines", async () => {
304 await makeMockServer();
305 const models = getRelatedModelsInstance(false);
306 let order = models["pos.order"].create({});
307 let line1 = models["pos.order.line"].create({
308 order_id: order,
309 });
310 let line2 = models["pos.order.line"].create({
311 order_id: order,
312 combo_parent_id: line1,
313 });
314 let line3 = models["pos.order.line"].create({
315 order_id: order,
316 combo_parent_id: line1,
317 });
318 models["pos.order.line"].create({
319 order_id: order,
320 combo_parent_id: line1,
321 });
322
323 expect(order.lines.length).toBe(4);
324 expect(order.lines[0].combo_line_ids.length).toBe(3);
325
326 {
327 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
328 const result = models.serializeForORM(order);
329 expect(keepCommandResult).toEqual(result);
330 expect(result.lines.length).toBe(4);
331 expect(result.lines[0][2].combo_parent_id).toBeEmpty();
332 expect(result.lines[1][2].combo_parent_id).toBeEmpty();
333
334 const { relations_uuid_mapping } = result;
335 expect(Object.keys(relations_uuid_mapping["pos.order.line"]).length).toBe(3);
336 expect(relations_uuid_mapping["pos.order.line"][line2.uuid]["combo_parent_id"]).toBe(
337 line1.uuid
338 );
339 expect(relations_uuid_mapping["pos.order.line"][line3.uuid]["combo_parent_id"]).toBe(
340 line1.uuid
341 );
342 }
343
344 models.connectNewData({
345 "pos.order": [
346 {
347 id: 1,
348 lines: [110, 111],
349 uuid: order.uuid,
350 },
351 ],
352 "pos.order.line": [
353 {
354 id: 1,
355 uuid: line1.uuid,
356 },
357 {
358 id: 110,
359 order_id: 1,
360 uuid: line2.uuid,
361 combo_parent_id: 1,
362 },
363 {
364 id: 111,
365 order_id: 1,
366 uuid: line3.uuid,
367 combo_parent_id: 1,
368 },
369 ],
370 });
371
372 order = models["pos.order"].getBy("uuid", order.uuid);
373 line1 = models["pos.order.line"].getBy("uuid", line1.uuid);
374 line2 = models["pos.order.line"].getBy("uuid", line2.uuid);
375 line3 = models["pos.order.line"].getBy("uuid", line3.uuid);
376
377 {
378 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
379 const result = models.serializeForORM(order);
380 expect(keepCommandResult).toEqual(result);
381 expect(result.relations_uuid_mapping).toBe(undefined);
382 }
383
384 // Delete line
385 line3.delete();
386 {
387 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
388 const result = models.serializeForORM(order);
389 expect(keepCommandResult).toEqual(result);
390
391 expect(result.lines.length).toBe(2);
392 expect(result.lines[0][0]).toBe(3);
393 expect(result.lines[0][1]).toBe(111);
394 expect(result.relations_uuid_mapping).toBe(undefined);
395 }
396
397 {
398 //All update/delete have been cleared
399 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
400 const result = models.serializeForORM(order);
401 expect(keepCommandResult).toEqual(result);
402 expect(result.lines).toHaveLength(1);
403 }
404 });
405
406 test("grouped lines and nested lines", async () => {
407 await makeMockServer();
408 const models = getRelatedModelsInstance(false);
409 const order = models["pos.order"].create({});
410 const parentLine = models["pos.order.line"].create({ order_id: order });
411 const line1 = models["pos.order.line"].create({
412 order_id: order,
413 combo_parent_id: parentLine,
414 });
415 const line2 = models["pos.order.line"].create({
416 order_id: order,
417 combo_parent_id: parentLine,
418 });
419 const line3 = models["pos.order.line"].create({
420 order_id: order,
421 });
422
423 {
424 const keepCommandResult = models.serializeForORM(order, { keepCommands: true });
425 const result = models.serializeForORM(order);
426 expect(keepCommandResult).toEqual(result);
427
428 expect(result.lines.length).toBe(4);
429 const { relations_uuid_mapping } = result;
430 expect(Object.keys(relations_uuid_mapping["pos.order.line"]).length).toBe(2);
431
432 const lineMapping = relations_uuid_mapping["pos.order.line"];
433 expect(lineMapping[line1.uuid]["combo_parent_id"]).toBe(parentLine.uuid);
434 expect(lineMapping[line2.uuid]["combo_parent_id"]).toBe(parentLine.uuid);
435 expect(lineMapping[line3.uuid]?.["combo_parent_id"]?.length).toBeEmpty();
436 }
437 });