Lines 1-8
Link Here
|
|
|
1 |
/** |
2 |
* Koha Cypress Testing Data Insertion Utilities |
3 |
* |
4 |
* This module provides functions to create and manage test data for Cypress tests. |
5 |
* It handles creating complete bibliographic records, patrons, holds, checkouts, |
6 |
* and other Koha objects with proper relationships and dependencies. |
7 |
* |
8 |
* @module insertData |
9 |
*/ |
10 |
|
1 |
const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); |
11 |
const { buildSampleObject, buildSampleObjects } = require("./mockData.js"); |
2 |
const { query } = require("./db.js"); |
12 |
const { query } = require("./db.js"); |
3 |
|
13 |
|
4 |
const { apiGet, apiPost } = require("./api-client.js"); |
14 |
const { apiGet, apiPost } = require("./api-client.js"); |
5 |
|
15 |
|
|
|
16 |
/** |
17 |
* Creates a complete bibliographic record with associated items and libraries. |
18 |
* |
19 |
* @async |
20 |
* @function insertSampleBiblio |
21 |
* @param {Object} params - Configuration parameters |
22 |
* @param {number} params.item_count - Number of items to create for this biblio |
23 |
* @param {Object} [params.options] - Additional options |
24 |
* @param {boolean} [params.options.different_libraries] - If true, creates different libraries for each item |
25 |
* @param {string} params.baseUrl - Base URL for API calls |
26 |
* @param {string} params.authHeader - Authorization header for API calls |
27 |
* @returns {Promise<Object>} Created biblio with items, libraries, and item_type |
28 |
* @returns {Object} returns.biblio - The created bibliographic record |
29 |
* @returns {Array<Object>} returns.items - Array of created item records |
30 |
* @returns {Array<Object>} returns.libraries - Array of created library records |
31 |
* @returns {Object} returns.item_type - The created item type record |
32 |
* @example |
33 |
* // Create a biblio with 3 items using the same library |
34 |
* const result = await insertSampleBiblio({ |
35 |
* item_count: 3, |
36 |
* baseUrl: 'http://localhost:8081', |
37 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
38 |
* }); |
39 |
* |
40 |
* @example |
41 |
* // Create a biblio with 2 items using different libraries |
42 |
* const result = await insertSampleBiblio({ |
43 |
* item_count: 2, |
44 |
* options: { different_libraries: true }, |
45 |
* baseUrl: 'http://localhost:8081', |
46 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
47 |
* }); |
48 |
*/ |
6 |
const insertSampleBiblio = async ({ |
49 |
const insertSampleBiblio = async ({ |
7 |
item_count, |
50 |
item_count, |
8 |
options, |
51 |
options, |
Lines 160-165
const insertSampleBiblio = async ({
Link Here
|
160 |
return { biblio, items: createdItems, libraries, item_type }; |
203 |
return { biblio, items: createdItems, libraries, item_type }; |
161 |
}; |
204 |
}; |
162 |
|
205 |
|
|
|
206 |
/** |
207 |
* Creates a hold request for a bibliographic record or item. |
208 |
* |
209 |
* @async |
210 |
* @function insertSampleHold |
211 |
* @param {Object} params - Configuration parameters |
212 |
* @param {Object} [params.item] - Item to place hold on (optional if biblio provided) |
213 |
* @param {Object} [params.biblio] - Biblio to place hold on (optional if item provided) |
214 |
* @param {string} [params.library_id] - Library ID for pickup location (defaults to item's home library) |
215 |
* @param {string} params.baseUrl - Base URL for API calls |
216 |
* @param {string} params.authHeader - Authorization header for API calls |
217 |
* @returns {Promise<Object>} Created hold with associated patron and patron_category |
218 |
* @returns {Object} returns.hold - The created hold record |
219 |
* @returns {Object} returns.patron - The patron who placed the hold |
220 |
* @returns {Object} returns.patron_category - The patron's category |
221 |
* @throws {Error} When neither library_id nor item is provided |
222 |
* @example |
223 |
* // Create a hold on a specific item |
224 |
* const holdResult = await insertSampleHold({ |
225 |
* item: { item_id: 123, home_library_id: 'CPL' }, |
226 |
* baseUrl: 'http://localhost:8081', |
227 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
228 |
* }); |
229 |
* |
230 |
* @example |
231 |
* // Create a biblio-level hold |
232 |
* const holdResult = await insertSampleHold({ |
233 |
* biblio: { biblio_id: 456 }, |
234 |
* library_id: 'CPL', |
235 |
* baseUrl: 'http://localhost:8081', |
236 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
237 |
* }); |
238 |
*/ |
163 |
const insertSampleHold = async ({ |
239 |
const insertSampleHold = async ({ |
164 |
item, |
240 |
item, |
165 |
biblio, |
241 |
biblio, |
Lines 199-204
const insertSampleHold = async ({
Link Here
|
199 |
return { hold, patron, patron_category }; |
275 |
return { hold, patron, patron_category }; |
200 |
}; |
276 |
}; |
201 |
|
277 |
|
|
|
278 |
/** |
279 |
* Creates a checkout record with associated biblio, item, and optional patron. |
280 |
* |
281 |
* @async |
282 |
* @function insertSampleCheckout |
283 |
* @param {Object} params - Configuration parameters |
284 |
* @param {Object} [params.patron] - Existing patron to check out to (creates new if not provided) |
285 |
* @param {string} params.baseUrl - Base URL for API calls |
286 |
* @param {string} params.authHeader - Authorization header for API calls |
287 |
* @returns {Promise<Object>} Created checkout with all associated records |
288 |
* @returns {Object} returns.biblio - The bibliographic record |
289 |
* @returns {Array<Object>} returns.items - Array of item records |
290 |
* @returns {Array<Object>} returns.libraries - Array of library records |
291 |
* @returns {Object} returns.item_type - The item type record |
292 |
* @returns {Object} returns.checkout - The checkout record |
293 |
* @returns {Object} [returns.patron] - The patron record (if generated) |
294 |
* @returns {Object} [returns.patron_category] - The patron category (if generated) |
295 |
* @example |
296 |
* // Create a checkout with a new patron |
297 |
* const checkoutResult = await insertSampleCheckout({ |
298 |
* baseUrl: 'http://localhost:8081', |
299 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
300 |
* }); |
301 |
* |
302 |
* @example |
303 |
* // Create a checkout for an existing patron |
304 |
* const checkoutResult = await insertSampleCheckout({ |
305 |
* patron: { patron_id: 123 }, |
306 |
* baseUrl: 'http://localhost:8081', |
307 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
308 |
* }); |
309 |
*/ |
202 |
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => { |
310 |
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => { |
203 |
const { biblio, items, libraries, item_type } = await insertSampleBiblio({ |
311 |
const { biblio, items, libraries, item_type } = await insertSampleBiblio({ |
204 |
item_count: 1, |
312 |
item_count: 1, |
Lines 248-253
const insertSampleCheckout = async ({ patron, baseUrl, authHeader }) => {
Link Here
|
248 |
}; |
356 |
}; |
249 |
}; |
357 |
}; |
250 |
|
358 |
|
|
|
359 |
/** |
360 |
* Creates a patron record with associated library and category. |
361 |
* |
362 |
* @async |
363 |
* @function insertSamplePatron |
364 |
* @param {Object} params - Configuration parameters |
365 |
* @param {Object} [params.library] - Library to assign patron to (creates new if not provided) |
366 |
* @param {Object} [params.patron_category] - Patron category to assign (creates new if not provided) |
367 |
* @param {string} params.baseUrl - Base URL for API calls |
368 |
* @param {string} params.authHeader - Authorization header for API calls |
369 |
* @returns {Promise<Object>} Created patron with associated records |
370 |
* @returns {Object} returns.patron - The created patron record |
371 |
* @returns {Object} [returns.library] - The library record (if generated) |
372 |
* @returns {Object} [returns.patron_category] - The patron category record (if generated) |
373 |
* @example |
374 |
* // Create a patron with new library and category |
375 |
* const patronResult = await insertSamplePatron({ |
376 |
* baseUrl: 'http://localhost:8081', |
377 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
378 |
* }); |
379 |
* |
380 |
* @example |
381 |
* // Create a patron for an existing library |
382 |
* const patronResult = await insertSamplePatron({ |
383 |
* library: { library_id: 'CPL' }, |
384 |
* baseUrl: 'http://localhost:8081', |
385 |
* authHeader: 'Basic dGVzdDp0ZXN0' |
386 |
* }); |
387 |
*/ |
251 |
const insertSamplePatron = async ({ |
388 |
const insertSamplePatron = async ({ |
252 |
library, |
389 |
library, |
253 |
patron_category, |
390 |
patron_category, |
Lines 324-329
const insertSamplePatron = async ({
Link Here
|
324 |
}; |
461 |
}; |
325 |
}; |
462 |
}; |
326 |
|
463 |
|
|
|
464 |
/** |
465 |
* Deletes test objects from the database in the correct order to respect foreign key constraints. |
466 |
* |
467 |
* @async |
468 |
* @function deleteSampleObjects |
469 |
* @param {Object|Array<Object>} allObjects - Object(s) to delete, can be single object or array |
470 |
* @returns {Promise<boolean>} True if deletion was successful |
471 |
* @description This function handles cleanup of test data by: |
472 |
* - Accepting single objects or arrays of objects |
473 |
* - Grouping objects by type (holds, checkouts, patrons, items, etc.) |
474 |
* - Deleting in dependency order to avoid foreign key violations |
475 |
* - Supporting all major Koha object types |
476 |
* @example |
477 |
* // Delete a single test result |
478 |
* await deleteSampleObjects(checkoutResult); |
479 |
* |
480 |
* @example |
481 |
* // Delete multiple test results |
482 |
* await deleteSampleObjects([biblioResult, holdResult, checkoutResult]); |
483 |
* |
484 |
* @example |
485 |
* // Delete after creating test data |
486 |
* const biblio = await insertSampleBiblio({ item_count: 2, baseUrl, authHeader }); |
487 |
* const hold = await insertSampleHold({ item: biblio.items[0], baseUrl, authHeader }); |
488 |
* // ... run tests ... |
489 |
* await deleteSampleObjects([biblio, hold]); |
490 |
*/ |
327 |
const deleteSampleObjects = async allObjects => { |
491 |
const deleteSampleObjects = async allObjects => { |
328 |
if (!Array.isArray(allObjects)) { |
492 |
if (!Array.isArray(allObjects)) { |
329 |
allObjects = [allObjects]; |
493 |
allObjects = [allObjects]; |
Lines 458-463
const deleteSampleObjects = async allObjects => {
Link Here
|
458 |
return true; |
622 |
return true; |
459 |
}; |
623 |
}; |
460 |
|
624 |
|
|
|
625 |
/** |
626 |
* Creates a library record via API, filtering out unsupported fields. |
627 |
* |
628 |
* @async |
629 |
* @function insertLibrary |
630 |
* @param {Object} params - Configuration parameters |
631 |
* @param {Object} params.library - Library object to insert |
632 |
* @param {string} params.baseUrl - Base URL for API calls |
633 |
* @param {string} params.authHeader - Authorization header for API calls |
634 |
* @returns {Promise<Object>} Created library record |
635 |
* @private |
636 |
* @description This is a helper function that removes fields not supported by the API |
637 |
* before creating the library record. |
638 |
*/ |
461 |
const insertLibrary = async ({ library, baseUrl, authHeader }) => { |
639 |
const insertLibrary = async ({ library, baseUrl, authHeader }) => { |
462 |
const { |
640 |
const { |
463 |
pickup_items, |
641 |
pickup_items, |
Lines 476-481
const insertLibrary = async ({ library, baseUrl, authHeader }) => {
Link Here
|
476 |
}); |
654 |
}); |
477 |
}; |
655 |
}; |
478 |
|
656 |
|
|
|
657 |
/** |
658 |
* Generic function to insert various types of Koha objects. |
659 |
* |
660 |
* @async |
661 |
* @function insertObject |
662 |
* @param {Object} params - Configuration parameters |
663 |
* @param {string} params.type - Type of object to insert ('library', 'item_type', 'hold', 'checkout', 'vendor', 'basket') |
664 |
* @param {Object} params.object - Object data to insert |
665 |
* @param {string} params.baseUrl - Base URL for API calls |
666 |
* @param {string} params.authHeader - Authorization header for API calls |
667 |
* @returns {Promise<Object|boolean>} Created object or true if successful |
668 |
* @throws {Error} When object type is not supported |
669 |
* @private |
670 |
* @description This is a generic helper function that handles the specifics of creating |
671 |
* different types of Koha objects. Each object type may require different field filtering, |
672 |
* API endpoints, or database operations. |
673 |
* |
674 |
* Supported object types: |
675 |
* - library: Creates library via API |
676 |
* - item_type: Creates item type via database query |
677 |
* - hold: Creates hold via API |
678 |
* - checkout: Creates checkout via API with confirmation token support |
679 |
* - vendor: Creates vendor via API |
680 |
* - basket: Creates basket via database query |
681 |
*/ |
479 |
const insertObject = async ({ type, object, baseUrl, authHeader }) => { |
682 |
const insertObject = async ({ type, object, baseUrl, authHeader }) => { |
480 |
if (type == "library") { |
683 |
if (type == "library") { |
481 |
const keysToKeep = ["library_id", "name"]; |
684 |
const keysToKeep = ["library_id", "name"]; |