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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 use Test::More tests => 23;
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 = {
38 branch_code => $branch_code,
39 batch_stat => 0, # False if any data has changed and the db has not been updated
45 diag "Testing Batch->new() method.";
46 ok($batch = C4::Labels::Batch->new(branch_code => $branch_code)) || diag "Batch->new() FAILED.";
47 my $batch_id = $batch->get_attr('batch_id');
48 $expected_batch->{'batch_id'} = $batch_id;
49 is_deeply($batch, $expected_batch) || diag "New batch object FAILED to verify.";
51 diag "Testing Batch->get_attr() method.";
52 foreach my $key (keys %{$expected_batch}) {
53 if (ref($expected_batch->{$key}) eq 'ARRAY') {
54 ok(ref($expected_batch->{$key}) eq ref($batch->get_attr($key))) || diag "Batch->get_attr() FAILED on attribute $key.";
57 ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
61 diag "Testing Batch->add_item() method.";
62 my $sth1 = C4::Context->dbh->prepare('SELECT itemnumber FROM items LIMIT 0,10');
64 while (my $row = $sth1->fetchrow_hashref()) {
65 diag sprintf('Database returned the following error: %s', $sth1->errstr) if $sth1->errstr;
66 ok($batch->add_item($row->{'itemnumber'}) eq 0 ) || diag "Batch->add_item() FAILED.";
67 $item_number = $row->{'itemnumber'};
70 diag "Testing Batch->retrieve() method.";
71 ok(my $saved_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id)) || diag "Batch->retrieve() FAILED.";
72 is_deeply($saved_batch, $batch) || diag "Retrieved batch object FAILED to verify.";
74 diag "Testing Batch->remove_item() method.";
76 ok($batch->remove_item($item_number) eq 0) || diag "Batch->remove_item() FAILED.";
77 my $updated_batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
78 is_deeply($updated_batch, $batch) || diag "Updated batch object FAILED to verify.";
80 diag "Testing Batch->delete() method.";
82 my $del_results = $batch->delete();
83 ok($del_results eq 0) || diag "Batch->delete() FAILED.";