From 68153d19ac0968a23890adcbc28eec7184c0f7e3 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 18 Jun 2025 14:40:34 +0200 Subject: [PATCH] Bug 40180: Add tests for insertSampleHold --- t/cypress/integration/t/insertData.ts | 220 +++++++++++++++++++------- t/cypress/plugins/insertData.js | 9 +- 2 files changed, 168 insertions(+), 61 deletions(-) diff --git a/t/cypress/integration/t/insertData.ts b/t/cypress/integration/t/insertData.ts index 9055067b541..b5ba8cae406 100644 --- a/t/cypress/integration/t/insertData.ts +++ b/t/cypress/integration/t/insertData.ts @@ -1,78 +1,178 @@ const { query } = require("./../../plugins/db.js"); +const { APIClient } = require("./../../plugins/dist/api-client.cjs.js"); +const { getBasicAuthHeader } = require("./../../plugins/auth.js"); -describe("insertSampleBiblio", () => { - it("insertSampleBiblio - should generate library and item type", () => { - cy.task("insertSampleBiblio", { item_count: 3 }).then(objects => { - expect(typeof objects.biblio.biblio_id).to.be.equal("number"); - expect(typeof objects.biblio.title).to.be.equal("string"); - expect(typeof objects.biblio.author).to.be.equal("string"); +const { APIClient } = require("./../../plugins/dist/api-client.cjs.js"); - const biblio_id = objects.biblio.biblio_id; +let client = APIClient.default; - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM biblio WHERE biblionumber=?", - values: [biblio_id], - }).then(result => { - expect(result[0].count).to.be.equal(1); - }); +describe("insertData", () => { + describe("insertSampleBiblio", () => { + it("should generate library and item type", () => { + cy.task("insertSampleBiblio", { item_count: 3 }).then(objects => { + expect(typeof objects.biblio.biblio_id).to.be.equal("number"); + expect(typeof objects.biblio.title).to.be.equal("string"); + expect(typeof objects.biblio.author).to.be.equal("string"); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM items WHERE biblionumber=?", - values: [biblio_id], - }).then(result => { - expect(result[0].count).to.be.equal(3); - }); + const biblio_id = objects.biblio.biblio_id; - cy.task("query", { - sql: "SELECT DISTINCT(itype) as count FROM items WHERE biblionumber=?", - values: [biblio_id], - }).then(result => { - expect(result.length).to.be.equal(1); - }); + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM biblio WHERE biblionumber=?", + values: [biblio_id], + }).then(result => { + expect(result[0].count).to.be.equal(1); + }); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM branches WHERE branchcode=?", - values: [objects.libraries[0].library_id], - }).then(result => { - expect(result[0].count).to.be.equal(1); - }); + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM items WHERE biblionumber=?", + values: [biblio_id], + }).then(result => { + expect(result[0].count).to.be.equal(3); + }); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM itemtypes WHERE itemtype=?", - values: [objects.item_type.item_type_id], - }).then(result => { - expect(result[0].count).to.be.equal(1); - }); + cy.task("query", { + sql: "SELECT DISTINCT(itype) as count FROM items WHERE biblionumber=?", + values: [biblio_id], + }).then(result => { + expect(result.length).to.be.equal(1); + }); - cy.task("deleteSampleObjects", objects); + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM branches WHERE branchcode=?", + values: [objects.libraries[0].library_id], + }).then(result => { + expect(result[0].count).to.be.equal(1); + }); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM biblio WHERE biblionumber=?", - values: [biblio_id], - }).then(result => { - expect(result[0].count).to.be.equal(0); - }); + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM itemtypes WHERE itemtype=?", + values: [objects.item_type.item_type_id], + }).then(result => { + expect(result[0].count).to.be.equal(1); + }); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM items WHERE biblionumber=?", - values: [biblio_id], - }).then(result => { - expect(result[0].count).to.be.equal(0); - }); + cy.task("deleteSampleObjects", objects); + + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM biblio WHERE biblionumber=?", + values: [biblio_id], + }).then(result => { + expect(result[0].count).to.be.equal(0); + }); + + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM items WHERE biblionumber=?", + values: [biblio_id], + }).then(result => { + expect(result[0].count).to.be.equal(0); + }); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM branches WHERE branchcode=?", - values: [objects.libraries[0].library_id], - }).then(result => { - expect(result[0].count).to.be.equal(0); + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM branches WHERE branchcode=?", + values: [objects.libraries[0].library_id], + }).then(result => { + expect(result[0].count).to.be.equal(0); + }); + + cy.task("query", { + sql: "SELECT COUNT(*) as count FROM itemtypes WHERE itemtype=?", + values: [objects.item_type.item_type_id], + }).then(result => { + expect(result[0].count).to.be.equal(0); + }); }); + }); + + it("insertSampleBiblio - options.different_libraries", () => { + cy.task("insertSampleBiblio", { + item_count: 3, + options: { different_libraries: true }, + }).then(objects => { + expect(objects.libraries.length).to.be.equal(3); + let libraries = objects.libraries + .map(library => library.library_id) + .sort(); + let itemsLibraries = objects.items + .map(item => item.home_library_id) + .sort(); + expect(libraries).deep.to.equal(itemsLibraries); - cy.task("query", { - sql: "SELECT COUNT(*) as count FROM itemtypes WHERE itemtype=?", - values: [objects.item_type.item_type_id], - }).then(result => { - expect(result[0].count).to.be.equal(0); + cy.task("deleteSampleObjects", objects); }); }); }); + + describe("insertSampleHold", () => { + it("insertSampleHold - item/biblio", () => { + cy.task("insertSampleBiblio", { item_count: 2 }).then( + objects_biblio => { + cy.task("insertSampleHold", { + item: objects_biblio.items[0], + }).then(objects_hold_1 => { + cy.task("insertSampleHold", { + biblio: objects_biblio.biblio, + library_id: objects_biblio.items[0].home_library_id, + }).then(objects_hold_2 => { + cy.task("apiGet", { + // No /holds/:hold_id (yet) + // No q={} (yet) + endpoint: `/api/v1/holds?hold_id=${objects_hold_1.hold.hold_id}`, + headers: { + "Content-Type": "application/json", + }, + }).then(holds => { + expect(holds.length).to.be.equal(1); + expect(holds[0].biblio_id).to.be.equal( + objects_biblio.biblio.biblio_id + ); + expect(holds[0].item_id).to.be.equal( + objects_biblio.items[0].item_id + ); + }); + cy.task("apiGet", { + // No /holds/:hold_id (yet) + // No q={} (yet) + endpoint: `/api/v1/holds?hold_id=${objects_hold_2.hold.hold_id}`, + headers: { + "Content-Type": "application/json", + }, + }).then(holds => { + expect(holds.length).to.be.equal(1); + expect(holds[0].biblio_id).to.be.equal( + objects_biblio.biblio.biblio_id + ); + expect(holds[0].item_id).to.be.equal(null); + }); + + cy.task("deleteSampleObjects", [ + objects_biblio, + objects_hold_1, + objects_hold_2, + ]); + }); + }); + } + ); + }); + + // How to properly test for Error? + it.skip("insertSampleHold - missing library_id", () => { + cy.task("insertSampleBiblio", { item_count: 2 }).then( + objects_biblio => { + cy.task("insertSampleHold", { + biblio: objects_biblio.biblio, + }).then( + () => { + throw new Error("Task should have failed"); + }, + err => { + expect(err.message).to.include( + "Could not generate sample hold without library_id or item" + ); + } + ); + } + ); + }); + }); }); diff --git a/t/cypress/plugins/insertData.js b/t/cypress/plugins/insertData.js index 954a86a8fa5..c8fdbdaea77 100644 --- a/t/cypress/plugins/insertData.js +++ b/t/cypress/plugins/insertData.js @@ -170,7 +170,14 @@ const insertSampleHold = async ({ authHeader, }) => { - library_id ||= item.home_library_id; + library_id ||= item?.home_library_id; + + if (!library_id) { + throw new Error( + "Could not generate sample hold without library_id or item" + ); + } + const generatedPatron = await buildSampleObject({ object: "patron", values: { library_id }, -- 2.34.1