Bug 8524: Add a non-automatic barcode plugin
[koha.git] / t / Barcodes_ValueBuilder.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use DBI;
5 use Test::More tests => 15;
6 use Test::MockModule;
7
8 BEGIN {
9     use_ok('C4::Barcodes::ValueBuilder');
10 }
11
12
13 my $module = new Test::MockModule('C4::Context');
14 $module->mock('_new_dbh', sub {
15                              my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
16                              || die "Cannot create handle: $DBI::errstr\n";
17                              return $dbh });
18
19 # Mock data
20 my $incrementaldata = [
21     ['max(abs(barcode))'],
22     ['33333074344563'],
23 ];
24
25
26 my $dbh = C4::Context->dbh();
27
28 my %args = (
29     year        => '2012',
30     mon         => '07',
31     day         => '30',
32     tag         => '952',
33     subfield    => 'p',
34     loctag      => '952',
35     locsubfield => 'a'
36 );
37
38 $dbh->{mock_add_resultset} = $incrementaldata;
39 my ($nextnum, $scr, $history);
40
41 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
42 is($nextnum, 33333074344564, 'incremental barcode');
43 ok(length($scr) == 0, 'incremental javascript');
44
45 # This should run exactly one query so we can test
46 $history = $dbh->{mock_all_history};
47 is(scalar(@{$history}), 1, 'Correct number of statements executed for incremental barcode') ;
48
49 my $hbyymmincrdata = [
50     ['number'],
51     ['890'],
52 ];
53
54 $dbh->{mock_add_resultset} = $hbyymmincrdata;
55 $dbh->{mock_clear_history} = 1;
56 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
57 is($nextnum, '12070891', 'hbyymmincr barcode');
58 ok(length($scr) > 0, 'hbyymmincr javascript');
59
60 # This should run exactly one query so we can test
61 $history = $dbh->{mock_all_history};
62 is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;
63
64 my $annualdata = [
65     ['max(cast( substring_index(barcode, \'-\',-1) as signed))'],
66     ['34'],
67 ];
68
69 $dbh->{mock_add_resultset} = $annualdata;
70 $dbh->{mock_clear_history} = 1;
71 ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
72 is($nextnum, '2012-0035', 'annual barcode');
73 ok(length($scr) == 0, 'annual javascript');
74
75 # This should run exactly one query so we can test
76 $history = $dbh->{mock_all_history};
77 is(scalar(@{$history}), 1, 'Correct number of statements executed for hbyymmincr barcode') ;