| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
use constant DEBUG => $ENV{DEBUG}; |
| 4 |
use if DEBUG, Smart::Comments, '###'; |
| 5 |
|
| 6 |
use strict; |
| 7 |
use Test::More; |
| 8 |
use Finance::Quote; |
| 9 |
use Scalar::Util qw(looks_like_number); |
| 10 |
|
| 11 |
if (not $ENV{ONLINE_TEST}) { |
| 12 |
plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test'; |
| 13 |
} |
| 14 |
|
| 15 |
sub module_check |
| 16 |
{ |
| 17 |
my ($module, $valid, $invalid, $options) = @_; |
| 18 |
|
| 19 |
plan tests => 1 + 2*@{$valid} + @{$invalid}; |
| 20 |
|
| 21 |
my $hash = {order => [$module]}; |
| 22 |
$hash->{lc($module)} = $options if defined $options; |
| 23 |
|
| 24 |
my $q = Finance::Quote->new('currency_rates' => $hash); |
| 25 |
ok($q); |
| 26 |
|
| 27 |
foreach my $test (@{$valid}) { |
| 28 |
my ($from, $to) = @{$test}; |
| 29 |
my $v = $q->currency($from, $to); |
| 30 |
ok(looks_like_number($v), "$from -> $to = $v"); |
| 31 |
|
| 32 |
my ($from_amount, $from_code) = $from =~ m/^([0-9.]+) ([A-Z]+)$/; |
| 33 |
SKIP: { |
| 34 |
skip "identity check because different currencies", 1 unless $from_code eq $to; |
| 35 |
|
| 36 |
my ($suffix) = $from_amount =~ m/[.]([0-9]+)/; |
| 37 |
my $string = sprintf("%.*f", length($suffix), $v); |
| 38 |
|
| 39 |
ok($from_amount eq $string, "identity check"); |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
foreach my $test (@{$invalid}) { |
| 44 |
my ($from, $to) = @{$test}; |
| 45 |
my $v = $q->currency($from, $to); |
| 46 |
is($v, undef, "$from -> $to failed as expected"); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
plan tests => 1; |
| 51 |
|
| 52 |
subtest 'OpenExchange' => sub { |
| 53 |
if ( not $ENV{TEST_OPENEXCHANGE_API_KEY} ) { |
| 54 |
plan skip_all => |
| 55 |
'Set $ENV{TEST_OPENEXCHANGE_API_KEY} to run this test; get one at https://openexchangerates.org'; |
| 56 |
} |
| 57 |
|
| 58 |
my @valid = (['100.00 USD', 'EUR'], ['1.00 GBP', 'IDR'], ['1.23 IDR', 'CAD'], ['10.00 AUD', 'AUD']); |
| 59 |
my @invalid = (['20.12 ZZZ', 'GBP']); |
| 60 |
|
| 61 |
module_check('OpenExchange', \@valid, \@invalid, {cache => 1, API_KEY => $ENV{TEST_OPENEXCHANGE_API_KEY}}); |
| 62 |
}; |
| 63 |
|