Debian Codegraph @

t/alphavantage.t

raw
1 #!/usr/bin/perl -w
2 use strict;
3 use Test::More;
4 use Finance::Quote;
5
6 if ( not $ENV{"ONLINE_TEST"} ) {
7 plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
8 }
9
10 if ( not $ENV{"TEST_ALPHAVANTAGE_API_KEY"} ) {
11 plan skip_all =>
12 'Set $ENV{TEST_ALPHAVANTAGE_API_KEY} to run this test; get one at https://www.alphavantage.co';
13 }
14
15 my $q = Finance::Quote->new('AlphaVantage', 'alphavantage' => {'API_KEY' => $ENV{"TEST_ALPHAVANTAGE_API_KEY"}} );
16 my $year = ( localtime() )[5] + 1900;
17 my $lastyear = $year - 1;
18
19 my @symbols = qw/
20 BP.L
21 CSCO
22 DIVO11.SA
23 ERCB.DE
24 IBM
25 MRT-UN.TRT
26 SAP.DE
27 SOLB.BR
28 TD.TO
29 /;
30
31 plan tests => 1 + 11*(1+$#symbols) + 10;
32
33 my %quotes = $q->alphavantage( @symbols, "BOGUS" );
34 ok(%quotes);
35
36 foreach my $symbol (@symbols) {
37 ok( $quotes{ $symbol, "success" }, "$symbol success" );
38 ok( $quotes{ $symbol, "symbol" } eq $symbol , "$symbol defined" );
39 ok( $quotes{ $symbol, "open" } > 0, "$symbol returned open" );
40 ok( $quotes{ $symbol, "close" } > 0, "$symbol returned close" );
41 ok( $quotes{ $symbol, "last" } > 0, "$symbol returned last" );
42 ok( $quotes{ $symbol, "high" } > 0, "$symbol returned high" );
43 ok( $quotes{ $symbol, "low" } > 0, "$symbol returned low" );
44 ok( $quotes{ $symbol, "volume" } >= 0, "$symbol returned volume" );
45 ok( $quotes{ $symbol, "p_change" } =~ /^[\-\.\d]+$/, "$symbol returned p_change" );
46 ok( substr( $quotes{ $symbol, "isodate" }, 0, 4 ) == $year
47 || substr( $quotes{ $symbol, "isodate" }, 0, 4 ) == $lastyear );
48 ok( substr( $quotes{ $symbol, "date" }, 6, 4 ) == $year
49 || substr( $quotes{ $symbol, "date" }, 6, 4 ) == $lastyear );
50 }
51
52 is( $quotes{ "BP.L", "currency" }, 'GBP' );
53 is( $quotes{ "CSCO", "currency" }, 'USD' );
54 is( $quotes{ "DIVO11.SA", "currency" }, 'BRL' );
55 is( $quotes{ "ERCB.DE", "currency" }, 'EUR' );
56 is( $quotes{ "IBM", "currency" }, 'USD' );
57 is( $quotes{ "MRT-UN.TRT", "currency" }, 'CAD' );
58 is( $quotes{ "SAP.DE", "currency" }, 'EUR' );
59 is( $quotes{ "SOLB.BR", "currency" }, 'EUR' );
60 is( $quotes{ "TD.TO", "currency" }, 'CAD' );
61
62 ok( !$quotes{ "BOGUS", "success" } );