Bug 5284 - Generic report covering various problems with the labels/patron card tests
[koha.git] / t / db_dependent / Labels / t_Batch.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2007 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #       
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
10 # version.
11 #
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.
15 #
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.
19
20 use strict;
21 use warnings;
22
23 use Test::More tests => 23;
24 use C4::Context;
25 use Data::Dumper;
26
27 BEGIN {
28     use_ok('C4::Labels::Batch');
29 }
30
31 my $sth = C4::Context->dbh->prepare('SELECT branchcode FROM branches b LIMIT 0,1');
32 $sth->execute();
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 = {
36         creator         => 'Labels',
37         items           => [],
38         branch_code     => $branch_code,
39         batch_stat      => 0,   # False if any data has changed and the db has not been updated
40     };
41
42 my $batch = 0;
43 my $item_number = 0;
44
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.";
50
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.";
55     }
56     else {
57         ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
58     }
59 }
60
61 diag "Testing Batch->add_item() method.";
62 my $sth1 = C4::Context->dbh->prepare('SELECT itemnumber FROM items LIMIT 0,10');
63 $sth1->execute();
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'};
68 }
69
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.";
73
74 diag "Testing Batch->remove_item() method.";
75
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.";
79
80 diag "Testing Batch->delete() method.";
81
82 my $del_results = $batch->delete();
83 ok($del_results eq 0) || diag "Batch->delete() FAILED.";