|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use C4::Context; |
| 5 |
|
| 6 |
use Test::More tests => 47; |
| 7 |
use Test::MockModule; |
| 8 |
|
| 9 |
use_ok('C4::Acquisition'); |
| 10 |
|
| 11 |
my $module = new Test::MockModule('C4::Context'); |
| 12 |
$module->mock('_new_dbh', sub { |
| 13 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 14 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 15 |
return $dbh; |
| 16 |
}); |
| 17 |
|
| 18 |
my $dbh = C4::Context->dbh; |
| 19 |
|
| 20 |
# We need to add a resultset to avoid DBI fail |
| 21 |
# ("DBI bind_columns: invalid number of arguments...") |
| 22 |
my $rs = [ |
| 23 |
[qw(one two three four)], |
| 24 |
[1, 2, 3, 4] |
| 25 |
]; |
| 26 |
|
| 27 |
$dbh->{mock_add_resultset} = $rs; |
| 28 |
my @invoices = C4::Acquisition::GetInvoices( |
| 29 |
supplierid => "supplierid", |
| 30 |
invoicenumber => "invoicenumber", |
| 31 |
suppliername => "suppliername", |
| 32 |
shipmentdatefrom => "shipmentdatefrom", |
| 33 |
shipmentdateto => "shipmentdateto", |
| 34 |
billingdatefrom => "billingdatefrom", |
| 35 |
billingdateto => "billingdateto", |
| 36 |
isbneanissn => "isbneanissn", |
| 37 |
title => "title", |
| 38 |
author => "author", |
| 39 |
publisher => "publisher", |
| 40 |
publicationyear => "publicationyear", |
| 41 |
branchcode => "branchcode", |
| 42 |
); |
| 43 |
my $history = $dbh->{mock_all_history}; |
| 44 |
|
| 45 |
is(scalar(@$history), 1); |
| 46 |
my @bound_params = @{ $history->[0]->{bound_params} }; |
| 47 |
is(scalar(@bound_params), 15); |
| 48 |
is($bound_params[0], 'supplierid'); |
| 49 |
is($bound_params[1], '%invoicenumber%'); |
| 50 |
is($bound_params[2], '%suppliername%'); |
| 51 |
is($bound_params[3], 'shipmentdatefrom'); |
| 52 |
is($bound_params[4], 'shipmentdateto'); |
| 53 |
is($bound_params[5], 'billingdatefrom'); |
| 54 |
is($bound_params[6], 'billingdateto'); |
| 55 |
is($bound_params[7], 'isbneanissn'); |
| 56 |
is($bound_params[8], 'isbneanissn'); |
| 57 |
is($bound_params[9], 'isbneanissn'); |
| 58 |
is($bound_params[10], 'title'); |
| 59 |
is($bound_params[11], 'author'); |
| 60 |
is($bound_params[12], 'publisher'); |
| 61 |
is($bound_params[13], 'publicationyear'); |
| 62 |
is($bound_params[14], 'branchcode'); |
| 63 |
|
| 64 |
$dbh->{mock_clear_history} = 1; |
| 65 |
$dbh->{mock_add_resultset} = $rs; |
| 66 |
GetInvoice(42); |
| 67 |
$history = $dbh->{mock_all_history}; |
| 68 |
is(scalar(@$history), 1); |
| 69 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 70 |
is(scalar(@bound_params), 1); |
| 71 |
is($bound_params[0], 42); |
| 72 |
|
| 73 |
$dbh->{mock_clear_history} = 1; |
| 74 |
$dbh->{mock_add_resultset} = $rs; |
| 75 |
$dbh->{mock_add_resultset} = $rs; |
| 76 |
my $invoice = GetInvoiceDetails(42); |
| 77 |
$history = $dbh->{mock_all_history}; |
| 78 |
is(scalar(@$history), 2); |
| 79 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 80 |
is(scalar(@bound_params), 1); |
| 81 |
is($bound_params[0], 42); |
| 82 |
@bound_params = @{ $history->[1]->{bound_params} }; |
| 83 |
is(scalar(@bound_params), 1); |
| 84 |
is($bound_params[0], 42); |
| 85 |
ok(exists $invoice->{orders}); |
| 86 |
|
| 87 |
$dbh->{mock_clear_history} = 1; |
| 88 |
is(AddInvoice(booksellerid => 1), undef); # Fails because of a missing parameter |
| 89 |
$history = $dbh->{mock_all_history}; |
| 90 |
is(scalar(@$history), 0); |
| 91 |
|
| 92 |
$dbh->{mock_clear_history} = 1; |
| 93 |
AddInvoice(invoicenumber => 'invoice', booksellerid => 1, unknown => "unknown"); |
| 94 |
$history = $dbh->{mock_all_history}; |
| 95 |
is(scalar(@$history), 1); |
| 96 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 97 |
is(scalar(@bound_params), 2); |
| 98 |
ok(grep /^1$/, @bound_params); |
| 99 |
ok(grep /^invoice$/, @bound_params); |
| 100 |
ok(not grep /unknown/, @bound_params); |
| 101 |
|
| 102 |
$dbh->{mock_clear_history} = 1; |
| 103 |
is(ModInvoice(booksellerid => 1), undef); # Fails because of a missing parameter |
| 104 |
$history = $dbh->{mock_all_history}; |
| 105 |
is(scalar(@$history), 0); |
| 106 |
|
| 107 |
$dbh->{mock_clear_history} = 1; |
| 108 |
ModInvoice(invoiceid => 3, invoicenumber => 'invoice', unknown => "unknown"); |
| 109 |
$history = $dbh->{mock_all_history}; |
| 110 |
is(scalar(@$history), 1); |
| 111 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 112 |
is(scalar(@bound_params), 2); |
| 113 |
ok(grep /^3$/, @bound_params); |
| 114 |
ok(grep /^invoice$/, @bound_params); |
| 115 |
ok(not grep /unknown/, @bound_params); |
| 116 |
|
| 117 |
$dbh->{mock_clear_history} = 1; |
| 118 |
CloseInvoice(42); |
| 119 |
$history = $dbh->{mock_all_history}; |
| 120 |
is(scalar(@$history), 1); |
| 121 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 122 |
is(scalar(@bound_params), 1); |
| 123 |
is($bound_params[0], 42); |
| 124 |
|
| 125 |
$dbh->{mock_clear_history} = 1; |
| 126 |
ReopenInvoice(42); |
| 127 |
$history = $dbh->{mock_all_history}; |
| 128 |
is(scalar(@$history), 1); |
| 129 |
@bound_params = @{ $history->[0]->{bound_params} }; |
| 130 |
is(scalar(@bound_params), 1); |
| 131 |
is($bound_params[0], 42); |