Debian Codegraph @

t/yahoojson.t

raw
1 #!/usr/bin/perl -w
2
3 # A test script to check for working of the YahooJSON module.
4
5 use strict;
6 use Test::More;
7 use Finance::Quote;
8
9 if (not $ENV{ONLINE_TEST}) {
10 plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
11 }
12
13 plan tests => 65;
14
15 my $q = Finance::Quote->new();
16
17 #List of stocks to fetch. Feel free to change this during testing
18 my @stocks =
19 ( "SUZLON.BO", "RECLTD.NS", "AMZN", "SOLB.BR", "^DJI", "BEL20.BR", "INGDIRECTFNE.BC", "AENA.MC", "CFR.JO" );
20
21 my %quotes = $q->fetch( "yahoo_json", @stocks );
22 ok( %quotes, "Data returned" );
23
24 foreach my $stock (@stocks) {
25
26 my $name = $quotes{ $stock, "name" };
27 ok( $quotes{ $stock, "success" }, "Retrieved $stock" );
28 if ( !$quotes{ $stock, "success" } ) {
29 my $errmsg = $quotes{ $stock, "errormsg" };
30 warn "Error Message:\n$errmsg\n";
31 }
32 else {
33 ok( $name, "Name is defined : $name" );
34
35 my $fetch_method = $quotes{ $stock, "method" };
36 ok( $fetch_method eq 'yahoo_json', "fetch_method is yahoo_json" );
37
38 my $last = $quotes{ $stock, "last" };
39 ok( $last > 0, "Last $last > 0" );
40
41 my $volume = $quotes{ $stock, "volume" };
42 ok( $volume > 0, "Volume $volume > 0" ) if !( grep { $_ eq $stock } ("BEL20.BR","INGDIRECTFNE.BC") );
43
44 my $type = $quotes{ $stock, "type" };
45 ok( $type, "Symbol type $type" );
46
47 for my $field (qw( market_cap volume_10_days trailing_pe book_value shares_outstanding shares_short shares_float )) {
48 my $value = $quotes{ $stock, $field } // 0;
49 ok( $value > 0, "$field $value > 0 for $stock" ) if $stock eq "AMZN";
50 }
51
52 #TODO: Add a test to raise a warning if the quote is excessively old
53 my $isodate = $quotes{ $stock, "isodate" };
54
55 # print "ISOdate: $isodate ";
56 my $date = $quotes{ $stock, "date" };
57
58 # currency for .BO stocks
59 ok( $quotes { $stock, "currency" } eq 'INR', 'Bombay stocks have currency INR' ) if $stock =~ /\.BO$/ ;
60 ok( $quotes { $stock, "currency" } eq 'EUR', 'Barcelona stocks have currency EUR' ) if $stock =~ /\.BC$/ ;
61 ok( $quotes { $stock, "currency" } eq 'EUR', 'Madrid stocks have currency EUR' ) if $stock =~ /\.MC$/ ;
62
63 # currency for .JO (Johannesburg Stock Exchange) stocks
64 ok( $quotes { $stock, "currency" } eq 'ZAR', 'Johannesburg stocks have currency ZAR' ) if $stock =~ /\.JO$/ ;
65
66 # print "Date: $date ";
67 }
68 }
69
70 # Check that a bogus stock returns no-success.
71 %quotes = $q->fetch( "yahoo_json", "BOGUS" );
72 ok( !$quotes{ "BOGUS", "success" }, "BOGUS failed" );