Debian Codegraph @

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