From 2ba9f5b796a29210b85ac73265411235ca72bf98 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 16 Apr 2024 09:53:04 +0000 Subject: [PATCH] Bug 36607: Add cypress commands to build and delete objects This patch is a proof of concept for using test builder in cypress. The commands take in a Koha class and any data values and pass these to a perl script to run test builder To delete values we can either pass values to identify the data to delete or simply tell the command how many rows to delete from the end of the table --- t/cypress/support/cypress_builder.pl | 18 +++++++++++ t/cypress/support/e2e.js | 47 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 t/cypress/support/cypress_builder.pl diff --git a/t/cypress/support/cypress_builder.pl b/t/cypress/support/cypress_builder.pl new file mode 100644 index 0000000000..e59b7d4bf3 --- /dev/null +++ b/t/cypress/support/cypress_builder.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Koha::Database; + +use t::lib::TestBuilder; +use JSON qw( decode_json ); +use Getopt::Long; + +my %data; +my $class; + +GetOptions( "data=s" => \%data, "class=s" => \$class ); + +my $builder = t::lib::TestBuilder->new; + +my $koha_object = $builder->build_object({ class => $class, value => \%data }); \ No newline at end of file diff --git a/t/cypress/support/e2e.js b/t/cypress/support/e2e.js index afbb2ccea9..6ebf77b746 100644 --- a/t/cypress/support/e2e.js +++ b/t/cypress/support/e2e.js @@ -43,6 +43,53 @@ Cypress.Commands.add('login', (username, password) => { Cypress.Commands.add('left_menu_active_item_is', (label) => { cy.get("#navmenulist a.current:not(.disabled)").should('have.length',1).contains(label); }) + +/** + * Builds an object based on the provided class and data. + * + * @param {string} objectClass - The class of the object to be built. + * @param {Object} data - The data used to build the object. If not provided, a default object will be built. + * @return {void} This function does not return anything. + */ +Cypress.Commands.add('buildObject', (objectClass, data) => { + if (!objectClass) return + if (!data) { + const buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}` + cy.exec(buildCommand) + return + } + const properties = Object.keys(data) + const buildValues = properties.map(prop => ` --data ${prop}=${data[prop]}`) + const buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}${buildValues.join('')}` + cy.exec(buildCommand) +}) + +const tableMappings = { + 'Koha::Patrons': { tableName: 'borrowers', primaryKey: 'borrowernumber'} +} + +/** + * Deletes one or more rows from a database table based on the given className and delete parameters. + * + * @param {string} className - The name of the class to delete rows from. + * @param {Object} deleteParameters - The parameters used to filter the rows to be deleted. + * @param {number} numberOfRows - The number of rows to delete. If provided, rows will be deleted in descending order up to the value of numberOfRows. + * @return {void} This function does not return anything. + */ +Cypress.Commands.add('deleteDbRow', (className, deleteParameters, numberOfRows) => { + if(!className) return + if(numberOfRows) { + const sql = `DELETE from ${tableMappings[className].tableName} ORDER BY ${tableMappings[className].primaryKey} DESC LIMIT ${numberOfRows}` + cy.query(sql); + return + } + const properties = Object.keys(deleteParameters) + const deleteFilters = properties.map(prop => `${prop}=?`) + const deleteValues = properties.map(prop => deleteParameters[prop]) + const sql = `DELETE from ${tableMappings[className].tableName} WHERE ${deleteFilters.join(' AND ')}` + cy.query(sql, deleteValues); +}) + const dayjs = require("dayjs") /* Cannot use our calendar JS code, it's in an include file (!) Also note that moment.js is deprecated */ -- 2.37.1 (Apple Git-137.1)