Bug 10289: UT: Reserves.t needs to create its own data
authorJonathan Druart <jonathan.druart@biblibre.com>
Mon, 20 May 2013 14:37:10 +0000 (16:37 +0200)
committerGalen Charlton <gmc@esilibrary.com>
Mon, 8 Jul 2013 14:23:16 +0000 (14:23 +0000)
Try before the patch:
prove t/db_dependent/Reserves.t

And after, it should produce:
  t/db_dependent/Reserves.t .. 1/4 #
  # Creating biblio instance for testing.
  # Creating item instance for testing.
  # Deleting item testing instance.
  # Deleting biblio testing instance.
  # Deleting borrower.
  t/db_dependent/Reserves.t .. ok
  All tests successful.
  Files=1, Tests=4,  1 wallclock secs ( 0.02 usr  0.01 sys +  0.39 cusr  0.02 csys =  0.44 CPU)
  Result: PASS

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
t/db_dependent/Reserves.t

index 1e20318..2033620 100755 (executable)
@@ -1,25 +1,30 @@
 #!/usr/bin/perl
 
-use strict;
-use warnings;
-use C4::Branch;
+use Modern::Perl;
 
 use Test::More tests => 4;
 use MARC::Record;
+
+use C4::Branch;
 use C4::Biblio;
 use C4::Items;
+use C4::Members;
 
 BEGIN {
-       use FindBin;
-       use lib $FindBin::Bin;
-       use_ok('C4::Reserves');
+    use_ok('C4::Reserves');
 }
 
 # Setup Test------------------------
 # Helper biblio.
 diag("\nCreating biblio instance for testing.");
-my ($bibnum, $title, $bibitemnum) = create_helper_biblio();
-
+my $bib = MARC::Record->new();
+my $title = 'Silence in the library';
+$bib->append_fields(
+    MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+    MARC::Field->new('245', ' ', ' ', a => $title),
+);
+my ($bibnum, $bibitemnum);
+($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
 # Helper item for that biblio.
 diag("Creating item instance for testing.");
 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
@@ -28,16 +33,16 @@ my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL'
 my $testbarcode = '97531';
 ModItem({ barcode => $testbarcode }, $bibnum, $itemnumber);
 
-# Get a borrower
-my $dbh = C4::Context->dbh;
-my $query = qq/SELECT borrowernumber
-    FROM   borrowers
-    LIMIT  1/;
-my $sth = $dbh->prepare($query);
-$sth->execute;
-my $borrower = $sth->fetchrow_hashref;
-
-my $borrowernumber = $borrower->{'borrowernumber'};
+# Create a borrower
+my %data = (
+    cardnumber => 'CARDNUMBER42',
+    firstname =>  'my firstname',
+    surname => 'my surname',
+    categorycode => 'S',
+    branchcode => 'CPL',
+);
+my $borrowernumber = AddMember(%data);
+my $borrower = GetMember( borrowernumber => $borrowernumber );
 my $biblionumber   = $bibnum;
 my $barcode        = $testbarcode;
 
@@ -56,33 +61,28 @@ my $branch = $branches[0][0]{value};
 AddReserve($branch,    $borrowernumber, $biblionumber,
         $constraint, $bibitems,  $priority, $resdate, $expdate, $notes,
         $title,      $checkitem, $found);
-        
+
 my ($status, $reserve, $all_reserves) = CheckReserves($itemnumber, $barcode);
-ok($status eq "Reserved", "CheckReserves Test 1");
+
+is($status, "Reserved", "CheckReserves Test 1");
 
 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
-ok($status eq "Reserved", "CheckReserves Test 2");
+is($status, "Reserved", "CheckReserves Test 2");
 
 ($status, $reserve, $all_reserves) = CheckReserves(undef, $barcode);
-ok($status eq "Reserved", "CheckReserves Test 3");
+is($status, "Reserved", "CheckReserves Test 3");
 
 
 # Teardown Test---------------------
 # Delete item.
 diag("Deleting item testing instance.");
+my $dbh = C4::Context->dbh;
 DelItem($dbh, $bibnum, $itemnumber);
 
 # Delete helper Biblio.
 diag("Deleting biblio testing instance.");
 DelBiblio($bibnum);
 
-# Helper method to set up a Biblio.
-sub create_helper_biblio {
-    my $bib = MARC::Record->new();
-    my $title = 'Silence in the library';
-    $bib->append_fields(
-        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
-        MARC::Field->new('245', ' ', ' ', a => $title),
-    );
-    return ($bibnum, $title, $bibitemnum) = AddBiblio($bib, '');
-}
+# Delete borrower
+diag("Deleting borrower.");
+DelMember($borrowernumber);