Debian Codegraph @

t/comdirect.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 = ('VWAGY' => 'Volkswagen',
21 'Volkswagen' => 'Volkswagen',
22 'DE0007664039' => 'Volkswagen',
23 'FR0010510800' => 'Amundi EUR Overnight Return UCITS ETF'
24 );
25
26 my @invalid = ('BOGUS');
27 my @symbols = (keys %valid, @invalid);
28
29 my $method = 'comdirect'; # Name of the target method for testing
30 my $currency = 'EUR'; # expected quote curreny
31 my $today = today(); # together with $window, validate date/isodate
32 my $window = 7; # quote must be within last $window days
33
34 my %check = (# Tests are called with (value_to_test, symbol, quote_hash_reference)
35 'success' => sub {$_[0] == 1},
36 'method' => sub {$_[0] eq 'comdirect'},
37 'open' => sub {not defined $_[0] or looks_like_number($_[0])},
38 'low' => sub {not defined $_[0] or looks_like_number($_[0])},
39 'high' => sub {not defined $_[0] or looks_like_number($_[0])},
40 'last' => sub {not defined $_[0] or looks_like_number($_[0])},
41 'currency' => sub {$_[0] =~ /^[A-Z]{3}$/},
42 'name' => sub {$_[0] =~ $valid{$_[1]}},
43 'isin' => sub {$_[0] =~ /^[A-Z0-9]{12}$/},
44 'isodate' => sub {defined $_[0] and Date::Range->new($today - $window, $today)->includes(Date::Simple::ISO->new($_[0]))},
45 );
46 my $q = Finance::Quote->new();
47
48 plan tests => 1 + %check*%valid + @invalid;
49
50 my %quotes = $q->fetch($method, @symbols);
51 ok(%quotes);
52
53 ### [<now>] quotes: %quotes
54
55 foreach my $symbol (keys %valid) {
56 while (my ($key, $lambda) = each %check) {
57 ok($lambda->($quotes{$symbol, $key}, $symbol, \%quotes), "$key -> " . (defined $quotes{$symbol, $key} ? $quotes{$symbol, $key} : '<undefined>'));
58 }
59 }
60
61 foreach my $symbol (@invalid) {
62 ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
63 }
64