3 # Copyright 2007 Foundations Bible College.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use Test::More tests => 22;
28 use_ok('C4::Labels::Batch');
31 my $sth = C4::Context->dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
33 my $branch_code = $sth->fetchrow_hashref()->{'branchcode'};
34 diag sprintf('Database returned the following error: %s', $sth->errstr) if $sth->errstr;
35 my $expected_batch = {
37 branch_code => $branch_code,
38 batch_stat => 0, # False if any data has changed and the db has not been updated
44 diag "Testing Batch->new() method.";
45 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code)) || diag "Batch->new() FAILED.";
46 my $batch_id = $batch->get_attr('batch_id');
47 $expected_batch->{'batch_id'} = $batch_id;
48 is_deeply($batch, $expected_batch) || diag "New batch object FAILED to verify.";
50 diag "Testing Batch->get_attr() method.";
51 foreach my $key (keys %{$expected_batch}) {
52 if (ref($expected_batch->{$key}) eq 'ARRAY') {
53 ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key))) || diag "Batch->get_attr() FAILED on attribute $key.";
56 ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
60 diag "Testing Batch->add_item() method.";
61 my $sth1 = C4::Context->dbh->prepare('SELECT itemnumber FROM items LIMIT 0,10');
63 while (my $row = $sth1->fetchrow_hashref()) {
64 diag sprintf('Database returned the following error: %s', $sth1->errstr) if $sth1->errstr;
65 ok($batch->add_item($row->{'itemnumber'}) eq 0 ) || diag "Batch->add_item() FAILED.";
66 $item_number = $row->{'itemnumber'};
69 diag "Testing Batch->retrieve() method.";
70 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id)) || diag "Batch->retrieve() FAILED.";
71 is_deeply($saved_batch, $batch) || diag "Retrieved batch object FAILED to verify.";
73 diag "Testing Batch->remove_item() method.";
75 ok($batch->remove_item($item_number) eq 0) || diag "Batch->remove_item() FAILED.";
76 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
77 is_deeply($updated_batch, $batch) || diag "Updated batch object FAILED to verify.";
79 diag "Testing Batch->delete() method.";
81 my $del_results = $batch->delete();
82 ok($del_results eq 0) || diag "Batch->delete() FAILED.";