From ce29d71ea501d0182496d32c2de36a524156a11b Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Wed, 8 Mar 2017 17:34:28 +0200 Subject: [PATCH] Bug 18226 - Remove "use Test::DBIx::Class" instantiations' dangerous code duplication. Replace mock_dbh 2 Content-Type: text/plain; charset=utf-8 Couldn't make the tests pass using Test::DBIx::Class, so reverted to the "usual way" since these tests are in db_dependent anyway. --- t/db_dependent/Circulation/CalcDateDue.t | 40 ++++++++++++++------------------ t/lib/Mocks.pm | 9 ------- 2 files changed, 17 insertions(+), 32 deletions(-) diff --git a/t/db_dependent/Circulation/CalcDateDue.t b/t/db_dependent/Circulation/CalcDateDue.t index d1f39de..e533806 100644 --- a/t/db_dependent/Circulation/CalcDateDue.t +++ b/t/db_dependent/Circulation/CalcDateDue.t @@ -7,31 +7,29 @@ use Test::MockModule; use DBI; use DateTime; use t::lib::Mocks; - -BEGIN { - t::lib::Mocks::mock_dbh; -} +use t::lib::TestBuilder; use_ok('C4::Circulation'); -my $dbh = C4::Context->dbh(); +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; +my $builder = t::lib::TestBuilder->new; +my $categorycode = 'B'; +my $itemtype = 'MX'; +my $branchcode = 'FPL'; my $issuelength = 10; my $renewalperiod = 5; my $lengthunit = 'days'; -my $mock_undef = [ - [] -]; - -my $mock_loan_length = [ - ['issuelength', 'renewalperiod', 'lengthunit'], - [$issuelength, $renewalperiod, $lengthunit] -]; - -my $categorycode = 'B'; -my $itemtype = 'MX'; -my $branchcode = 'FPL'; +Koha::Database->schema->resultset('Issuingrule')->create({ + categorycode => $categorycode, + itemtype => $itemtype, + branchcode => $branchcode, + issuelength => $issuelength, + renewalperiod => $renewalperiod, + lengthunit => $lengthunit, +}); #Set syspref ReturnBeforeExpiry = 1 and useDaysMode = 'Days' t::lib::Mocks::mock_preference('ReturnBeforeExpiry', 1); @@ -41,10 +39,8 @@ my $dateexpiry = '2013-01-01'; my $borrower = {categorycode => 'B', dateexpiry => $dateexpiry}; my $start_date = DateTime->new({year => 2013, month => 2, day => 9}); -$dbh->{mock_add_resultset} = $mock_loan_length; my $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower ); is($date, $dateexpiry . 'T23:59:00', 'date expiry'); -$dbh->{mock_add_resultset} = $mock_loan_length; $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 ); @@ -54,11 +50,9 @@ t::lib::Mocks::mock_preference('useDaysMode', 'noDays'); $borrower = {categorycode => 'B', dateexpiry => $dateexpiry}; $start_date = DateTime->new({year => 2013, month => 2, day => 9}); -$dbh->{mock_add_resultset} = $mock_loan_length; $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower ); is($date, $dateexpiry . 'T23:59:00', 'date expiry'); -$dbh->{mock_add_resultset} = $mock_loan_length; $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 ); @@ -68,10 +62,10 @@ t::lib::Mocks::mock_preference('useDaysMode', 'Days'); $borrower = {categorycode => 'B', dateexpiry => $dateexpiry}; $start_date = DateTime->new({year => 2013, month => 2, day => 9}); -$dbh->{mock_add_resultset} = $mock_loan_length; $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower ); is($date, '2013-02-' . (9 + $issuelength) . 'T23:59:00', "date expiry ( 9 + $issuelength )"); -$dbh->{mock_add_resultset} = $mock_loan_length; $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 ); is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "date expiry ( 9 + $renewalperiod )"); + +$schema->storage->txn_rollback; diff --git a/t/lib/Mocks.pm b/t/lib/Mocks.pm index 46a87cb..da20739 100644 --- a/t/lib/Mocks.pm +++ b/t/lib/Mocks.pm @@ -41,13 +41,4 @@ sub mock_preference { }); } -sub mock_dbh { - our $context = new Test::MockModule('Koha::Database'); - $context->mock( '_new_schema', sub { - my $dbh = Koha::Schema->connect( 'DBI:Mock:', '', '' ); - return $dbh; - } ); - return $context; -} - 1; -- 2.1.4