View | Details | Raw Unified | Return to bug 40180
Collapse All | Expand All

(-)a/t/cypress/integration/t/insertData.ts (+19 lines)
Lines 2-7 const { query } = require("./../../plugins/db.js"); Link Here
2
const { getBasicAuthHeader } = require("./../../plugins/auth.js");
2
const { getBasicAuthHeader } = require("./../../plugins/auth.js");
3
3
4
describe("insertData", () => {
4
describe("insertData", () => {
5
    describe("deleteSampleObjects", () => {
6
        it("should delete everything from Object", () => {
7
            cy.task("insertSampleBiblio", { item_count: 2 }).then(objects => {
8
                cy.task("deleteSampleObjects", objects);
9
            });
10
        });
11
        it("should delete everything from Array", () => {
12
            cy.task("insertSampleBiblio", { item_count: 2 }).then(objects => {
13
                cy.task("deleteSampleObjects", [
14
                    { biblio: objects.biblio },
15
                    { item_type: objects.item_type },
16
                    { item: objects.items[0] },
17
                    { item: objects.items[1] },
18
                    { library: objects.libraries[0] },
19
                ]);
20
            });
21
        });
22
    });
23
5
    describe("insertSampleBiblio", () => {
24
    describe("insertSampleBiblio", () => {
6
        it("should generate library and item type", () => {
25
        it("should generate library and item type", () => {
7
            cy.task("insertSampleBiblio", { item_count: 3 }).then(objects => {
26
            cy.task("insertSampleBiblio", { item_count: 3 }).then(objects => {
(-)a/t/cypress/plugins/insertData.js (-72 / +83 lines)
Lines 210-291 const deleteSampleObjects = async allObjects => { Link Here
210
        allObjects = [allObjects];
210
        allObjects = [allObjects];
211
    }
211
    }
212
212
213
    const pluralMap = {
214
        hold: "holds",
215
        patron: "patrons",
216
        item: "items",
217
        biblio: "biblios",
218
        library: "libraries",
219
        item_type: "item_types",
220
    };
221
    // Merge by type
222
    const mergedObjects = {};
223
    for (const objects of allObjects) {
224
        for (const [type, value] of Object.entries(objects)) {
225
            let plural = pluralMap?.[type] || type;
226
            if (!mergedObjects[plural]) {
227
                mergedObjects[plural] = [];
228
            }
229
230
            if (Array.isArray(value)) {
231
                mergedObjects[plural].push(...value);
232
            } else {
233
                mergedObjects[plural].push(value);
234
            }
235
        }
236
    }
237
213
    const deletionOrder = [
238
    const deletionOrder = [
214
        "hold",
239
        "holds",
215
        "patron",
240
        "patrons",
216
        "item",
217
        "items",
241
        "items",
218
        "biblio",
242
        "biblios",
219
        "library",
220
        "libraries",
243
        "libraries",
221
        "item_type",
244
        "item_types",
222
    ];
245
    ];
223
246
224
    for (const type of deletionOrder) {
247
    for (const type of deletionOrder) {
225
        for (const objects of allObjects) {
248
        if (!mergedObjects[type] || mergedObjects[type].length === 0) {
226
            if (!objects.hasOwnProperty(type)) {
249
            continue;
227
                continue;
250
        }
228
            }
251
229
            if (Array.isArray(objects[type]) && objects[type].length == 0) {
252
        const objects = mergedObjects[type];
230
                // Empty array
253
        let ids = [];
231
                continue;
254
        switch (type) {
232
            }
255
            case "biblios":
233
            switch (type) {
256
                ids = objects.map(i => i.biblio_id);
234
                case "biblio":
257
                await query({
235
                    await query({
258
                    sql: `DELETE FROM biblio WHERE biblionumber IN (${ids.map(() => "?").join(",")})`,
236
                        sql: "DELETE FROM biblio WHERE biblionumber=?",
259
                    values: ids,
237
                        values: [objects[type].biblio_id],
260
                });
238
                    });
261
                break;
239
                    break;
262
            case "items":
240
                case "items":
263
                ids = objects.map(i => i.item_id);
241
                    let item_ids = objects[type].map(i => i.item_id);
264
                await query({
242
                    await query({
265
                    sql: `DELETE FROM items WHERE itemnumber IN (${ids.map(() => "?").join(",")})`,
243
                        sql: `DELETE FROM items WHERE itemnumber IN (${item_ids.map(() => "?").join(",")})`,
266
                    values: ids,
244
                        values: item_ids,
267
                });
245
                    });
268
                break;
246
                    break;
269
            case "libraries":
247
                case "item":
270
                ids = objects.map(i => i.library_id);
248
                    await query({
271
                await query({
249
                        sql: "DELETE FROM items WHERE itemnumber = ?",
272
                    sql: `DELETE FROM branches WHERE branchcode IN (${ids.map(() => "?").join(",")})`,
250
                        values: [objects[type].item_id],
273
                    values: ids,
251
                    });
274
                });
252
                    break;
275
                break;
253
                case "library":
276
            case "holds":
254
                    await query({
277
                ids = objects.map(i => i.hold_id);
255
                        sql: "DELETE FROM branches WHERE branchcode = ?",
278
                await query({
256
                        values: [objects[type].library_id],
279
                    sql: `DELETE FROM reserves WHERE reserve_id IN (${ids.map(() => "?").join(",")})`,
257
                    });
280
                    values: ids,
258
                    break;
281
                });
259
                case "libraries":
282
                break;
260
                    let library_ids = objects[type].map(i => i.library_id);
283
            case "item_types":
261
                    await query({
284
                ids = objects.map(i => i.item_type_id);
262
                        sql: `DELETE FROM branches WHERE branchcode IN (${library_ids.map(() => "?").join(",")})`,
285
                await query({
263
                        values: library_ids,
286
                    sql: `DELETE FROM itemtypes WHERE itemtype IN (${ids.map(() => "?").join(",")})`,
264
                    });
287
                    values: ids,
265
                    break;
288
                });
266
                case "hold":
289
                break;
267
                    await query({
290
            case "patrons":
268
                        sql: "DELETE FROM reserves WHERE reserve_id = ?",
291
                ids = objects.map(i => i.patron_id);
269
                        values: [objects[type].hold_id],
292
                await query({
270
                    });
293
                    sql: `DELETE FROM borrowers WHERE borrowernumber IN (${ids.map(() => "?").join(",")})`,
271
                    break;
294
                    values: ids,
272
                case "item_type":
295
                });
273
                    await query({
296
                break;
274
                        sql: "DELETE FROM itemtypes WHERE itemtype = ?",
297
            default:
275
                        values: [objects[type].item_type_id],
298
                throw Error(
276
                    });
299
                    `Not implemented yet: cannot deleted object '${type}'`
277
                    break;
300
                );
278
                case "patron":
279
                    await query({
280
                        sql: "DELETE FROM borrowers WHERE borrowernumber = ?",
281
                        values: [objects[type].patron_id],
282
                    });
283
                    break;
284
                default:
285
                    console.log(
286
                        `Not implemented yet: cannot deleted object '${type}'`
287
                    );
288
            }
289
        }
301
        }
290
    }
302
    }
291
    return true;
303
    return true;
Lines 420-426 const insertObject = async ({ type, object, baseUrl, authHeader }) => { Link Here
420
            authHeader,
432
            authHeader,
421
        });
433
        });
422
    } else {
434
    } else {
423
        return false;
435
        throw Error(`Unsupported object type '${type}' to insert`);
424
    }
436
    }
425
437
426
    return true;
438
    return true;
427
- 

Return to bug 40180