| 1 |
#!/usr/bin/perl -w |
| 2 |
# vi: set ts=2 sw=2 noai ic showmode showmatch: |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
|
| 7 |
use constant DEBUG => $ENV{DEBUG}; |
| 8 |
use if DEBUG, 'Smart::Comments'; |
| 9 |
|
| 10 |
use Test::More; |
| 11 |
use Finance::Quote; |
| 12 |
|
| 13 |
if (not $ENV{ONLINE_TEST}) { |
| 14 |
plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test'; |
| 15 |
} |
| 16 |
|
| 17 |
if ( not $ENV{"TEST_STOCKDATA_API_KEY"} ) { |
| 18 |
plan skip_all => 'Set $ENV{"TEST_STOCKDATA_API_KEY"} to run this test'; |
| 19 |
} |
| 20 |
|
| 21 |
my @valid = qw/CSCO F GE SWAV WM/; |
| 22 |
my @invalid = ('BUGUS'); |
| 23 |
|
| 24 |
my $q = Finance::Quote->new('StockData', timeout => 30); |
| 25 |
|
| 26 |
my %check = ( |
| 27 |
'currency' => sub { $_[0] =~ /^[A-Z]+$/ }, |
| 28 |
'date' => sub { $_[0] =~ m{^[0-9]{2}/[0-9]{2}/[0-9]{4}$} }, |
| 29 |
'isodate' => sub { $_[0] =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}$/ }, |
| 30 |
'last' => sub { $_[0] =~ /^[0-9.]+$/ }, |
| 31 |
'method' => sub { $_[0] =~ /^stockdata$/ }, |
| 32 |
'success' => sub { $_[0] }, |
| 33 |
'symbol' => sub { $_[0] eq $_[1] }, |
| 34 |
); |
| 35 |
|
| 36 |
plan tests => 1 + %check*@valid + @invalid; |
| 37 |
|
| 38 |
my %quotes = $q->fetch('stockdata', @valid); |
| 39 |
ok(%quotes); |
| 40 |
|
| 41 |
### [<now>] quotes: %quotes |
| 42 |
|
| 43 |
foreach my $symbol (@valid) { |
| 44 |
while (my ($key, $lambda) = each %check) { |
| 45 |
ok($lambda->($quotes{$symbol, $key}, $symbol), "$key -> $quotes{$symbol, $key}"); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
foreach my $symbol (@invalid) { |
| 50 |
ok((not $quotes{'BOGUS', 'success'}), 'failed as expected'); |
| 51 |
} |
| 52 |
|