Debian Codegraph @

t/bourso.t

raw
1 #!/usr/bin/perl -w
2
3 use constant DEBUG => $ENV{DEBUG};
4 use if DEBUG, Smart::Comments;
5
6 use strict;
7 use Test::More;
8 use Finance::Quote;
9 use Scalar::Util qw(looks_like_number);
10 use Date::Simple qw(today);
11 use Date::Range;
12 use Date::Manip;
13
14 if ( not $ENV{ONLINE_TEST} ) {
15 plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
16 }
17
18 # Bourso tests need to cover all the possible cases:
19 #
20 # Name What Test Case
21 #
22 # action Stock 1rPAF, MSFT, FF11-SOLB, 1rPSOLB, 1rPCNP
23 # obligation Bond 1rPFR0010371401
24 # opcvm Fund MP-802941
25 # warrant Warrant 1rAHX70B - expired & removed from tests
26 # indice Index 1rPCAC
27 # tracker Tracker 1rTBX4
28
29 my %valid = ('MP-802941' => {currency => 'EUR', days => 32, name => 'Covéa Actions Asie C'}, # Fund, EUR
30 '1rPAF' => {currency => 'EUR', days => 7, name => 'AIR FRANCE-KLM'}, # Stock, EUR, Euronext Paris
31 'MSFT' => {currency => 'USD', days => 7, name => 'MICROSOFT'}, # Stock, USD, NASDAQ
32 'FF11-SOLB' => {currency => 'EUR', days => 7, name => 'SOLVAY'}, # Stock, EUR, Euronext Bruxelles
33 '2rPDE000CX0QLH6' => {currency => 'EUR', days => 7, name => 'GOLD/CITI WT OPEN'}, # Warrant
34 '1rPFR0010371401' => {currency => '' , days => 100, name => 'FRENCH REPUBLIC 4% 25/10/38 EUR'}, # Bond, EUR, Euronext Paris,
35 '1rPCAC' => {currency => 'Pts', days => 7, name => 'CAC 40'}, # Index, Pts, Paris,
36 '1rTBX4' => {currency => 'EUR', days => 7, name => 'Lyxor CAC 40 Daily Double Short UCITS ETF - Acc'}, # Tracker, EUR
37 'FR0010037341' => {currency => 'EUR', days => 7, name => 'CM-AM Europe Growth RC'}, # Added to check for spaces in quote
38 );
39 my %invalid = ('BOGUS' => undef);
40 my @symbols = (keys %valid, keys %invalid);
41 my $today = today();
42 my %check = (# Tests are called with (value_to_test, symbol, quote_hash_reference)
43 'name' => sub {$valid{$_[1]}{name} eq $_[0]}, # @_ = (value, symbol)
44 'currency' => sub {$valid{$_[1]}{currency} eq $_[0]}, #
45 'method' => sub {$_[0] eq 'bourso'}, #
46 'success' => sub {$_[0]}, #
47 'volume' => sub {defined $_[0] ? looks_like_number($_[0]) : 1}, # volume is optional
48 'close' => sub {defined $_[0] ? looks_like_number($_[0]) : 1}, # close is optional
49 'last' => sub {looks_like_number($_[0])}, # last is REQUIRED
50 'high' => sub {defined $_[0] ? looks_like_number($_[0]) : 1}, # high is optional
51 'low' => sub {defined $_[0] ? looks_like_number($_[0]) : 1}, # low is optional
52 'net' => sub {defined $_[0] ? looks_like_number($_[0]) : 1}, # net is optional
53 'exchange' => sub {defined $_[0] ? $_[0] =~ /^[A-Z]+$/ : 1}, # exchange is optional
54 'isodate' => sub {Date::Range->new($today - $valid{$_[1]}{days}, $today)->includes(Date::Simple::ISO->new($_[0]))},
55 'date' => sub {my $a = Date::Manip::Date->new(); $a->parse_format('%m/%d/%Y', $_[0]);
56 my $b = Date::Manip::Date->new(); $b->parse_format('%Y-%m-%d', $_[2]->{$_[1], 'isodate'});
57 return $a->cmp($b) == 0;},
58 );
59 my $q = Finance::Quote->new();
60
61
62 plan tests => 1 + %check*%valid + %invalid;
63
64 my %quotes = $q->fetch('bourso', @symbols);
65 ok(%quotes);
66
67 ### [<now>] quotes: %quotes
68
69 foreach my $symbol (keys %valid) {
70 while (my ($key, $lambda) = each %check) {
71 ### check key: $key
72 ### check res: $quotes{$symbol, $key}
73 ok($lambda->($quotes{$symbol, $key}, $symbol, \%quotes),
74 defined $quotes{$symbol, $key}
75 ? "$key -> $quotes{$symbol, $key}"
76 : "$key -> <undefined>");
77 }
78 }
79
80 foreach my $symbol (keys %invalid) {
81 ok((not $quotes{'BOGUS', 'success'}), 'failed as expected');
82 }
83