Bug 14334: Remove AutoCommit from tests
[koha.git] / t / db_dependent / MungeMarcPrice.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4 use C4::Biblio;
5 use Koha::Database;
6 use Koha::Acquisition::Currencies;
7 use Test::More;
8
9 # work around wide character warnings
10 binmode Test::More->builder->output, ":encoding(UTF-8)";
11 binmode Test::More->builder->failure_output, ":encoding(UTF-8)";
12
13 my $schema = Koha::Database->new->schema;
14 $schema->storage->txn_begin;
15 my $dbh = C4::Context->dbh;
16
17 # set some test price strings and expected output
18 my @prices2test=( { string => '25,5 £, $34,55, $LD35',       expected => '34.55' },
19                   { string => '32 EUR, 42.50$ USD, 54 CAD',  expected=>'42.50' },
20                   { string => '38.80 Ksh, ¥300, 51,50 USD',  expected => '51.50' },
21                   { string => '44 $, 33 €, 64 Br, £30',      expected => '44' },
22                   { string => '9 EUR,$34,55 USD,$7.35 CAN',  expected => '34.55' },
23                   { string => '$55.32',                      expected => '55.32' },
24                   { string => '9.99 USD (paperback)',        expected => '9.99' },
25                   { string => '$9.99 USD (paperback)',       expected => '9.99' },
26                   { string => '18.95 (U.S.)',                expected => '18.95' },
27                   { string => '$5.99 ($7.75 CAN)',           expected => '5.99' },
28                   { string => '5.99 (7.75 CAN)',             expected => '5.99' },
29                 );
30
31 plan tests => 2 * scalar @prices2test;
32
33 # set active currency test data
34 my $CURRENCY = 'TEST';
35 my $SYMBOL = '$';
36 my $ISOCODE = 'USD';
37 my $RATE= 1;
38
39 # disables existing active currency if necessary.
40 my $active_currency = Koha::Acquisition::Currencies->get_active;
41 my $curr;
42 if ($active_currency) {
43     $curr = $active_currency->currency;
44     $dbh->do("UPDATE currency set active = 0 where currency = '$curr'");
45 }
46
47 $dbh->do("INSERT INTO currency ( currency,symbol,isocode,rate,active )
48           VALUES ('$CURRENCY','$SYMBOL','$ISOCODE','$RATE',1)");
49 foreach my $price (@prices2test) {
50     is(
51         MungeMarcPrice($price->{'string'}),
52         $price->{'expected'},
53         "got expected price from $price->{'string'} (using currency.isocode)",
54     );
55 }
56
57 # run tests again, but fall back to currency name
58 $dbh->do('DELETE FROM aqbasket');
59 $dbh->do('DELETE FROM currency');
60 $dbh->do("INSERT INTO currency ( currency, symbol, rate, active )
61           VALUES ('$ISOCODE', '$SYMBOL', '$RATE', 1)");
62
63 foreach my $price (@prices2test) {
64     is(
65         MungeMarcPrice($price->{'string'}),
66         $price->{'expected'},
67         "got expected price from $price->{'string'} (using ISO code as currency name)",
68     );
69 }