From ffaa7218e556077c01fc82da6d116e77bd09d3cb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Sun, 1 Nov 2015 16:39:37 -0300 Subject: [PATCH] Bug 15081: (followup) Make test files using TestBuilder handle their transactions This trivial patch introduces the code needed on the test files so they handle the DB transaction instead of relying on the (removed) transaction started/rolled back by TestBuilder. --- t/db_dependent/Accounts.t | 4 +- t/db_dependent/Barcodes_ValueBuilder.t | 25 ++++++++++++ t/db_dependent/Borrower_Discharge.t | 25 +++++++++++- t/db_dependent/Category.t | 11 ++++++ t/db_dependent/Circulation/AnonymiseIssueHistory.t | 28 ++++++++++++++ t/db_dependent/Circulation/TooMany.t | 24 +++++++++++- t/db_dependent/Circulation_dateexpiry.t | 12 ++++++ t/db_dependent/Exporter/Record.t | 44 +++++++++++++++------- t/db_dependent/Holds.t | 14 +++---- t/db_dependent/Holidays.t | 9 +++-- t/db_dependent/Items.t | 4 +- t/db_dependent/Items/MoveItemFromBiblio.t | 8 ++++ t/db_dependent/Koha/Cities.t | 11 ++++++ .../Members/GetUpcomingMembershipExpires.t | 14 +++++-- t/db_dependent/Reserves/GetReserveFee.t | 12 +++++- 15 files changed, 207 insertions(+), 38 deletions(-) diff --git a/t/db_dependent/Accounts.t b/t/db_dependent/Accounts.t index cefbb3b..4fa582c 100644 --- a/t/db_dependent/Accounts.t +++ b/t/db_dependent/Accounts.t @@ -47,8 +47,10 @@ can_ok( 'C4::Accounts', WriteOffFee ) ); +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new(); -my $schema = Koha::Database->new()->schema(); my $dbh = C4::Context->dbh; $dbh->{RaiseError}=1; diff --git a/t/db_dependent/Barcodes_ValueBuilder.t b/t/db_dependent/Barcodes_ValueBuilder.t index 29f0b12..5d19423 100644 --- a/t/db_dependent/Barcodes_ValueBuilder.t +++ b/t/db_dependent/Barcodes_ValueBuilder.t @@ -1,15 +1,36 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + use Modern::Perl; + use Test::More tests => 7; use Test::MockModule; use t::lib::TestBuilder; +use Koha::Database; + BEGIN { use_ok('C4::Barcodes::ValueBuilder'); }; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; + my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM items|); my $item_1 = $builder->build({ @@ -52,3 +73,7 @@ ok(length($scr) > 0, 'hbyymmincr javascript'); ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args); is($nextnum, '2012-0035', 'annual barcode'); is($scr, undef, 'annual javascript'); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Borrower_Discharge.t b/t/db_dependent/Borrower_Discharge.t index ce96b22..d05b4a8 100644 --- a/t/db_dependent/Borrower_Discharge.t +++ b/t/db_dependent/Borrower_Discharge.t @@ -1,4 +1,18 @@ -#!/usr/bin/perl; +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . use Modern::Perl; use Test::More tests => 17; @@ -13,8 +27,13 @@ use C4::Members qw( AddMember GetMember ); use t::lib::TestBuilder; use Koha::Borrower::Discharge; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; + my $dbh = C4::Context->dbh; $dbh->do(q|DELETE FROM discharges|); @@ -102,3 +121,7 @@ else { # The error is: # DBIx::Class::ResultSet::create(): DBI Exception: DBD::mysql::st execute failed: Lock wait timeout exceeded; try restarting transaction [for Statement "INSERT INTO discharges ( borrower, needed, validated) VALUES ( ?, ?, ? )" with ParamValues: 0='121', 1='2014-01-08T16:38:29', 2=undef] at /home/koha/src/Koha/DataObject/Discharge.pm line 33 #is( Koha::Service::Borrower::Discharge::request({ borrowernumber => $borrower->{borrowernumber} }), 1, 'Discharge request sent' ); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Category.t b/t/db_dependent/Category.t index ebfeb26..93fd0df 100755 --- a/t/db_dependent/Category.t +++ b/t/db_dependent/Category.t @@ -18,14 +18,21 @@ # along with Koha; if not, see . use Modern::Perl; + use t::lib::TestBuilder; use Test::More tests => 3; +use Koha::Database; + BEGIN { use_ok('C4::Category'); } +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new(); + my $nonexistent_categorycode = 'NONEXISTEN'; $builder->build({ source => 'Category', @@ -49,3 +56,7 @@ ok( @categories, 'all returns categories' ); my $match = grep {$_->{categorycode} eq $nonexistent_categorycode } @categories; is( $match, 1, 'all returns the inserted category'); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Circulation/AnonymiseIssueHistory.t b/t/db_dependent/Circulation/AnonymiseIssueHistory.t index 213be0b..f4611eb 100644 --- a/t/db_dependent/Circulation/AnonymiseIssueHistory.t +++ b/t/db_dependent/Circulation/AnonymiseIssueHistory.t @@ -1,11 +1,35 @@ + +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; + use Test::More tests => 3; use C4::Context; use C4::Circulation; +use Koha::Database; + use t::lib::Mocks; use t::lib::TestBuilder; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; # TODO create a subroutine in t::lib::Mocks @@ -136,3 +160,7 @@ subtest 'AnonymousPatron is not defined' => sub { |, undef, $item->{itemnumber}); is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' ); }; + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Circulation/TooMany.t b/t/db_dependent/Circulation/TooMany.t index 9b32ea9..b29d2b0 100644 --- a/t/db_dependent/Circulation/TooMany.t +++ b/t/db_dependent/Circulation/TooMany.t @@ -1,5 +1,19 @@ #!/usr/bin/perl +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, see . + use Modern::Perl; use Test::More tests => 6; use C4::Context; @@ -12,13 +26,15 @@ use C4::Items; use C4::Context; use Koha::DateUtils qw( dt_from_string ); +use Koha::Database; use t::lib::TestBuilder; use t::lib::Mocks; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + our $dbh = C4::Context->dbh; -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM items|); @@ -389,7 +405,11 @@ subtest '1 BranchBorrowerCircRule exist: 1 CO allowed, 1 OSCO allowed' => sub { teardown(); }; +$schema->storage->txn_rollback; + sub teardown { $dbh->do(q|DELETE FROM issues|); $dbh->do(q|DELETE FROM issuingrules|); } + +1; diff --git a/t/db_dependent/Circulation_dateexpiry.t b/t/db_dependent/Circulation_dateexpiry.t index dfa4e4d..57f306e 100644 --- a/t/db_dependent/Circulation_dateexpiry.t +++ b/t/db_dependent/Circulation_dateexpiry.t @@ -22,10 +22,18 @@ use Time::HiRes qw/gettimeofday time/; use Test::More tests => 2; use C4::Members; use Koha::DateUtils; +use Koha::Database; + use t::lib::TestBuilder; use t::lib::Mocks qw( mock_preference ); +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new(); + +$ENV{ DEBUG } = 0; + subtest 'Tests for CanBookBeIssued related to dateexpiry' => sub { plan tests => 4; can_book_be_issued(); @@ -109,3 +117,7 @@ sub calc_date_due { my $t2 = time; is( ref $d eq "DateTime" && $t2 - $t1 < 1, 1, "CalcDateDue with expiry in year 9876 in " . sprintf( "%6.4f", $t2 - $t1 ) . " seconds." ); } + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Exporter/Record.t b/t/db_dependent/Exporter/Record.t index d5225e2..69362d8 100644 --- a/t/db_dependent/Exporter/Record.t +++ b/t/db_dependent/Exporter/Record.t @@ -1,32 +1,43 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + use Modern::Perl; + use Test::More tests => 3; +use t::lib::TestBuilder; + use MARC::Record; use MARC::File::USMARC; -use MARC::File::XML;# ( BinaryEncoding => 'utf-8' ); -#use XML::Simple; +use MARC::File::XML; use MARC::Batch; -use t::lib::TestBuilder; use File::Slurp; -#use utf8; use Encode; use C4::Biblio; use C4::Context; - +use Koha::Database; use Koha::Exporter::Record; -my $dbh = C4::Context->dbh; -#$dbh->{AutoCommit} = 0; -#$dbh->{RaiseError} = 1; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; -#$dbh->do(q|DELETE FROM issues|); -#$dbh->do(q|DELETE FROM reserves|); -#$dbh->do(q|DELETE FROM items|); -#$dbh->do(q|DELETE FROM biblio|); -#$dbh->do(q|DELETE FROM auth_header|); +my $dbh = C4::Context->dbh; my $biblio_1_title = 'Silence in the library'; -#my $biblio_2_title = Encode::encode('UTF-8', 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é'); my $biblio_2_title = 'The art of computer programming ກ ຂ ຄ ງ ຈ ຊ ຍ é'; my $biblio_1 = MARC::Record->new(); $biblio_1->leader('00266nam a22001097a 4500'); @@ -164,3 +175,8 @@ subtest 'export iso2709' => sub { $title = Encode::encode('UTF-8', $title); is( $title, $biblio_2_title, 'Export ISO2709: The title is correctly encoded' ); }; + +$schema->storage->txn_rollback; + +1; + diff --git a/t/db_dependent/Holds.t b/t/db_dependent/Holds.t index 203e715..f219f87 100755 --- a/t/db_dependent/Holds.t +++ b/t/db_dependent/Holds.t @@ -14,10 +14,9 @@ use C4::Biblio; use C4::Items; use C4::Members; use C4::Calendar; - -use Koha::Holds; - +use Koha::Database; use Koha::DateUtils qw( dt_from_string output_pref ); +use Koha::Holds; BEGIN { use FindBin; @@ -25,12 +24,11 @@ BEGIN { use_ok('C4::Reserves'); } -my $builder = t::lib::TestBuilder->new(); -my $dbh = C4::Context->dbh; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; -# Start transaction -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; +my $builder = t::lib::TestBuilder->new(); +my $dbh = C4::Context->dbh; # Create two random branches my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode }; diff --git a/t/db_dependent/Holidays.t b/t/db_dependent/Holidays.t index fb9b1fa..3080d5c 100755 --- a/t/db_dependent/Holidays.t +++ b/t/db_dependent/Holidays.t @@ -22,6 +22,7 @@ use t::lib::TestBuilder; use C4::Context; use C4::Branch; +use Koha::Database; use Koha::DateUtils; use DateTime; @@ -32,10 +33,10 @@ BEGIN { use_ok('C4::Calendar'); } +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $dbh = C4::Context->dbh(); -# Start transaction -$dbh->{AutoCommit} = 0; -$dbh->{RaiseError} = 1; my $builder = t::lib::TestBuilder->new(); # Create two fresh branches for the tests @@ -118,6 +119,6 @@ C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday( is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" ); is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1"); -$dbh->rollback; +$schema->storage->txn_rollback; 1; diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index ae9d4df..32c8ed2 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -34,7 +34,7 @@ BEGIN { use_ok('Koha::Items'); } -my $schema = Koha::Database->new->schema; +my $schema = Koha::Database->new->schema; my $branches = GetBranches; my ($branch1, $branch2) = keys %$branches; @@ -216,8 +216,6 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub { $schema->storage->txn_begin; - my $schema = Koha::Database->new()->schema(); - my $biblio = $schema->resultset('Biblio')->create( { diff --git a/t/db_dependent/Items/MoveItemFromBiblio.t b/t/db_dependent/Items/MoveItemFromBiblio.t index 1ded6f2..30ec6a2 100644 --- a/t/db_dependent/Items/MoveItemFromBiblio.t +++ b/t/db_dependent/Items/MoveItemFromBiblio.t @@ -20,9 +20,12 @@ use Test::More tests => 8; use C4::Items; use C4::Reserves; +use Koha::Database; use t::lib::TestBuilder; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $builder = t::lib::TestBuilder->new; # NOTE This is a trick, if we want to populate the biblioitems table, we should not create a Biblio but a Biblioitem @@ -82,3 +85,8 @@ my $get_item_level_hold_2 = C4::Reserves::GetReserve( $item_level_hold_to_move-> is( $get_bib_level_hold->{biblionumber}, $from_biblio->{biblionumber}, 'MoveItemFromBiblio should not have moved the biblio-level hold' ); is( $get_item_level_hold_1->{biblionumber}, $from_biblio->{biblionumber}, 'MoveItemFromBiblio should not have moved the item-level hold placed on item 1' ); is( $get_item_level_hold_2->{biblionumber}, $to_biblio->{biblionumber}, 'MoveItemFromBiblio should have moved the item-level hold placed on item 2' ); + +$schema->storage->txn_rollback; + +1; + diff --git a/t/db_dependent/Koha/Cities.t b/t/db_dependent/Koha/Cities.t index 1f90bd4..941dd75 100644 --- a/t/db_dependent/Koha/Cities.t +++ b/t/db_dependent/Koha/Cities.t @@ -18,11 +18,18 @@ # along with Koha; if not, see . use Modern::Perl; + use Test::More tests => 4; + use Koha::City; use Koha::Cities; +use Koha::Database; + use t::lib::TestBuilder; +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $builder = t::lib::TestBuilder->new; my $nb_of_cities = Koha::Cities->search->count; my $new_city_1 = Koha::City->new({ @@ -46,3 +53,7 @@ is( $retrieved_city_1->city_name, $new_city_1->city_name, 'Find a city by id sho $retrieved_city_1->delete; is( Koha::Cities->search->count, $nb_of_cities + 1, 'Delete should have deleted the city' ); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Members/GetUpcomingMembershipExpires.t b/t/db_dependent/Members/GetUpcomingMembershipExpires.t index 241858f..1026b92 100644 --- a/t/db_dependent/Members/GetUpcomingMembershipExpires.t +++ b/t/db_dependent/Members/GetUpcomingMembershipExpires.t @@ -18,18 +18,22 @@ # along with Koha; if not, see . use Modern::Perl; -use C4::Members; + +use Test::More tests => 5; use Test::MockModule; use t::lib::TestBuilder; use t::lib::Mocks qw( mock_preference ); -use Test::More tests => 5; -use Test::MockModule; +use C4::Members; +use Koha::Database; BEGIN { use_ok('C4::Members'); } +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + my $date_time = new Test::MockModule('DateTime'); $date_time->mock( 'now', sub { @@ -115,3 +119,7 @@ t::lib::Mocks::mock_preference('MembershipExpiryDaysNotice', undef); $upcoming_mem_expires = C4::Members::GetUpcomingMembershipExpires(); is(scalar(@$upcoming_mem_expires), 0, 'Get upcoming membership expires without MembershipExpiryDaysNotice should return 0.'); + +$schema->storage->txn_rollback; + +1; diff --git a/t/db_dependent/Reserves/GetReserveFee.t b/t/db_dependent/Reserves/GetReserveFee.t index ddc25ec..35b5bd1 100755 --- a/t/db_dependent/Reserves/GetReserveFee.t +++ b/t/db_dependent/Reserves/GetReserveFee.t @@ -20,12 +20,17 @@ # along with Koha; if not, see . use Modern::Perl; + use Test::More tests => 5; use Test::MockModule; +use t::lib::TestBuilder; use C4::Circulation; use C4::Reserves qw|AddReserve|; -use t::lib::TestBuilder; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; my $mContext = new Test::MockModule('C4::Context'); $mContext->mock( 'userenv', sub { @@ -104,7 +109,6 @@ $fee = C4::Reserves::GetReserveFee( $patron2->{borrowernumber}, $biblio->{biblio is( $fee > 0, 1, 'Patron 2 should be charged again this time' ); # End of tests - sub acctlines { #calculate number of accountlines for a patron my @temp = $dbh->selectrow_array( "SELECT COUNT(*) FROM accountlines WHERE borrowernumber=?", undef, ( $_[0] ) ); return $temp[0]; @@ -125,3 +129,7 @@ sub addreserve { '' ); } + +$schema->storage->txn_rollback; + +1; -- 2.6.2