From 55b5b84a32e12e5e7d41989d1c7ab71332380817 Mon Sep 17 00:00:00 2001 From: Mark Hofstetter Date: Mon, 6 May 2024 05:59:00 +0000 Subject: [PATCH] added teardown https://bugs.koha-community.org/show_bug.cgi?id=36607 --- .../integration/DataTestSetupTeardown_spec.ts | 32 ++++++++++++------- t/cypress/support/cypress_builder.pl | 28 ++++++++++++++-- t/cypress/support/e2e.js | 7 ++-- t/lib/TestBuilder.pm | 2 +- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/t/cypress/integration/DataTestSetupTeardown_spec.ts b/t/cypress/integration/DataTestSetupTeardown_spec.ts index fbcc8eddac..7aef9470f7 100644 --- a/t/cypress/integration/DataTestSetupTeardown_spec.ts +++ b/t/cypress/integration/DataTestSetupTeardown_spec.ts @@ -6,23 +6,33 @@ describe("Test data", () => { cy.title().should("eq", "Koha staff interface"); }); - it("should create an object based on passed data", () => { + // yarn cypress run --config video=false,screenshotOnRunFailure=false --spec t/cypress/integration/DataTest_spec.ts + + it("should create an object based on passed data and remove it", () => { + var patron_count = 0; + cy.query("SELECT COUNT(*) as count FROM borrowers").then(result => { + patron_count = result[0].count; + }); + const testPatronData = { - firstname: "Cypress", - surname: "Test", + firstname: "CypressTest", + surname: "SurnameTest", + cardnumber: "td" + Math.floor(Math.random() * 8), }; - - cy.exec(`perl t/cypress/support/cypress_patron.pl --setup ${patron}`); - cy.exec(buildCommand) + + cy.buildObject("Koha::Patrons", testPatronData); + cy.query( "SELECT firstname, surname FROM borrowers WHERE firstname=?", - "Cypress" + testPatronData.firstname ).then(result => { - expect(result[0].surname).to.equal('Test'); + expect(result[0].surname).to.equal(testPatronData.surname); }); - cy.exec(`perl t/cypress/support/cypress_patron.pl --teardown ${patron}`); - }); + cy.buildObject("Koha::Patrons", testPatronData, "teardown"); - + cy.query("SELECT COUNT(*) as count FROM borrowers").then(result => { + expect(result[0].count).to.equal(patron_count); + }); + }); }); diff --git a/t/cypress/support/cypress_builder.pl b/t/cypress/support/cypress_builder.pl index 7880ef8149..73c07f0e71 100644 --- a/t/cypress/support/cypress_builder.pl +++ b/t/cypress/support/cypress_builder.pl @@ -22,12 +22,36 @@ use Koha::Database; use t::lib::TestBuilder; use JSON qw( decode_json ); use Getopt::Long; +use Koha::Patrons; +use Koha::Libraries; my %data; my $class; +my $teardown; -GetOptions( "data=s" => \%data, "class=s" => \$class ); +GetOptions( "data=s" => \%data, "class=s" => \$class, "teardown" => \$teardown); my $builder = t::lib::TestBuilder->new; -my $koha_object = $builder->build_object({ class => $class, value => \%data }); \ No newline at end of file +my $objects = { + "Koha::Patrons" => { id => 'cardnumber', teardown => 'patron_delete', source => 'Borrower'}, +}; + +die unless $data{$objects->{$class}->{id}}; + +if ($teardown) { + my $sub = $objects->{$class}->{teardown}; + no strict 'refs'; + &$sub($data{$objects->{$class}->{id}}); +} else { + my $koha_object = $builder->build_object({ class => $class, value => \%data }); +} + +sub patron_delete { + my $id = shift; + my $patron = Koha::Patrons->find( {cardnumber => $id }); + my $branch = Koha::Libraries->find( {branchcode => $patron->branchcode }); + + $builder->delete({ source => 'Borrower', records => $patron}); + $builder->delete({ source => 'Branch', records => $branch}); +} \ No newline at end of file diff --git a/t/cypress/support/e2e.js b/t/cypress/support/e2e.js index 6b2fe35c5d..72b0e5d8e8 100644 --- a/t/cypress/support/e2e.js +++ b/t/cypress/support/e2e.js @@ -51,14 +51,17 @@ Cypress.Commands.add('left_menu_active_item_is', (label) => { * @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) => { +Cypress.Commands.add('buildObject', (objectClass, data, teardown = '') => { if (!objectClass) return var buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}` if (data) { const properties = Object.keys(data) const buildValues = properties.map(prop => ` --data ${prop}=${data[prop]}`) buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}${buildValues.join('')}` - } + } + if (teardown == 'teardown') { + buildCommand += ` --teardown` + } cy.exec(buildCommand).then((result) => { return result }) diff --git a/t/lib/TestBuilder.pm b/t/lib/TestBuilder.pm index 91fb518494..8c8147d265 100644 --- a/t/lib/TestBuilder.pm +++ b/t/lib/TestBuilder.pm @@ -54,7 +54,7 @@ sub delete { foreach my $rec ( @recs ) { # delete only works when you supply full primary key values # $cond does not include searches for undef (not allowed in PK) - my $cond = { map { defined $rec->{$_}? ($_, $rec->{$_}): (); } @pk }; + my $cond = { map { defined $rec->$_? ($_, $rec->$_): (); } @pk }; next if keys %$cond < @pk; $self->schema->resultset( $source )->search( $cond )->delete; # we clear the pk columns in the supplied hash -- 2.39.2