From 464124020be2736889c1dbdb0a2d2f3290ede58f Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Thu, 23 Apr 2015 16:36:05 +0200 Subject: [PATCH] Bug 7957: Add unit tests for new subroutines Signed-off-by: Aleisha --- C4/Serials/RoutingLists.pm | 17 ++- t/db_dependent/Serials.t | 41 +++++- t/db_dependent/Serials/RoutingLists.t | 240 ++++++++++++++++++++++++++++++++++ 3 files changed, 295 insertions(+), 3 deletions(-) create mode 100644 t/db_dependent/Serials/RoutingLists.t diff --git a/C4/Serials/RoutingLists.pm b/C4/Serials/RoutingLists.pm index 0c38313..a891d86 100644 --- a/C4/Serials/RoutingLists.pm +++ b/C4/Serials/RoutingLists.pm @@ -49,14 +49,17 @@ this function create a new routing list for a subscription. sub AddRoutingList { my ($subscriptionid, $title) = @_; + return unless $subscriptionid and $title; + my $dbh = C4::Context->dbh(); my $query = qq{ INSERT INTO subscriptionroutinglist (subscriptionid, title) VALUES (?, ?) }; my $sth = $dbh->prepare($query); - $sth->execute($subscriptionid, $title); + my $rv = $sth->execute($subscriptionid, $title); + return unless $rv; return $dbh->last_insert_id(undef, undef, 'subscriptionroutinglist', undef); } @@ -71,6 +74,8 @@ this function modifies a routing list. sub ModRoutingList { my ($routinglistid, $subscriptionid, $title, $notes, @borrowernumbers) = @_; + return unless $routinglistid; + my $dbh = C4::Context->dbh; my $query = 'UPDATE subscriptionroutinglist'; my @setstrings = (); @@ -127,6 +132,8 @@ this function delete a routing list. sub DelRoutingList { my ($routinglistid) = @_; + return unless $routinglistid; + my $dbh = C4::Context->dbh; my $query = qq{ DELETE FROM subscriptionroutinglist @@ -150,6 +157,8 @@ to this routing list. sub GetRoutingList { my ($routinglistid) = @_; + return unless $routinglistid; + my $dbh = C4::Context->dbh; my $query = qq{ SELECT * @@ -177,7 +186,7 @@ sub GetRoutingList { =head2 GetRoutingLists -$routinglists = &GetRoutingLists($subscriptionid); +@routinglists = &GetRoutingLists($subscriptionid); this function get all routing lists for a subscription. @@ -186,6 +195,8 @@ this function get all routing lists for a subscription. sub GetRoutingLists { my ($subscriptionid) = @_; + return () unless $subscriptionid; + my $dbh = C4::Context->dbh; my $query = qq{ SELECT routinglistid @@ -240,6 +251,8 @@ this function return the routing list as a CSV file. sub GetRoutingListAsCSV { my ($routinglistid) = @_; + return unless $routinglistid; + require C4::Biblio; require C4::Serials; require C4::Members; diff --git a/t/db_dependent/Serials.t b/t/db_dependent/Serials.t index 018a4d5..fc9cbb8 100644 --- a/t/db_dependent/Serials.t +++ b/t/db_dependent/Serials.t @@ -14,8 +14,9 @@ use C4::Debug; use C4::Bookseller; use C4::Biblio; use C4::Budgets; +use C4::Items; use Koha::DateUtils; -use Test::More tests => 43; +use Test::More tests => 45; BEGIN { use_ok('C4::Serials'); @@ -240,4 +241,42 @@ for my $am ( @arrived_missing ) { } is( $subscription->{missinglist}, join('; ', @serialseqs), "subscription missinglist is updated after ModSerialStatus" ); +subtest 'GetSerial' => sub { + plan tests => 6; + + my $serial; + + $serial = GetSerial; + ok(not (defined $serial), 'GetSerial() returns undef'); + + $serial = GetSerial(-1); + ok(not (defined $serial), 'GetSerial(-1) returns undef'); + + my ($total_issues, @serials) = C4::Serials::GetSerials( $subscriptionid ); + $serial = GetSerial($serials[0]->{serialid}); + is($serial->{serialid}, $serials[0]->{serialid}, 'serialid is ok'); + is($serial->{serialseq}, $serials[0]->{serialseq}, 'serialseq is ok'); + is($serial->{status}, $serials[0]->{status}, 'status is ok'); + is($serial->{notes}, $serials[0]->{notes}, 'notes is ok'); +}; + +subtest 'GetSerialItemnumber' => sub { + plan tests => 3; + + my $itemnumber; + + $itemnumber = GetSerialItemnumber; + ok(not (defined $itemnumber), 'GetSerialItemnumber() returns undef'); + + $itemnumber = GetSerialItemnumber(-1); + ok(not (defined $itemnumber), 'GetSerialItemnumber(-1) returns undef'); + + my $item_itemnumber = C4::Items::AddItem({}, $biblionumber); + my ($total_issues, @serials) = C4::Serials::GetSerials( $subscriptionid ); + AddItem2Serial($serials[0]->{serialid}, $item_itemnumber); + $itemnumber = GetSerialItemnumber($serials[0]->{serialid}); + is($itemnumber, $item_itemnumber, + 'GetSerialItemnumber($serialid) returns itemnumber'); +}; + $dbh->rollback; diff --git a/t/db_dependent/Serials/RoutingLists.t b/t/db_dependent/Serials/RoutingLists.t new file mode 100644 index 0000000..ea492d4 --- /dev/null +++ b/t/db_dependent/Serials/RoutingLists.t @@ -0,0 +1,240 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 7; + +use C4::Serials; +use C4::Serials::RoutingLists; + +use C4::Biblio; +use C4::Members; + +use Koha::Database; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; + +my $record = MARC::Record->new; +my $field = C4::Context->preference('marcflavour') eq 'UNIMARC' + ? MARC::Field->new(200, '', '', a => 'Title') + : MARC::Field->new(245, '', '', a => 'Title'); +$record->append_fields($field); +my ($biblionumber) = AddBiblio($record, ''); + +my $subscriptionid = C4::Serials::NewSubscription(undef, '', (undef)x3, + $biblionumber, '2015-04-23', (undef)x10, '', '', (undef)x6, 0, '', 0, + undef, undef, 0, undef, undef, 0); + +my $schema = Koha::Database->new->schema; +my $categorycode = $schema->resultset('Category')->search->first->categorycode; +my $branchcode = $schema->resultset('Branch')->search->first->branchcode; + +my $borrowernumber1 = C4::Members::AddMember( + surname => 'Doe', + firstname => 'John', + categorycode => $categorycode, + branchcode => $branchcode, +); +my $borrowernumber2 = C4::Members::AddMember( + surname => 'Smith', + firstname => 'Jane', + categorycode => $categorycode, + branchcode => $branchcode, +); + +subtest 'AddRoutingList' => sub { + plan tests => 9; + + my $routinglistid; + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList; + ok(not (defined $routinglistid), + 'AddRoutingList with bad parameters should return undef'); + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList(-1); + ok(not (defined $routinglistid), + 'AddRoutingList with bad parameters should return undef'); + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid); + ok(not (defined $routinglistid), + 'AddRoutingList with bad parameters should return undef'); + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, ''); + ok(not (defined $routinglistid), + 'AddRoutingList with bad parameters should return undef'); + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); + ok((defined $routinglistid), + 'AddRoutingList with good parameters should return an id'); + + my $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + is($routinglist->{routinglistid}, $routinglistid, + 'new routing list id is ok'); + is($routinglist->{subscriptionid}, $subscriptionid, + 'new routing list subscriptionid is ok'); + is($routinglist->{title}, + 'Title', 'new routing list title is ok'); + ok(not (defined $routinglist->{notes}), + 'new routing list has not notes'); +}; + +subtest 'ModRoutingList' => sub { + plan tests => 5; + + my ($routinglist, $expected); + + my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + $expected = { + routinglistid => $routinglistid, + subscriptionid => $subscriptionid, + title => 'Title', + notes => undef, + }; + is_deeply($routinglist, $expected, 'routing list is as expected'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, 'Title 2'); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + $expected = { + routinglistid => $routinglistid, + subscriptionid => $subscriptionid, + title => 'Title 2', + notes => undef, + }; + is_deeply($routinglist, $expected, 'routing list is as expected'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, 'notes'); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + $expected = { + routinglistid => $routinglistid, + subscriptionid => $subscriptionid, + title => 'Title 2', + notes => 'notes', + }; + is_deeply($routinglist, $expected, 'routing list is as expected'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, + undef, $borrowernumber1, $borrowernumber2); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + $expected = { + routinglistid => $routinglistid, + subscriptionid => $subscriptionid, + title => 'Title 2', + notes => 'notes', + borrowers => [$borrowernumber1, $borrowernumber2], + }; + is_deeply($routinglist, $expected, 'routing list is as expected'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + $expected = { + routinglistid => $routinglistid, + subscriptionid => $subscriptionid, + title => 'Title 2', + notes => 'notes', + }; + is_deeply($routinglist, $expected, 'routing list is as expected'); +}; + +subtest 'DelRoutingList' => sub { + plan tests => 1; + + my $routinglist; + + my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); + C4::Serials::RoutingLists::DelRoutingList($routinglistid); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + ok(not (defined $routinglist), 'routing list is deleted'); +}; + +subtest 'GetRoutingList' => sub { + plan tests => 3; + + my $routinglist; + + $routinglist = C4::Serials::RoutingLists::GetRoutingList; + ok (not (defined $routinglist), 'GetRoutingList() returns undef'); + + $routinglist = C4::Serials::RoutingLists::GetRoutingList(-1); + ok (not (defined $routinglist), 'GetRoutingList(-1) returns undef'); + + my $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'Title'); + $routinglist = C4::Serials::RoutingLists::GetRoutingList($routinglistid); + ok ((defined $routinglist), 'GetRoutingList($routinglistid) returns undef'); +}; + +subtest 'GetRoutingLists' => sub { + plan tests => 4; + + my (@routinglists, $routinglistid); + + # Remove routing lists from previous tests + @routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); + foreach my $routinglist (@routinglists) { + C4::Serials::RoutingLists::DelRoutingList($routinglist->{routinglistid}); + } + + @routinglists = C4::Serials::RoutingLists::GetRoutingLists; + is(scalar @routinglists, 0, 'GetRoutingLists() returns an empty list'); + + @routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); + is(scalar @routinglists, 0, + 'GetRoutingLists($subscriptionid) returns an empty list'); + + $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 1'); + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, + undef, $borrowernumber1); + $routinglistid = C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 2'); + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, + undef, $borrowernumber1); + @routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); + is(scalar @routinglists, 2, + 'GetRoutingLists($subscriptionid) returns all routing lists'); + is_deeply($routinglists[0]->{borrowers}, [$borrowernumber1], + 'GetRoutingLists returns borrower list for each routing list'); +}; + +subtest 'GetRoutingListsCount' => sub { + plan tests => 3; + + my $count; + + # Remove routing lists from previous tests + my @routinglists = C4::Serials::RoutingLists::GetRoutingLists($subscriptionid); + foreach my $routinglist (@routinglists) { + C4::Serials::RoutingLists::DelRoutingList($routinglist->{routinglistid}); + } + + $count = C4::Serials::RoutingLists::GetRoutingListsCount; + is($count, undef, 'GetRoutingListsCount() returns undef'); + + $count = C4::Serials::RoutingLists::GetRoutingListsCount($subscriptionid); + is($count, 0, 'GetRoutingListsCount($subscriptionid) returns 0'); + + C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 1'); + C4::Serials::RoutingLists::AddRoutingList($subscriptionid, 'list 2'); + $count = C4::Serials::RoutingLists::GetRoutingListsCount($subscriptionid); + is($count, 2, 'GetRoutingListsCount($subscriptionid) returns 2'); +}; + +subtest 'GetRoutingListAsCSV' => sub { + plan tests => 1; + + my $routinglistid = C4::Serials::RoutingLists::AddRoutingList( + $subscriptionid, 'list 1'); + + C4::Serials::RoutingLists::ModRoutingList($routinglistid, undef, undef, + 'notes', $borrowernumber1, $borrowernumber2); + + my $csv = C4::Serials::RoutingLists::GetRoutingListAsCSV($routinglistid); + my $expected = + q|"Subscription title","Routing list",Surname,Firstname,Notes| . "\n" + . q|Title,"list 1",Doe,John,notes| . "\n" + . q|Title,"list 1",Smith,Jane,notes| . "\n"; + is($csv, $expected, 'csv is as expected'); +}; + +$dbh->rollback; -- 2.1.4