Debian Codegraph @

t/six.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 = ('NESN' => "Nestl\x{e9} AG",
21 'CSSMI' => 'BlackRock Asset Management Schweiz AG',
22 'AAPL' => 'Apple Inc'
23 );
24 my @invalid = ('BOGUS');
25 my @symbols = (keys %valid, @invalid);
26
27 my $method = 'six'; # Name of the target method for testing
28 my $currency = 'EUR'; # expected quote curreny
29 my $today = today(); # together with $window, validate date/isodate
30 my $window = 7; # quote must be within last $window days
31
32 my %check = (# Tests are called with (value_to_test, symbol, quote_hash_reference)
33 'success' => sub {$_[0] == 1},
34 'volume' => sub {not defined $_[0] or looks_like_number($_[0])},
35 'open' => sub {not defined $_[0] or looks_like_number($_[0])},
36 'low' => sub {not defined $_[0] or looks_like_number($_[0])},
37 'high' => sub {not defined $_[0] or looks_like_number($_[0])},
38 'close' => sub {not defined $_[0] or looks_like_number($_[0])},
39 'ask' => sub {not defined $_[0] or looks_like_number($_[0])},
40 'currency' => sub {$_[0] =~ /^[A-Z]{3}$/},
41 'isin' => sub {$_[0] =~ /^[A-Z0-9]{12}$/},
42 'isodate' => sub {Date::Range->new($today - $window, $today)->includes(Date::Simple::ISO->new($_[0]))},
43 'date' => sub {my $a = Date::Manip::Date->new(); $a->parse_format('%m/%d/%Y', $_[0]);
44 my $b = Date::Manip::Date->new(); $b->parse_format('%Y-%m-%d', $_[2]->{$_[1], 'isodate'});
45 return $a->cmp($b) == 0;}
46 );
47 my $q = Finance::Quote->new();
48
49 plan tests => 1 + %check*%valid + @invalid;
50
51 my %quotes = $q->fetch($method, @symbols);
52 ok(%quotes);
53
54 ### [<now>] quotes: %quotes
55
56 foreach my $symbol (keys %valid) {
57 while (my ($key, $lambda) = each %check) {
58 ok($lambda->($quotes{$symbol, $key}, $symbol, \%quotes), "$key -> " . (defined $quotes{$symbol, $key} ? $quotes{$symbol, $key} : '<undefined>'));
59 }
60 }
61
62 foreach my $symbol (@invalid) {
63 ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
64 }
65