Debian Codegraph @

t/finnhub.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_FINNHUB_API_KEY"} ) {
11 plan skip_all =>
12 'Set $ENV{TEST_FINNHUB_API_KEY} to run this test; get a free key at https://finnhub.io';
13 }
14
15 my $q = Finance::Quote->new('Finnhub', 'finnhub' => {'API_KEY' => $ENV{"TEST_FINNHUB_API_KEY"}} );
16 my $year = ( localtime() )[5] + 1900;
17 my $lastyear = $year - 1;
18
19 # AAPL, MSFT: US equities (currency/name from company profile)
20 # SPY: ETF (no company profile; currency defaults to USD)
21 my @symbols = qw/AAPL MSFT SPY/;
22
23 plan tests => 1 + 6*(1+$#symbols) + 1;
24
25 my %quotes = $q->fetch( 'finnhub', @symbols, "BOGUS" );
26 ok(%quotes);
27
28 foreach my $symbol (@symbols) {
29 ok( $quotes{ $symbol, "success" }, "$symbol success" );
30 ok( $quotes{ $symbol, "symbol" } eq $symbol, "$symbol defined" );
31 ok( $quotes{ $symbol, "last" } > 0, "$symbol returned last" );
32 ok( $quotes{ $symbol, "currency" } eq 'USD', "$symbol currency USD" );
33 ok( $quotes{ $symbol, "method" } eq 'finnhub', "$symbol method" );
34 ok( substr( $quotes{ $symbol, "isodate" }, 0, 4 ) == $year
35 || substr( $quotes{ $symbol, "isodate" }, 0, 4 ) == $lastyear,
36 "$symbol isodate in current or last year" );
37 }
38
39 ok( !$quotes{ "BOGUS", "success" }, 'BOGUS failed as expected' );