1#!/usr/bin/perl -w
2
3use strict;
4use warnings;
5
6use constant DEBUG => $ENV{DEBUG};
7use if DEBUG, 'Smart::Comments';
8
9use Test::More;
10use Finance::Quote;
11use Date::Simple qw(today);
12use Scalar::Util qw(looks_like_number);
13use Date::Range;
14use Date::Manip;
15
16if (not $ENV{ONLINE_TEST}) {
17    plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
18}
19
20my %valid    = ('AGL' => 'ANGLO AMERICAN PLC - AGL',
21                'AMS' => 'ANGLO AMERICAN PLATINUM CORPORATION LIMITED - AMS'
22               );
23my @invalid  = ('BOGUS');
24my @symbols  = (keys %valid, @invalid);
25
26my $method   = 'za';     # Name of the target method for testing
27my $currency = 'ZAR';    # expected quote curreny
28my $today    = today();  # together with $window, validate date/isodate
29my $window   = 7;        # quote must be within last $window days
30
31
32my %check    = (# Tests are called with (value_to_test, symbol, quote_hash_reference)
33                'success'  => sub {$_[0]},
34                'currency' => sub {$_[0] eq $currency},
35                'name'     => sub {$_[0] eq $valid{$_[1]}},
36                'price'    => sub {looks_like_number($_[0])},
37                'isodate'  => sub {Date::Range->new($today - $window, $today + 1)->includes(Date::Simple::ISO->new($_[0]))},
38                'date'     => sub {my $a = Date::Manip::Date->new(); $a->parse_format('%m/%d/%Y', $_[0]);
39                                   my $b = Date::Manip::Date->new(); $b->parse_format('%Y-%m-%d', $_[2]->{$_[1], 'isodate'});
40                                   return $a->cmp($b) == 0;},
41               );
42my $q        = Finance::Quote->new();
43
44plan tests => 1 + %check*%valid + @invalid;
45
46my %quotes = $q->fetch($method, @symbols);
47ok(%quotes);
48
49### [<now>] quotes: %quotes
50
51foreach my $symbol (keys %valid) {
52  while (my ($key, $lambda) = each %check) {
53    ok($lambda->($quotes{$symbol, $key}, $symbol, \%quotes), "$key -> $quotes{$symbol, $key}");
54  }
55}
56
57foreach my $symbol (@invalid) {
58  ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
59}
60
61