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

(-)a/t/cypress/integration/DataTestSetupTeardown_spec.ts (-11 / +21 lines)
Lines 6-28 describe("Test data", () => { Link Here
6
        cy.title().should("eq", "Koha staff interface");
6
        cy.title().should("eq", "Koha staff interface");
7
    });
7
    });
8
8
9
    it("should create an object based on passed data", () => {
9
    // yarn cypress run --config video=false,screenshotOnRunFailure=false --spec t/cypress/integration/DataTest_spec.ts
10
11
    it("should create an object based on passed data and remove it", () => {
12
        var patron_count = 0;
13
        cy.query("SELECT COUNT(*) as count FROM borrowers").then(result => {
14
            patron_count = result[0].count;
15
        });
16
10
        const testPatronData = {
17
        const testPatronData = {
11
            firstname: "Cypress",
18
            firstname: "CypressTest",
12
            surname: "Test",
19
            surname: "SurnameTest",
20
            cardnumber: "td" + Math.floor(Math.random() * 8),
13
        };
21
        };
14
        
22
15
        cy.exec(`perl t/cypress/support/cypress_patron.pl --setup ${patron}`);
23
        cy.buildObject("Koha::Patrons", testPatronData);
16
        cy.exec(buildCommand)
24
17
        cy.query(
25
        cy.query(
18
            "SELECT firstname, surname FROM borrowers WHERE firstname=?",
26
            "SELECT firstname, surname FROM borrowers WHERE firstname=?",
19
            "Cypress"
27
            testPatronData.firstname
20
        ).then(result => {
28
        ).then(result => {
21
            expect(result[0].surname).to.equal('Test');
29
            expect(result[0].surname).to.equal(testPatronData.surname);
22
        });
30
        });
23
        cy.exec(`perl t/cypress/support/cypress_patron.pl --teardown ${patron}`);
24
31
25
    });
32
        cy.buildObject("Koha::Patrons", testPatronData, "teardown");
26
33
27
   
34
        cy.query("SELECT COUNT(*) as count FROM borrowers").then(result => {
35
            expect(result[0].count).to.equal(patron_count);
36
        });
37
    });
28
});
38
});
(-)a/t/cypress/support/cypress_builder.pl (-2 / +26 lines)
Lines 22-33 use Koha::Database; Link Here
22
use t::lib::TestBuilder;
22
use t::lib::TestBuilder;
23
use JSON qw( decode_json );
23
use JSON qw( decode_json );
24
use Getopt::Long;
24
use Getopt::Long;
25
use Koha::Patrons;
26
use Koha::Libraries;
25
27
26
my %data;
28
my %data;
27
my $class;
29
my $class;
30
my $teardown;
28
31
29
GetOptions( "data=s" => \%data, "class=s" => \$class );
32
GetOptions( "data=s" => \%data, "class=s" => \$class, "teardown" => \$teardown);
30
33
31
my $builder = t::lib::TestBuilder->new;
34
my $builder = t::lib::TestBuilder->new;
32
35
33
my $koha_object = $builder->build_object({ class => $class, value => \%data });
36
my $objects = {
37
    "Koha::Patrons" => { id => 'cardnumber', teardown => 'patron_delete', source => 'Borrower'},
38
};
39
40
die unless $data{$objects->{$class}->{id}};
41
42
if ($teardown) {
43
    my $sub = $objects->{$class}->{teardown};
44
    no strict 'refs';
45
    &$sub($data{$objects->{$class}->{id}});
46
} else {
47
    my $koha_object = $builder->build_object({ class => $class, value => \%data });
48
}
49
50
sub patron_delete {
51
    my $id = shift;
52
    my $patron = Koha::Patrons->find( {cardnumber => $id });
53
    my $branch = Koha::Libraries->find( {branchcode => $patron->branchcode });
54
   
55
    $builder->delete({ source => 'Borrower',  records => $patron});
56
    $builder->delete({ source => 'Branch',  records => $branch});   
57
}
(-)a/t/cypress/support/e2e.js (-2 / +5 lines)
Lines 51-64 Cypress.Commands.add('left_menu_active_item_is', (label) => { Link Here
51
 * @param {Object} data - The data used to build the object. If not provided, a default object will be built.
51
 * @param {Object} data - The data used to build the object. If not provided, a default object will be built.
52
 * @return {void} This function does not return anything.
52
 * @return {void} This function does not return anything.
53
 */
53
 */
54
Cypress.Commands.add('buildObject', (objectClass, data) => {
54
Cypress.Commands.add('buildObject', (objectClass, data, teardown = '') => {
55
    if (!objectClass) return
55
    if (!objectClass) return
56
    var buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}`
56
    var buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}`
57
    if (data) {
57
    if (data) {
58
        const properties = Object.keys(data)
58
        const properties = Object.keys(data)
59
        const buildValues = properties.map(prop => ` --data ${prop}=${data[prop]}`)
59
        const buildValues = properties.map(prop => ` --data ${prop}=${data[prop]}`)
60
        buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}${buildValues.join('')}`
60
        buildCommand = `perl t/cypress/support/cypress_builder.pl --class ${objectClass}${buildValues.join('')}`
61
    } 
61
    }
62
    if (teardown == 'teardown') {
63
        buildCommand += ` --teardown`
64
    }
62
    cy.exec(buildCommand).then((result) => {
65
    cy.exec(buildCommand).then((result) => {
63
       return result
66
       return result
64
    })
67
    })
(-)a/t/lib/TestBuilder.pm (-2 / +1 lines)
Lines 54-60 sub delete { Link Here
54
    foreach my $rec ( @recs ) {
54
    foreach my $rec ( @recs ) {
55
    # delete only works when you supply full primary key values
55
    # delete only works when you supply full primary key values
56
    # $cond does not include searches for undef (not allowed in PK)
56
    # $cond does not include searches for undef (not allowed in PK)
57
        my $cond = { map { defined $rec->{$_}? ($_, $rec->{$_}): (); } @pk };
57
        my $cond = { map { defined $rec->$_? ($_, $rec->$_): (); } @pk };
58
        next if keys %$cond < @pk;
58
        next if keys %$cond < @pk;
59
        $self->schema->resultset( $source )->search( $cond )->delete;
59
        $self->schema->resultset( $source )->search( $cond )->delete;
60
        # we clear the pk columns in the supplied hash
60
        # we clear the pk columns in the supplied hash
61
- 

Return to bug 36607