Added unit tests using a test database for XISBN.
[koha.git] / t / db_dependent / Barcodes.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 49;
7 BEGIN {
8         use FindBin;
9         use lib $FindBin::Bin;
10         use_ok('C4::Barcodes');
11 }
12
13 my %thash = (
14         incremental => [],
15         annual => [],
16         hbyymmincr => ['MAIN'],
17 );
18
19 print "\n";
20 my ($obj1,$obj2,$format,$value,$initial,$serial,$re,$next,$previous,$temp);
21 my @formats = sort keys %thash;
22 foreach (@formats) {
23         my $pre = sprintf '(%-12s)', $_;
24         ok($obj1 = C4::Barcodes->new($_),           "$pre Barcode Creation : new($_)");
25         SKIP: {
26                 skip "No Object Returned by new($_)", 17 unless $obj1;
27         ok($_ eq ($format = $obj1->autoBarcode()),  "$pre autoBarcode()    : " . ($format || 'FAILED') );
28         ok($initial= $obj1->initial(),              "$pre initial()        : " . ($initial|| 'FAILED') );
29         $temp = $obj1->db_max();
30         diag ".    $pre db_max()         : " . ($temp   || 'Database Empty or No Matches') ;
31         ok($temp   = $obj1->max(),                  "$pre max()            : " . ($temp   || 'FAILED') );
32         ok($value  = $obj1->value(),                "$pre value()          : " . ($value  || 'FAILED') );
33         ok($serial = $obj1->serial(),               "$pre serial()         : " . ($serial || 'FAILED') );
34         ok($temp   = $obj1->is_max(),               "$pre obj1->is_max() [obj1 should currently be max]");
35         diag "Creating new Barcodes object (obj2) based on the old one (obj1)\n";
36         ok($obj2   = $obj1->new(),                  "$pre Barcode Creation : obj2 = obj1->new()");
37         diag ".    $pre obj2->value: " . $obj2->value . "\n";
38         ok(not($obj1->is_max()),                    "$pre obj1->is_max() [obj1 should no longer be max]");
39         ok(    $obj2->is_max(),                     "$pre obj2->is_max() [obj2 should currently be max]");
40         ok($obj2->serial == $obj1->serial + 1,      "$pre obj2->serial()   : " . ($obj2->serial || 'FAILED'));
41         ok($previous = $obj2->previous(),           "$pre obj2->previous() : " . ($previous     || 'FAILED'));
42         ok($next     = $obj1->next(),               "$pre obj1->next()     : " . ($next         || 'FAILED'));
43         ok($next->previous()->value() eq $obj1->value(),  "$pre Roundtrip, value : " . ($obj1->value || 'FAILED'));
44         ok($previous->next()->value() eq $obj2->value(),  "$pre Roundtrip, value : " . ($obj2->value || 'FAILED'));
45         }
46         print "\n";
47 }
48
49 diag "\nTesting with valid inputs:\n";
50 foreach $format (@formats) {
51         my $pre = sprintf '(%-12s)', $format;
52   foreach my $testval (@{$thash{ $format }}) {
53         ok($obj1 = C4::Barcodes->new($format,$testval),    "$pre Barcode Creation : new('$format','$testval')");
54         if ($format eq 'hbyymmincr') {
55                 diag "\nExtra tests for hbyymmincr\n";
56                 $obj2 = $obj1->new();
57                 my $branch;
58                 ok($branch = $obj1->branch(),   "$pre branch() : " . ($branch || 'FAILED') );
59                 ok($branch eq $obj2->branch(),  "$pre branch extended to derived object : " . ($obj2->branch || 'FAILED'));
60         }
61         print "\n";
62   }
63 }
64
65 diag "done.\n";