bug 5327: added some tests for C4/Items.pm
authorStacey Walker <stacey@catalyst-eu.net>
Mon, 11 Jun 2012 16:06:00 +0000 (17:06 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Fri, 6 Jul 2012 09:33:42 +0000 (11:33 +0200)
Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
t/Items.t [deleted file]
t/db_dependent/Items.t [new file with mode: 0755]

diff --git a/t/Items.t b/t/Items.t
deleted file mode 100755 (executable)
index c19cdc7..0000000
--- a/t/Items.t
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/usr/bin/perl
-#
-# This Koha test module is a stub!  
-# Add more tests here!!!
-
-use strict;
-use warnings;
-
-use Test::More tests => 1;
-
-BEGIN {
-        use_ok('C4::Items');
-}
-
diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t
new file mode 100755 (executable)
index 0000000..be209c8
--- /dev/null
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+#
+# This Koha test module is a stub!
+# Add more tests here!!!
+
+use strict;
+use warnings;
+use MARC::Record;
+use C4::Biblio;
+
+use Test::More tests => 7;
+
+BEGIN {
+    use_ok('C4::Items');
+}
+
+# Helper biblio.
+diag("Creating biblio instance for testing.");
+my ($bibnum, $bibitemnum) = get_biblio();
+
+# Add an item.
+my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => 'CPL', holdingbranch => 'CPL' } , $bibnum);
+cmp_ok($item_bibnum, '==', $bibnum, "New item is linked to correct biblionumber.");
+cmp_ok($item_bibitemnum, '==', $bibitemnum, "New item is linked to correct biblioitemnumber.");
+
+# Get item.
+my $getitem = GetItem($itemnumber);
+cmp_ok($getitem->{'itemnumber'}, '==', $itemnumber, "Retrieved item has correct itemnumber.");
+cmp_ok($getitem->{'biblioitemnumber'}, '==', $item_bibnum, "Retrieved item has correct biblioitemnumber.");
+
+# Modify item; setting barcode.
+ModItem({ barcode => '987654321' }, $bibnum, $itemnumber);
+my $moditem = GetItem($itemnumber);
+cmp_ok($moditem->{'barcode'}, '==', '987654321', 'Modified item barcode successfully to: '.$moditem->{'barcode'} . '.');
+
+# Delete item.
+my $dbh = C4::Context->dbh;
+DelItem($dbh, $bibnum, $itemnumber);
+my $getdeleted = GetItem($itemnumber);
+is($getdeleted->{'itemnumber'}, undef, "Item deleted as expected.");
+
+# Delete helper Biblio.
+diag("Deleting biblio testing instance.");
+DelBiblio($bibnum);
+
+# Helper method to set up a Biblio.
+sub get_biblio {
+    my $bib = MARC::Record->new();
+    $bib->append_fields(
+        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
+    );
+    return ($bibnum, $bibitemnum) = AddBiblio($bib, '');
+}