Fix FSF address in directory t/
[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 => 22;
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         items           => [],
37         branch_code     => $branch_code,
38         batch_stat      => 0,   # False if any data has changed and the db has not been updated
39     };
40
41 my $batch = 0;
42 my $item_number = 0;
43
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.";
49
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.";
54     }
55     else {
56         ok($expected_batch->{$key} eq $batch->get_attr($key)) || diag "Batch->get_attr() FAILED on attribute $key.";
57     }
58 }
59
60 diag "Testing Batch->add_item() method.";
61 my $sth1 = C4::Context->dbh->prepare('SELECT itemnumber FROM items LIMIT 0,10');
62 $sth1->execute();
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'};
67 }
68
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.";
72
73 diag "Testing Batch->remove_item() method.";
74
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.";
78
79 diag "Testing Batch->delete() method.";
80
81 my $del_results = $batch->delete();
82 ok($del_results eq 0) || diag "Batch->delete() FAILED.";