removed reports/inventory.pl
[koha.git] / t / VirtualShelves.t
blobf3f3b352d61e21b736d82267390ada288a62080f
1 #!/usr/bin/perl
4 # This file is a test script for C4::VirtualShelves.pm
5 # Author : Antoine Farnault, antoine@koha-fr.org
8 use Test;
9 use strict;
10 use C4::Context;
12 # Making 30 tests.
13 BEGIN { plan tests => 30 }
15 # Getting some borrowers from database.
16 my $dbh = C4::Context->dbh;
17 my $query = qq/
18 SELECT borrowernumber
19 FROM borrowers
20 LIMIT 10
22 my $sth = $dbh->prepare($query);
23 $sth->execute;
24 my @borrowers;
25 while(my $borrower = $sth->fetchrow){
26 push @borrowers, $borrower;
29 # Getting some itemnumber from database
30 my $query = qq/
31 SELECT itemnumber
32 FROM items
33 LIMIT 10
35 my $sth = $dbh->prepare($query);
36 $sth->execute;
37 my @items;
38 while(my $item = $sth->fetchrow){
39 push @items, $item;
42 # Getting some biblionumbers from database
43 my $query = qq/
44 SELECT biblionumber
45 FROM biblio
46 LIMIT 10
48 my $sth = $dbh->prepare($query);
49 $sth->execute;
50 my @biblionumbers;
51 while(my $biblionumber = $sth->fetchrow){
52 push @biblionumbers, $biblionumber;
55 # ---
56 my $delete_virtualshelf = qq/
57 DELETE FROM virtualshelf WHERE 1
59 my $delete_virtualshelfcontent =qq/
60 DELETE FROM shelfcontents WHERE 1
63 my $sth = $dbh->prepare($delete_virtualshelf);
64 $sth->execute;
65 my $sth = $dbh->prepare($delete_virtualshelfcontent);
66 $sth->execute;
67 # ---
69 #----------------------------------------------------------------------#
71 # TESTS START HERE
73 #----------------------------------------------------------------------#
75 use C4::VirtualShelves;
76 my $version = C4::VirtualShelves->VERSION;
77 print "\n----------Testing C4::VirtualShelves version ".$version."--------\n";
79 ok($version); # First test: the module is loaded & the version is readable.
82 #-----------------------TEST AddShelf function------------------------#
83 # usage : $shelfnumber = &AddShelf( $shelfname, $owner, $category);
85 # creating 10 good shelves.
86 my @shelves;
87 for(my $i=0; $i<10;$i++){
88 my $ShelfNumber = AddShelf("Shelf_".$i,$borrowers[$i],int(rand(3))+1);
89 die "test Not ok, remove some shelves before" if ($ShelfNumber == -1);
90 ok($ShelfNumber); # Shelf creation successful;
91 push @shelves, $ShelfNumber if ok($ShelfNumber);
94 ok(10,scalar @shelves); # 10 shelves in @shelves;
96 # try to create some shelf which already exists.
97 for(my $i=0;$i<10;$i++){
98 my $badNumShelf = AddShelf("Shelf_".int(rand(9)),'','');
99 ok(-1,$badNumShelf); # AddShelf returns -1 if name already exist.
102 #-----------TEST AddToShelf & &AddToShelfFromBiblio & GetShelfContents & DelFromShelf functions--------------#
103 # usage : &AddToShelf($itemnumber, $shelfnumber);
104 # usage : $itemlist = &GetShelfContents($shelfnumber);
105 # usage : $itemlist = GetShelfContents($shelfnumber);
107 for(my $i=0; $i<10;$i++){
108 my $item = $items[int(rand(9))];
109 my $shelfnumber = $shelves[int(rand(9))];
111 my $itemlistBefore = GetShelfContents($shelfnumber);
112 AddToShelf($item,$shelfnumber);
113 my $itemlistAfter = GetShelfContents($shelfnumber);
114 ok(scalar @$itemlistBefore,scalar (@$itemlistAfter - 1)); # the item has been successfuly added.
117 # same thing with AddToShelfFromBiblio
118 my $biblionumber = $biblionumbers[int(rand(10))];
119 &AddToShelfFromBiblio($biblionumber, $shelfnumber);
120 my $AfterAgain = GetShelfContents($shelfnumber);
121 ok(scalar @$itemlistAfter, scalar (@$AfterAgain -1));
124 #-----------------------TEST ModShelf & GetShelf functions------------------------#
125 # usage : ModShelf($shelfnumber, $shelfname, $owner, $category )
126 # usage : (shelfnumber,shelfname,owner,category) = GetShelf($shelfnumber);
128 for(my $i=0; $i<10;$i++){
129 my $rand = int(rand(9));
130 my $numA = $shelves[$rand];
131 my $nameA = "NewName_".$rand;
132 my $ownerA = $borrowers[$rand];
133 my $categoryA = int(rand(3))+1;
135 ModShelf($numA,$nameA,$ownerA,$categoryA);
136 my ($numB,$nameB,$ownerB,$categoryB) = GetShelf($numA);
138 ok($numA,$numB);
139 ok($nameA,$nameB);
140 ok($ownerB,$ownerA);
141 ok($categoryA,$categoryB);
144 #-----------------------TEST DelShelf & DelFromShelf functions------------------------#
145 # usage : ($status) = &DelShelf($shelfnumber);
146 # usage : &DelFromShelf( $itemnumber, $shelfnumber);
148 for(my $i=0; $i<10;$i++){
149 my $shelfnumber = $shelves[$i];
150 my $status = DelShelf($shelfnumber);
151 if($status){
152 my $items = GetShelfContents($shelfnumber);
153 ok($status,scalar @$items);
154 foreach (@$items){ # delete all the item in this shelf
155 DelFromShelf($_{'itemnumber'},$shelfnumber);
157 ok(DelShelf($shelfnumber));