| 1 |
import { describe, 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 |
describe("models with backlinks", () => { |
| 9 |
describe("many2one and one2many field relations to other models", () => { |
| 10 |
test("create operation", async () => { |
| 11 |
await makeMockServer(); |
| 12 |
const models = getRelatedModelsInstance(false); |
| 13 |
const category = models["pos.category"].create({}); |
| 14 |
const product = models["product.template"].create({ pos_categ_ids: [category] }); |
| 15 |
expect(product.pos_categ_ids).toInclude(category); |
| 16 |
expect(category.backLink("product.template.pos_categ_ids")).toInclude(product); |
| 17 |
}); |
| 18 |
test("read operation 1", async () => { |
| 19 |
await makeMockServer(); |
| 20 |
const models = getRelatedModelsInstance(false); |
| 21 |
const c1 = models["pos.category"].create({}); |
| 22 |
const c2 = models["pos.category"].create({}); |
| 23 |
const p1 = models["product.template"].create({ pos_categ_ids: [c1] }); |
| 24 |
const p2 = models["product.template"].create({ pos_categ_ids: [c1] }); |
| 25 |
const p3 = models["product.template"].create({ pos_categ_ids: [c2] }); |
| 26 |
|
| 27 |
// Test reading back the categories directly |
| 28 |
const readC1 = models["pos.category"].read(c1.id); |
| 29 |
expect(readC1).toEqual(c1); |
| 30 |
|
| 31 |
const readP1 = models["product.template"].read(p1.id); |
| 32 |
expect(readP1).toEqual(p1); |
| 33 |
|
| 34 |
// Test the many2one relationship from products to category |
| 35 |
expect(readP1.pos_categ_ids).toEqual([c1]); |
| 36 |
|
| 37 |
// Additional checks for completeness |
| 38 |
const readMany = models["product.template"].readMany([p2.id, p3.id]); |
| 39 |
expect(readMany).toEqual([p2, p3]); |
| 40 |
|
| 41 |
const readNonExistent = models["product.template"].read(9999); |
| 42 |
expect(readNonExistent).toBe(undefined); |
| 43 |
|
| 44 |
const readNonExistentC = models["pos.category"].read(9999); |
| 45 |
expect(readNonExistentC).toBe(undefined); |
| 46 |
}); |
| 47 |
|
| 48 |
test("update operation, many2one", async () => { |
| 49 |
await makeMockServer(); |
| 50 |
const models = getRelatedModelsInstance(false); |
| 51 |
const p1 = models["product.template"].create({}); |
| 52 |
const c1 = models["product.category"].create({}); |
| 53 |
|
| 54 |
p1.update({ categ_id: c1 }); |
| 55 |
expect(p1.categ_id).toBe(c1); |
| 56 |
}); |
| 57 |
|
| 58 |
test("update operation, one2many", async () => { |
| 59 |
await makeMockServer(); |
| 60 |
const models = getRelatedModelsInstance(false); |
| 61 |
const l1 = models["pos.order.line"].create({}); |
| 62 |
const l2 = models["pos.order.line"].create({}); |
| 63 |
const order1 = models["pos.order"].create({}); |
| 64 |
|
| 65 |
order1.update({ lines: [["link", l1, l2]] }); |
| 66 |
expect(order1.lines).toInclude(l1); |
| 67 |
expect(order1.lines).toInclude(l2); |
| 68 |
expect(l1.order_id).toBe(order1); |
| 69 |
}); |
| 70 |
|
| 71 |
test("update operation, unlink many2one", async () => { |
| 72 |
await makeMockServer(); |
| 73 |
const models = getRelatedModelsInstance(false); |
| 74 |
const o1 = models["pos.order"].create({ lines: [] }); |
| 75 |
const l1 = models["pos.order.line"].create({ |
| 76 |
order_id: o1, |
| 77 |
}); |
| 78 |
|
| 79 |
expect(l1.order_id).toEqual(o1); |
| 80 |
|
| 81 |
o1.update({ lines: [] }); |
| 82 |
expect(l1.order_id).toBe(undefined); |
| 83 |
}); |
| 84 |
|
| 85 |
test("update operation, unlink one2many", async () => { |
| 86 |
await makeMockServer(); |
| 87 |
const models = getRelatedModelsInstance(false); |
| 88 |
const o1 = models["pos.order"].create({ lines: [] }); |
| 89 |
const l1 = models["pos.order.line"].create({ |
| 90 |
order_id: o1, |
| 91 |
}); |
| 92 |
|
| 93 |
o1.update({ lines: [["link", l1]] }); |
| 94 |
expect(o1.lines).toInclude(l1); |
| 95 |
expect(l1.order_id).toBe(o1); |
| 96 |
|
| 97 |
o1.update({ lines: [["unlink", l1]] }); |
| 98 |
expect(o1.lines).not.toInclude(l1); |
| 99 |
expect(l1.order_id).toBe(undefined); |
| 100 |
}); |
| 101 |
|
| 102 |
test("update operation, Clear one2many", async () => { |
| 103 |
await makeMockServer(); |
| 104 |
const models = getRelatedModelsInstance(false); |
| 105 |
const o1 = models["pos.order"].create({}); |
| 106 |
const l1 = models["pos.order.line"].create({ |
| 107 |
order_id: o1, |
| 108 |
}); |
| 109 |
o1.update({ lines: [["link", l1]] }); |
| 110 |
expect(o1.lines).toInclude(l1); |
| 111 |
expect(l1.order_id).toBe(o1); |
| 112 |
|
| 113 |
o1.update({ lines: [["unlink", l1]] }); |
| 114 |
expect(o1.lines).not.toInclude(l1); |
| 115 |
expect(l1.order_id).toBe(undefined); |
| 116 |
}); |
| 117 |
|
| 118 |
test("update operation, Clear many2one", async () => { |
| 119 |
await makeMockServer(); |
| 120 |
const models = getRelatedModelsInstance(false); |
| 121 |
const o1 = models["pos.order"].create({ lines: [] }); |
| 122 |
models["pos.order.line"].create({ |
| 123 |
order_id: o1, |
| 124 |
}); |
| 125 |
|
| 126 |
models["pos.order"].update(o1, { lines: [] }); |
| 127 |
expect(o1.lines).toHaveLength(0); |
| 128 |
}); |
| 129 |
|
| 130 |
test("delete operation, one2many item", async () => { |
| 131 |
await makeMockServer(); |
| 132 |
const models = getRelatedModelsInstance(false); |
| 133 |
const l1 = models["pos.order.line"].create({}); |
| 134 |
const l2 = models["pos.order.line"].create({}); |
| 135 |
const o1 = models["pos.order"].create({}); |
| 136 |
|
| 137 |
o1.update({ lines: [["link", l1, l2]] }); |
| 138 |
expect(o1.lines).toInclude(l1); |
| 139 |
|
| 140 |
l1.delete(); |
| 141 |
expect(models["pos.order.line"].read(l1.id)).toBe(undefined); |
| 142 |
expect(o1.lines).not.toInclude(l1); |
| 143 |
}); |
| 144 |
|
| 145 |
test("delete operation, many2one item", async () => { |
| 146 |
await makeMockServer(); |
| 147 |
const models = getRelatedModelsInstance(false); |
| 148 |
const o1 = models["pos.order"].create({}); |
| 149 |
const l1 = models["pos.order.line"].create({}); |
| 150 |
|
| 151 |
o1.update({ lines: [["link", l1]] }); |
| 152 |
expect(l1.order_id).toBe(o1); |
| 153 |
|
| 154 |
l1.delete(); |
| 155 |
expect(models["pos.order.line"].read(l1.id)).toBe(undefined); |
| 156 |
expect(o1.lines).not.toInclude(l1); |
| 157 |
}); |
| 158 |
}); |
| 159 |
describe("many2one/one2many field relations to own model", () => { |
| 160 |
test("create operation", async () => { |
| 161 |
await makeMockServer(); |
| 162 |
const models = getRelatedModelsInstance(false); |
| 163 |
const o1 = models["pos.order"].create({}); |
| 164 |
const l2 = models["pos.order.line"].create({ order_id: o1 }); |
| 165 |
|
| 166 |
expect(l2.order_id).toBe(o1); |
| 167 |
expect(o1.lines).toInclude(l2); |
| 168 |
}); |
| 169 |
|
| 170 |
test("read operation 2", async () => { |
| 171 |
await makeMockServer(); |
| 172 |
const models = getRelatedModelsInstance(false); |
| 173 |
const c1 = models["pos.category"].create({}); |
| 174 |
const c2 = models["pos.category"].create({ parent_id: c1 }); |
| 175 |
|
| 176 |
const readC1 = models["pos.category"].read(c1.id); |
| 177 |
expect(readC1.child_ids).toEqual([c2]); |
| 178 |
|
| 179 |
const readC2 = models["pos.category"].read(c2.id); |
| 180 |
expect(readC2.parent_id).toEqual(c1); |
| 181 |
|
| 182 |
const readMany = models["pos.category"].readMany([c1.id, c2.id]); |
| 183 |
expect(readMany).toEqual([c1, c2]); |
| 184 |
}); |
| 185 |
|
| 186 |
test("update operation, many2one", async () => { |
| 187 |
await makeMockServer(); |
| 188 |
const models = getRelatedModelsInstance(false); |
| 189 |
const o1 = models["pos.order"].create({}); |
| 190 |
const l1 = models["pos.order.line"].create({}); |
| 191 |
const l2 = models["pos.order.line"].create({ order_id: o1 }); |
| 192 |
|
| 193 |
expect(l2.order_id).toBe(o1); |
| 194 |
o1.update({ lines: [l1] }); |
| 195 |
expect(o1.lines).not.toInclude(l2); |
| 196 |
}); |
| 197 |
|
| 198 |
test("update operation, one2many", async () => { |
| 199 |
await makeMockServer(); |
| 200 |
const models = getRelatedModelsInstance(false); |
| 201 |
const o1 = models["pos.order"].create({}); |
| 202 |
const l1 = models["pos.order.line"].create({ order_id: o1 }); |
| 203 |
const l2 = models["pos.order.line"].create({}); |
| 204 |
|
| 205 |
expect(o1.lines).toInclude(l1); |
| 206 |
l2.update({ order_id: o1 }); |
| 207 |
expect(o1.lines).toInclude(l2); |
| 208 |
}); |
| 209 |
|
| 210 |
test("update operation, unlink many2one", async () => { |
| 211 |
await makeMockServer(); |
| 212 |
const models = getRelatedModelsInstance(false); |
| 213 |
const c1 = models["pos.category"].create({}); |
| 214 |
const c2 = models["pos.category"].create({}); |
| 215 |
|
| 216 |
c2.update({ parent_id: c1 }); |
| 217 |
expect(c2.parent_id).toBe(c1); |
| 218 |
|
| 219 |
c2.update({ parent_id: undefined }); |
| 220 |
expect(c2.parent_id).toBe(undefined); |
| 221 |
expect(c1.child_ids).not.toInclude(c2); |
| 222 |
}); |
| 223 |
|
| 224 |
test("update operation, unlink one2many", async () => { |
| 225 |
await makeMockServer(); |
| 226 |
const models = getRelatedModelsInstance(false); |
| 227 |
const c1 = models["pos.category"].create({}); |
| 228 |
const c2 = models["pos.category"].create({ parent_id: c1 }); |
| 229 |
|
| 230 |
expect(c1.child_ids).toInclude(c2); |
| 231 |
|
| 232 |
c1.update({ child_ids: [["unlink", c2]] }); |
| 233 |
expect(c1.child_ids).not.toInclude(c2); |
| 234 |
expect(c2.parent_id).toBe(undefined); |
| 235 |
}); |
| 236 |
|
| 237 |
test("update operation, Clear one2many", async () => { |
| 238 |
await makeMockServer(); |
| 239 |
const models = getRelatedModelsInstance(false); |
| 240 |
const category = models["pos.category"].create({}); |
| 241 |
models["pos.category"].create({ parent_id: category }); |
| 242 |
models["pos.category"].create({ parent_id: category }); |
| 243 |
|
| 244 |
expect(category.child_ids).toHaveLength(2); |
| 245 |
models["pos.category"].update(category, { child_ids: [["clear"]] }); |
| 246 |
expect(category.child_ids).toHaveLength(0); |
| 247 |
}); |
| 248 |
|
| 249 |
test("update operation, Clear many2one", async () => { |
| 250 |
await makeMockServer(); |
| 251 |
const models = getRelatedModelsInstance(false); |
| 252 |
const category = models["pos.category"].create({}); |
| 253 |
const category1 = models["pos.category"].create({ parent_id: category }); |
| 254 |
|
| 255 |
expect(category.child_ids).toInclude(category1); |
| 256 |
models["pos.category"].update(category1, { parent_id: undefined }); |
| 257 |
expect(category.child_ids).not.toInclude(category1); |
| 258 |
}); |
| 259 |
|
| 260 |
test("delete operation, one2many item", async () => { |
| 261 |
await makeMockServer(); |
| 262 |
const models = getRelatedModelsInstance(false); |
| 263 |
const c1 = models["pos.category"].create({}); |
| 264 |
const c2 = models["pos.category"].create({ parent_id: c1 }); |
| 265 |
|
| 266 |
expect(c1.child_ids).toInclude(c2); |
| 267 |
|
| 268 |
c2.delete(); |
| 269 |
expect(models["pos.category"].read(c2.id)).toBe(undefined); |
| 270 |
expect(c1.child_ids).not.toInclude(c2); |
| 271 |
}); |
| 272 |
|
| 273 |
test("delete operation, many2one item", async () => { |
| 274 |
await makeMockServer(); |
| 275 |
const models = getRelatedModelsInstance(false); |
| 276 |
const c1 = models["pos.category"].create({}); |
| 277 |
const c2 = models["pos.category"].create({ parent_id: c1 }); |
| 278 |
|
| 279 |
expect(c1.child_ids).toInclude(c2); |
| 280 |
|
| 281 |
c1.delete(); |
| 282 |
expect(models["pos.category"].read(c1.id)).toBe(undefined); |
| 283 |
expect(c2.parent_id).toBe(undefined); |
| 284 |
}); |
| 285 |
}); |
| 286 |
describe("many2many field relations to other models", () => { |
| 287 |
test("create operation, create", async () => { |
| 288 |
await makeMockServer(); |
| 289 |
const models = getRelatedModelsInstance(false); |
| 290 |
const line1 = { id: 2 }; |
| 291 |
const line2 = { name: 4 }; |
| 292 |
const product = models["pos.order"].create({ |
| 293 |
name: "Smartphone", |
| 294 |
lines: [["create", line1, line2]], |
| 295 |
}); |
| 296 |
expect(product.lines).toHaveLength(2); |
| 297 |
}); |
| 298 |
|
| 299 |
test("create operation, link", async () => { |
| 300 |
await makeMockServer(); |
| 301 |
const models = getRelatedModelsInstance(false); |
| 302 |
const line1 = models["pos.order.line"].create({ id: 1 }); |
| 303 |
const line2 = models["pos.order.line"].create({ id: 2 }); |
| 304 |
const product = models["pos.order"].create({ |
| 305 |
name: "Smartphone", |
| 306 |
lines: [["link", line1, line2]], |
| 307 |
}); |
| 308 |
expect(product.lines).toInclude(line1); |
| 309 |
expect(product.lines).toInclude(line2); |
| 310 |
}); |
| 311 |
|
| 312 |
test("read operation 3", async () => { |
| 313 |
await makeMockServer(); |
| 314 |
const models = getRelatedModelsInstance(false); |
| 315 |
const c1 = models["pos.category"].create({}); |
| 316 |
const c2 = models["pos.category"].create({}); |
| 317 |
const c3 = models["pos.category"].create({}); |
| 318 |
const t1 = models["product.template"].create({ pos_categ_ids: [["link", c1, c2, c3]] }); |
| 319 |
const t2 = models["product.template"].create({ pos_categ_ids: [["link", c1, c2]] }); |
| 320 |
|
| 321 |
const readT1 = models["product.template"].read(t1.id); |
| 322 |
expect(readT1).toEqual(t1); |
| 323 |
const readP1 = models["product.template"].read(t2.id); |
| 324 |
expect(readP1).toEqual(t2); |
| 325 |
|
| 326 |
expect(readT1.pos_categ_ids).toInclude(c1); |
| 327 |
expect(readT1.pos_categ_ids).toInclude(c2); |
| 328 |
expect(readT1.pos_categ_ids).toInclude(c3); |
| 329 |
expect(readP1.pos_categ_ids).toInclude(c1); |
| 330 |
expect(readP1.pos_categ_ids).toInclude(c2); |
| 331 |
|
| 332 |
const readMany = models["product.template"].readMany([t1.id, t2.id]); |
| 333 |
expect(readMany[0]).toBe(t1); |
| 334 |
expect(readMany[1]).toBe(t2); |
| 335 |
}); |
| 336 |
|
| 337 |
test("update operation, many2many", async () => { |
| 338 |
await makeMockServer(); |
| 339 |
const models = getRelatedModelsInstance(false); |
| 340 |
const c1 = models["pos.category"].create({}); |
| 341 |
const c2 = models["pos.category"].create({}); |
| 342 |
const t1 = models["product.template"].create({ pos_categ_ids: [] }); |
| 343 |
expect(t1.pos_categ_ids).not.toInclude(c1); |
| 344 |
|
| 345 |
t1.update({ pos_categ_ids: [["link", c1]] }); |
| 346 |
expect(t1.pos_categ_ids).toInclude(c1); |
| 347 |
expect(t1.pos_categ_ids).not.toInclude(c2); |
| 348 |
|
| 349 |
t1.update({ pos_categ_ids: [["link", c2]] }); |
| 350 |
expect(t1.pos_categ_ids).toInclude(c2); |
| 351 |
}); |
| 352 |
|
| 353 |
test("update operation, unlink many2many", async () => { |
| 354 |
await makeMockServer(); |
| 355 |
const models = getRelatedModelsInstance(false); |
| 356 |
const c1 = models["pos.category"].create({}); |
| 357 |
const t1 = models["product.template"].create({ pos_categ_ids: [] }); |
| 358 |
|
| 359 |
t1.update({ pos_categ_ids: [["link", c1]] }); |
| 360 |
expect(t1.pos_categ_ids).toInclude(c1); |
| 361 |
|
| 362 |
t1.update({ pos_categ_ids: [["unlink", c1]] }); |
| 363 |
expect(t1.pos_categ_ids).not.toInclude(c1); |
| 364 |
}); |
| 365 |
|
| 366 |
test("update operation, Clear many2many", async () => { |
| 367 |
await makeMockServer(); |
| 368 |
const models = getRelatedModelsInstance(false); |
| 369 |
const c1 = models["pos.category"].create({}); |
| 370 |
const c2 = models["pos.category"].create({}); |
| 371 |
const t1 = models["product.template"].create({ pos_categ_ids: [c1, c2] }); |
| 372 |
|
| 373 |
expect(t1.pos_categ_ids).toHaveLength(2); |
| 374 |
|
| 375 |
t1.update({ pos_categ_ids: [["clear"]] }); |
| 376 |
expect(t1.pos_categ_ids).toHaveLength(0); |
| 377 |
}); |
| 378 |
|
| 379 |
test("delete operation, many2many item", async () => { |
| 380 |
await makeMockServer(); |
| 381 |
const models = getRelatedModelsInstance(false); |
| 382 |
const c1 = models["pos.category"].create({}); |
| 383 |
const c2 = models["pos.category"].create({}); |
| 384 |
const t1 = models["product.template"].create({ pos_categ_ids: [] }); |
| 385 |
t1.update({ pos_categ_ids: [["link", c1, c2]] }); |
| 386 |
|
| 387 |
expect(t1.pos_categ_ids).toInclude(c1); |
| 388 |
|
| 389 |
c1.delete(); |
| 390 |
expect(models["pos.category"].read(c1.id)).toBe(undefined); |
| 391 |
expect(t1.pos_categ_ids).not.toInclude(c1); |
| 392 |
expect(t1.pos_categ_ids).toInclude(c2); |
| 393 |
}); |
| 394 |
|
| 395 |
describe("many2many field relations to own model", () => { |
| 396 |
test("create operation, link", async () => { |
| 397 |
await makeMockServer(); |
| 398 |
const models = getRelatedModelsInstance(false); |
| 399 |
const l1 = models["pos.order.line"].create({ id: 1 }); |
| 400 |
const l2 = models["pos.order.line"].create({ id: 2, combo_parent_id: l1 }); |
| 401 |
expect(l1.combo_line_ids).toInclude(l2); |
| 402 |
expect(l2.combo_parent_id).toBe(l1); |
| 403 |
}); |
| 404 |
|
| 405 |
test("read operation 4", async () => { |
| 406 |
await makeMockServer(); |
| 407 |
const models = getRelatedModelsInstance(false); |
| 408 |
const l1 = models["pos.order.line"].create({}); |
| 409 |
const l2 = models["pos.order.line"].create({}); |
| 410 |
const l3 = models["pos.order.line"].create({}); |
| 411 |
const l4 = models["pos.order.line"].create({ combo_line_ids: [l1, l2, l3] }); |
| 412 |
|
| 413 |
const readL1 = models["pos.order.line"].read(l1.id); |
| 414 |
expect(readL1).toEqual(l1); |
| 415 |
|
| 416 |
const readL4 = models["pos.order.line"].read(l4.id); |
| 417 |
expect(readL4).toEqual(l4); |
| 418 |
|
| 419 |
expect([l1, l2, l3].every((n) => readL4.combo_line_ids.includes(n))).toBe(true); |
| 420 |
|
| 421 |
const readMany = models["pos.order.line"].readMany([l2.id, l3.id]); |
| 422 |
expect(readMany[0]).toBe(l2); |
| 423 |
expect(readMany[1]).toBe(l3); |
| 424 |
}); |
| 425 |
|
| 426 |
test("update operation, many2many", async () => { |
| 427 |
await makeMockServer(); |
| 428 |
const models = getRelatedModelsInstance(false); |
| 429 |
const l1 = models["pos.order.line"].create({}); |
| 430 |
const l2 = models["pos.order.line"].create({}); |
| 431 |
const l3 = models["pos.order.line"].create({}); |
| 432 |
l1.update({ combo_line_ids: [["link", l3]] }); |
| 433 |
expect(l1.combo_line_ids).toInclude(l3); |
| 434 |
expect(l3.combo_parent_id).toBe(l1); |
| 435 |
|
| 436 |
l3.update({ combo_line_ids: [["link", l2]] }); |
| 437 |
expect(l3.combo_line_ids).toInclude(l2); |
| 438 |
|
| 439 |
l3.update({ combo_line_ids: [["unlink", l2]] }); |
| 440 |
expect(l3.combo_line_ids).not.toInclude(l2); |
| 441 |
expect(l2.combo_line_ids).not.toInclude(l3); |
| 442 |
}); |
| 443 |
|
| 444 |
test("update operation, unlink many2many", async () => { |
| 445 |
await makeMockServer(); |
| 446 |
const models = getRelatedModelsInstance(false); |
| 447 |
const l1 = models["pos.order.line"].create({}); |
| 448 |
const l2 = models["pos.order.line"].create({}); |
| 449 |
|
| 450 |
l2.update({ combo_line_ids: [["link", l1]] }); |
| 451 |
expect(l2.combo_line_ids).toInclude(l1); |
| 452 |
expect(l1.combo_parent_id).toBe(l2); |
| 453 |
|
| 454 |
l2.update({ combo_line_ids: [["unlink", l1]] }); |
| 455 |
expect(l2.combo_line_ids).not.toInclude(l1); |
| 456 |
expect(l1.combo_parent_id).toBeEmpty(); |
| 457 |
}); |
| 458 |
|
| 459 |
test("update operation, Clear many2many", async () => { |
| 460 |
await makeMockServer(); |
| 461 |
const models = getRelatedModelsInstance(false); |
| 462 |
const l1 = models["pos.order.line"].create({}); |
| 463 |
const l2 = models["pos.order.line"].create({}); |
| 464 |
const l3 = models["pos.order.line"].create({}); |
| 465 |
const l4 = models["pos.order.line"].create({ combo_line_ids: [l1, l2, l3] }); |
| 466 |
|
| 467 |
expect(l4.combo_line_ids).toHaveLength(3); |
| 468 |
|
| 469 |
models["pos.order.line"].update(l4, { combo_line_ids: [["clear"]] }); |
| 470 |
|
| 471 |
expect(l4.combo_line_ids).toHaveLength(0); |
| 472 |
expect(l1.combo_parent_id).toBeEmpty(); |
| 473 |
}); |
| 474 |
|
| 475 |
test("delete operation, many2many item", async () => { |
| 476 |
await makeMockServer(); |
| 477 |
const models = getRelatedModelsInstance(false); |
| 478 |
const l1 = models["pos.order.line"].create({}); |
| 479 |
const l2 = models["pos.order.line"].create({}); |
| 480 |
const l3 = models["pos.order.line"].create({}); |
| 481 |
l3.update({ combo_line_ids: [["link", l1, l2]] }); |
| 482 |
|
| 483 |
expect([l1, l2].every((n) => l3.combo_line_ids.includes(n))).toBe(true); |
| 484 |
|
| 485 |
l1.delete(); |
| 486 |
expect(models["pos.order.line"].read(l1.id)).toBe(undefined); |
| 487 |
expect(l3.combo_line_ids).not.toInclude(l1); |
| 488 |
}); |
| 489 |
}); |
| 490 |
}); |
| 491 |
}); |
| 492 |
|
| 493 |
describe("models without backlinks", () => { |
| 494 |
describe("many2one and one2many field relations to other models", () => { |
| 495 |
test("create operation", async () => { |
| 496 |
await makeMockServer(); |
| 497 |
const models = getRelatedModelsInstance(false); |
| 498 |
const category = models["pos.category"].create({ id: 1 }); |
| 499 |
const product = models["product.template"].create({ pos_categ_ids: [category] }); |
| 500 |
expect(product.pos_categ_ids).toEqual([category]); |
| 501 |
expect(category.backLink("<-product.template.pos_categ_ids")).toEqual([product]); |
| 502 |
}); |
| 503 |
|
| 504 |
test("read operation 5", async () => { |
| 505 |
await makeMockServer(); |
| 506 |
const models = getRelatedModelsInstance(false); |
| 507 |
const c1 = models["pos.category"].create({}); |
| 508 |
const p1 = models["product.template"].create({ pos_categ_ids: [c1] }); |
| 509 |
const p2 = models["product.template"].create({ pos_categ_ids: [c1] }); |
| 510 |
|
| 511 |
const readC1 = models["pos.category"].read(c1.id); |
| 512 |
expect(readC1).toEqual(c1); |
| 513 |
|
| 514 |
const readP1 = models["product.template"].read(p1.id); |
| 515 |
expect(readP1).toEqual(p1); |
| 516 |
|
| 517 |
expect(readC1.backLink("product.template.pos_categ_ids")).toEqual([p1, p2]); |
| 518 |
|
| 519 |
expect(readP1.pos_categ_ids).toEqual([c1]); |
| 520 |
}); |
| 521 |
|
| 522 |
test("update operation, many2one", async () => { |
| 523 |
await makeMockServer(); |
| 524 |
const models = getRelatedModelsInstance(false); |
| 525 |
const p1 = models["product.template"].create({}); |
| 526 |
const c1 = models["pos.category"].create({}); |
| 527 |
|
| 528 |
p1.update({ pos_categ_ids: [c1] }); |
| 529 |
expect(p1.pos_categ_ids).toInclude(c1); |
| 530 |
}); |
| 531 |
|
| 532 |
test("update operation, unlink many2one", async () => { |
| 533 |
await makeMockServer(); |
| 534 |
const models = getRelatedModelsInstance(false); |
| 535 |
const categ = models["pos.category"].create({}); |
| 536 |
const p1 = models["product.template"].create({ pos_categ_ids: [categ] }); |
| 537 |
|
| 538 |
p1.update({ pos_categ_ids: [] }); |
| 539 |
expect(p1.pos_categ_ids).toHaveLength(0); |
| 540 |
expect(categ.backLink("product.template.pos_categ_ids")).toHaveLength(0); |
| 541 |
}); |
| 542 |
|
| 543 |
test("delete operation, many2one item", async () => { |
| 544 |
await makeMockServer(); |
| 545 |
const models = getRelatedModelsInstance(false); |
| 546 |
const p1 = models["product.template"].create({}); |
| 547 |
const c1 = models["pos.category"].create({}); |
| 548 |
|
| 549 |
p1.update({ pos_categ_ids: [c1] }); |
| 550 |
expect(c1.backLink("<-product.template.pos_categ_ids")).toEqual([p1]); |
| 551 |
|
| 552 |
c1.delete(); |
| 553 |
expect(models["pos.category"].read(c1.id)).toBe(undefined); |
| 554 |
expect(p1.pos_categ_ids).toHaveLength(0); |
| 555 |
}); |
| 556 |
}); |
| 557 |
|
| 558 |
describe("many2many relations", () => { |
| 559 |
test("create operation, link", async () => { |
| 560 |
await makeMockServer(); |
| 561 |
const models = getRelatedModelsInstance(false); |
| 562 |
const l1 = models["pos.order.line"].create({}); |
| 563 |
const l2 = models["pos.order.line"].create({}); |
| 564 |
const l3 = models["pos.order.line"].create({}); |
| 565 |
const o1 = models["pos.order"].create({ |
| 566 |
lines: [["link", l1, l2, l3]], |
| 567 |
}); |
| 568 |
|
| 569 |
expect(o1.lines).toInclude(l1); |
| 570 |
}); |
| 571 |
|
| 572 |
test("read operation 6", async () => { |
| 573 |
await makeMockServer(); |
| 574 |
const models = getRelatedModelsInstance(false); |
| 575 |
const c1 = models["pos.category"].create({}); |
| 576 |
const c2 = models["pos.category"].create({}); |
| 577 |
const p1 = models["product.template"].create({ |
| 578 |
pos_categ_ids: [["link", c1, c2]], |
| 579 |
}); |
| 580 |
const p2 = models["product.template"].create({ |
| 581 |
pos_categ_ids: [["link", c1, c2]], |
| 582 |
}); |
| 583 |
const p3 = models["product.template"].create({ pos_categ_ids: [["link", c1]] }); |
| 584 |
|
| 585 |
const readT1 = models["product.template"].read(p1.id); |
| 586 |
expect(readT1).toEqual(p1); |
| 587 |
|
| 588 |
const readP1 = models["product.template"].read(p2.id); |
| 589 |
expect(readP1).toEqual(p2); |
| 590 |
|
| 591 |
const readMany = models["product.template"].readMany([p2.id, p3.id]); |
| 592 |
expect(readMany).toEqual([p2, p3]); |
| 593 |
}); |
| 594 |
|
| 595 |
test("update operation, link", async () => { |
| 596 |
await makeMockServer(); |
| 597 |
const models = getRelatedModelsInstance(false); |
| 598 |
const p1 = models["product.template"].create({}); |
| 599 |
const p2 = models["product.template"].create({}); |
| 600 |
const t1 = models["pos.category"].create({}); |
| 601 |
|
| 602 |
p1.update({ pos_categ_ids: [["link", t1]] }); |
| 603 |
expect(p1.pos_categ_ids).toInclude(t1); |
| 604 |
expect(t1.backLink("<-product.template.pos_categ_ids")).toInclude(p1); |
| 605 |
expect(t1.backLink("<-product.template.pos_categ_ids")).not.toInclude(p2); |
| 606 |
|
| 607 |
p2.update({ pos_categ_ids: [["link", t1]] }); |
| 608 |
expect(t1.backLink("<-product.template.pos_categ_ids")).toInclude(p2); |
| 609 |
}); |
| 610 |
|
| 611 |
test("update operation, unlink", async () => { |
| 612 |
await makeMockServer(); |
| 613 |
const models = getRelatedModelsInstance(false); |
| 614 |
const p1 = models["product.template"].create({}); |
| 615 |
const t1 = models["pos.category"].create({}); |
| 616 |
|
| 617 |
p1.update({ pos_categ_ids: [["link", t1]] }); |
| 618 |
expect(t1.backLink("<-product.template.pos_categ_ids")).toInclude(p1); |
| 619 |
expect(p1.pos_categ_ids).toInclude(t1); |
| 620 |
|
| 621 |
p1.update({ pos_categ_ids: [["unlink", t1]] }); |
| 622 |
expect(t1.backLink("<-product.template.pos_categ_ids")).not.toInclude(p1); |
| 623 |
expect(p1.pos_categ_ids).toHaveLength(0); |
| 624 |
}); |
| 625 |
|
| 626 |
test("update operation, Clear", async () => { |
| 627 |
await makeMockServer(); |
| 628 |
const models = getRelatedModelsInstance(false); |
| 629 |
const tag1 = models["pos.category"].create({}); |
| 630 |
const tag2 = models["pos.category"].create({}); |
| 631 |
const product = models["product.template"].create({ pos_categ_ids: [tag1, tag2] }); |
| 632 |
models["product.template"].update(product, { pos_categ_ids: [["clear"]] }); |
| 633 |
const updatedProduct = models["product.template"].read(product.id); |
| 634 |
expect(updatedProduct.pos_categ_ids).toHaveLength(0); |
| 635 |
|
| 636 |
models["product.template"].update(product, { pos_categ_ids: [["link", tag1, tag2]] }); |
| 637 |
expect([tag1, tag2].every((t) => product.pos_categ_ids.includes(t))).toBe(true); |
| 638 |
models["product.template"].update(product, { pos_categ_ids: [["clear"]] }); |
| 639 |
expect(tag1.backLink("<-product.template.pos_categ_ids")).not.toInclude(product); |
| 640 |
}); |
| 641 |
|
| 642 |
test("delete operation", async () => { |
| 643 |
await makeMockServer(); |
| 644 |
const models = getRelatedModelsInstance(false); |
| 645 |
const p1 = models["product.template"].create({}); |
| 646 |
const p2 = models["product.template"].create({}); |
| 647 |
const t1 = models["pos.category"].create({}); |
| 648 |
|
| 649 |
p1.update({ pos_categ_ids: [["link", t1]] }); |
| 650 |
p2.update({ pos_categ_ids: [["link", t1]] }); |
| 651 |
|
| 652 |
expect(t1.backLink("<-product.template.pos_categ_ids")).toInclude(p1); |
| 653 |
|
| 654 |
p1.delete(); |
| 655 |
expect(models["product.template"].read(p1.id)).toBe(undefined); |
| 656 |
expect(t1.backLink("<-product.template.pos_categ_ids")).not.toInclude(p1); |
| 657 |
|
| 658 |
t1.delete(); |
| 659 |
expect(models["pos.category"].read(t1.id)).toBe(undefined); |
| 660 |
expect(p1.pos_categ_ids).toHaveLength(0); |
| 661 |
}); |
| 662 |
}); |
| 663 |
}); |