Debian Codegraph @

t/asx.t

raw
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4 use Scalar::Util qw(looks_like_number);
5 use Finance::Quote;
6 use Test::More;
7 use Time::Piece;
8 use feature 'say';
9
10 if (not $ENV{ONLINE_TEST}) {
11 plan skip_all => 'Set $ENV{ONLINE_TEST} to run this test';
12 }
13
14 plan tests => 6;
15
16 # Listed securities come and go, so entries on these lists will eventually fail.
17 my @ordinaries = qw/ARG BHP CBA RIO/;
18 my @numerics = qw/360 14D A2M XF1/;
19 my @corp_bonds = qw/AYUHD/;
20 my @govt_bonds = qw/GSBK51 GSIC50/;
21 my @etps = qw/BEAR ETPMPT GOLD IAA/;
22 my @warrants = qw/BKIJOB/;
23 my @indices = qw/XAO XJO/;
24 my @invalids = qw/BOGUS TooLong Non-AN/;
25
26 my ($q, %quotes);
27
28 subtest 'Startup: Test object creation' => sub {
29 $q = new_ok( 'Finance::Quote' );
30 };
31
32 subtest 'Test the "normal" securities that use the Primary ASX data source' => sub {
33 ok( %quotes = $q->asx(@ordinaries, @numerics), 'fetch quotes' );
34
35 for my $symbol (@ordinaries, @numerics) {
36
37 ok( $quotes{$symbol, 'success'} == 1, "Success for $symbol" );
38
39 subtest "Data tests for $symbol" => sub {
40 plan 'skip_all' => "Bypass data tests for $symbol - no data returned" unless $quotes{$symbol, 'success'} == 1;
41 plan tests => 10;
42
43 ok( $quotes{$symbol, 'errormsg'} eq '', 'no error message for valid symbol' );
44 ok( $quotes{$symbol, 'currency'} eq 'AUD', 'got expected currency' );
45 ok( $quotes{$symbol, 'method' } eq 'asx', 'got expected method' );
46
47 ok( length $quotes{$symbol,'name'}, 'got a name' );
48 ok( $quotes{$symbol,'symbol'} eq $symbol, 'matching symbol' );
49
50
51
52 # For the securities in this group, expect *all* prices and volume to have non-zero values.
53 # (The occasional trading halt or corporate action may cause some of these to fail - c'est la vie.)
54 for my $field (qw/ask bid last price/) {
55 ok( $quotes{$symbol, $field} > 0, "$field > 0" );
56 }
57
58 for my $field (qw/p_change/) {
59 ok( looks_like_number($quotes{$symbol, $field}),
60 "$field looks like a number" );
61 }
62
63 done_testing();
64 };
65 }
66 done_testing();
67 };
68
69 subtest 'Test the "other" security types, many of which use the Alternate ASX data source' => sub {
70 ok( %quotes = $q->asx(@corp_bonds, @govt_bonds, @etps, ), 'fetch quotes' );
71
72 for my $symbol (@corp_bonds, @govt_bonds, @etps) {
73
74 ok( $quotes{$symbol, 'success'} == 1, "success for $symbol" );
75
76 subtest "Data tests for $symbol" => sub {
77 plan 'skip_all' => "Bypass data tests for $symbol - no data returned" unless $quotes{$symbol, 'success'} == 1;
78 plan tests => 10;
79
80 ok( $quotes{$symbol, 'errormsg'} eq '', 'no error message for valid symbol' );
81 ok( $quotes{$symbol, 'currency'} eq 'AUD', 'got expected currency' );
82 ok( $quotes{$symbol, 'method' } eq 'asx', 'got expected method' );
83
84 ok( length $quotes{$symbol,'name'}, 'got a name' );
85 ok( $quotes{$symbol,'symbol'} eq $symbol, 'matching symbol' );
86
87
88 # For the securities in this group, only expect the prices to have non-zero values.
89 for my $field (qw/last price/) {
90 ok( $quotes{$symbol, $field} > 0, "$field > 0" );
91 }
92
93 for my $field (qw/ask bid/) {
94 SKIP: {
95 skip "Bypass check for '$field' if not received", 1 if $quotes{$symbol, $field} eq '';
96 ok( looks_like_number($quotes{$symbol, $field}),
97 "$field looks like a number" );
98
99 }
100 }
101
102 for my $field (qw/p_change/) {
103 ok( looks_like_number($quotes{$symbol, $field}),
104 "$field looks like a number" );
105 }
106
107 done_testing();
108 };
109 }
110 done_testing();
111 };
112
113 subtest 'Test the "warrants and options" security types, which use the Alternate ASX data source and are thinly traded' => sub {
114 ok( %quotes = $q->asx(@warrants), 'fetch quotes' );
115
116 for my $symbol (@warrants) {
117
118 ok( $quotes{$symbol, 'success'} == 1, "success for $symbol" );
119
120 subtest "Data tests for $symbol" => sub {
121 plan 'skip_all' => "Bypass data tests for $symbol - no data returned" unless $quotes{$symbol, 'success'} == 1;
122 plan tests => 9;
123
124 ok( $quotes{$symbol, 'errormsg'} eq '', 'no error message for valid symbol' );
125 ok( $quotes{$symbol, 'currency'} eq 'AUD', 'got expected currency' );
126 ok( $quotes{$symbol, 'method' } eq 'asx', 'got expected method' );
127
128 ok( length $quotes{$symbol,'name'}, 'got a name' );
129 ok( length $quotes{$symbol,'type'}, 'got a type' );
130 ok( $quotes{$symbol,'symbol'} eq $symbol, 'matching symbol' );
131
132
133 # For the securities in this group, only check for numeric values.
134 for my $field (qw/last price p_change/) {
135 ok( looks_like_number($quotes{$symbol, $field}),
136 "$field looks like a number" );
137 }
138
139 done_testing();
140 };
141 }
142 done_testing();
143 };
144
145 subtest 'Test the indexes, which use the Alternate ASX data source' => sub {
146 ok( %quotes = $q->asx(@indices), 'fetch quotes' );
147
148 for my $symbol (@indices) {
149
150 ok( $quotes{$symbol, 'success'} == 1, "success for $symbol" );
151
152 subtest "Data tests for $symbol" => sub {
153 plan 'skip_all' => "Bypass data tests for $symbol - no data returned" unless $quotes{$symbol, 'success'} == 1;
154 plan tests => 7;
155
156 ok( $quotes{$symbol, 'errormsg'} eq '', 'no error message for valid symbol' );
157 ok( $quotes{$symbol, 'method' } eq 'asx', 'got expected method' );
158
159 ok( length $quotes{$symbol,'name'}, 'got a name' );
160 ok( $quotes{$symbol,'symbol'} eq $symbol, 'matching symbol' );
161
162
163 # Indices should have non-zero prices and volumes.
164 for my $field (qw/last price/) {
165 ok( $quotes{$symbol, $field} > 0, "$field > 0" );
166 }
167
168 for my $field (qw/p_change/) {
169 ok( looks_like_number($quotes{$symbol, $field}),
170 "$field looks like number" );
171 }
172
173 done_testing();
174 };
175 }
176 done_testing();
177 };
178
179 subtest 'Test that invalid symbols fail properly' => sub {
180 ok( %quotes = $q->asx(@invalids), 'fetch quotes' );
181
182 for my $symbol (@invalids) {
183
184 subtest "Data tests for $symbol" => sub {
185 plan tests => 2;
186
187 ok( !$quotes{$symbol, 'success'}, , 'bad symbol returned no result' );
188 ok( $quotes{$symbol, 'errormsg'} ne '', 'got error message for bad symbol' );
189
190 done_testing();
191 };
192
193 }
194 done_testing();
195 };
196
197 done_testing();