Debian Codegraph @

t/za.t

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 use constant DEBUG => $ENV{DEBUG};
7 use if DEBUG, 'Smart::Comments';
8
9 use Test::More;
10 use Finance::Quote;
11 use Date::Simple qw(today);
12 use Scalar::Util qw(looks_like_number);
13 use Date::Range;
14 use Date::Manip;
15
16 if (not $ENV{ONLINE_TEST}) {
17 plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
18 }
19
20 my %valid = ('AGL' => 'ANGLO AMERICAN PLC - AGL',
21 'MTN' => 'MTN GROUP LIMITED - MTN'
22 );
23 my @invalid = ('BOGUS');
24 my @symbols = (keys %valid, @invalid);
25
26 my $method = 'za'; # Name of the target method for testing
27 my $currency = 'ZAR'; # expected quote curreny
28 my $today = today(); # together with $window, validate date/isodate
29 my $window = 7; # quote must be within last $window days
30
31
32 my %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 );
42 my $q = Finance::Quote->new();
43
44 plan tests => 1 + (scalar keys %check) * (scalar keys %valid) + scalar @invalid;
45
46 my %quotes = $q->fetch($method, @symbols);
47 ok(%quotes);
48
49 ### [<now>] quotes: %quotes
50
51 foreach 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
57 foreach my $symbol (@invalid) {
58 ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
59 }
60