This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
client_integration_test.go
275 lines (242 loc) · 7.89 KB
/
client_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2019-2024 The iexcloud developers. All rights reserved.
// Project site: https://github.com/goinvest/iexcloud
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE file for the project.
//go:build integration
// +build integration
package iex_test
import (
"context"
"io"
"log"
"math"
"os"
"sort"
"testing"
"time"
"github.com/BurntSushi/toml"
iex "github.com/goinvest/iexcloud/v2"
)
var client *iex.Client
// Config contains the configuration information needed to program and test the
// adapaters.
type Config struct {
Token string
BaseURL string
}
// ReadConfig will read the TOML config file.
func readConfig(configFile string) (Config, error) {
var cfg Config
// Read config file
f, err := os.Open(configFile)
if err != nil {
return cfg, err
}
defer f.Close()
buf, err := io.ReadAll(f)
if err != nil {
return cfg, err
}
err = toml.Unmarshal(buf, &cfg)
return cfg, err
}
func TestMain(m *testing.M) {
cfg, err := readConfig("config_test.toml")
if err != nil {
log.Fatalf("Error reading config file: %s", err)
}
client = iex.NewClient(
cfg.Token,
iex.WithBaseURL(cfg.BaseURL),
iex.WithRateLimiter(200*time.Millisecond, 1),
)
m.Run()
}
func TestIntegrationAnnualBalanceSheets(t *testing.T) {
bs, err := client.AnnualBalanceSheets(context.Background(), "aapl", 4)
if err != nil {
log.Fatalf("Error getting annual balance sheets: %s", err)
}
assertString(t, "symbol", bs.Symbol, "AAPL")
assertInt(t, "number of years", len(bs.Statements), 4)
y1 := bs.Statements[0]
assertString(t, "filing type", y1.FilingType, "10-K")
assertInt(t, "fiscal quarter", y1.FiscalQuarter, 0)
isPositiveInt(t, "fiscal year", y1.FiscalYear)
assertString(t, "currency", y1.Currency, "USD")
}
func TestIntegrationQuarterlyBalanceSheets(t *testing.T) {
bs, err := client.QuarterlyBalanceSheets(context.Background(), "aapl", 4)
if err != nil {
log.Fatalf("Error getting balance sheets: %s", err)
}
assertString(t, "symbol", bs.Symbol, "AAPL")
assertInt(t, "number of quarters", len(bs.Statements), 4)
q1 := bs.Statements[0]
isPositiveInt(t, "fiscal quarter", q1.FiscalQuarter)
isPositiveInt(t, "fiscal year", q1.FiscalYear)
assertString(t, "currency", q1.Currency, "USD")
}
func TestIntegrationAnnualCashFlows(t *testing.T) {
cf, err := client.AnnualCashFlows(context.Background(), "aapl", 4)
if err != nil {
log.Fatalf("Error getting annual cash flows: %s", err)
}
assertString(t, "symbol", cf.Symbol, "AAPL")
assertInt(t, "number of years", len(cf.Statements), 4)
y1 := cf.Statements[0]
assertString(t, "filing type", y1.FilingType, "10-K")
assertInt(t, "fiscal quarter", y1.FiscalQuarter, 0)
isPositiveInt(t, "fiscal year", y1.FiscalYear)
assertString(t, "currency", y1.Currency, "USD")
}
func TestIntegrationQuarterlyCashFlows(t *testing.T) {
cf, err := client.QuarterlyCashFlows(context.Background(), "aapl", 4)
if err != nil {
log.Fatalf("Error getting balance sheets: %s", err)
}
assertString(t, "symbol", cf.Symbol, "AAPL")
assertInt(t, "number of quarters", len(cf.Statements), 4)
q1 := cf.Statements[0]
isPositiveInt(t, "fiscal quarter", q1.FiscalQuarter)
isPositiveInt(t, "fiscal year", q1.FiscalYear)
assertString(t, "currency", q1.Currency, "USD")
}
func TestIntegrationAnnualIncomeStatements(t *testing.T) {
is, err := client.AnnualIncomeStatements(context.Background(), "f", 4)
if err != nil {
log.Fatalf("Error getting annual income statements: %s", err)
}
assertString(t, "symbol", is.Symbol, "F")
if len(is.Statements) != 4 {
t.Errorf("\ngot = %d %s\nwant = %d", len(is.Statements), "number of years", 4)
t.FailNow()
}
assertInt(t, "number of years", len(is.Statements), 4)
y2 := is.Statements[1]
assertString(t, "filing type", y2.FilingType, "10-K")
isPositiveInt(t, "fiscal year", y2.FiscalYear)
assertString(t, "currency", y2.Currency, "USD")
}
func TestIntegrationQuarterlyIncomeStatements(t *testing.T) {
is, err := client.QuarterlyIncomeStatements(context.Background(), "f", 4)
if err != nil {
log.Fatalf("Error getting quarterly income statements: %s", err)
}
assertString(t, "symbol", is.Symbol, "F")
if len(is.Statements) != 4 {
t.Errorf("\ngot = %d %s\nwant = %d", len(is.Statements), "number of quarters", 4)
t.FailNow()
}
q3 := is.Statements[2]
isPositiveInt(t, "fiscal quarter", q3.FiscalQuarter)
isPositiveInt(t, "fiscal year", q3.FiscalYear)
assertString(t, "currency", q3.Currency, "USD")
}
func TestIntegrationBook(t *testing.T) {
got, err := client.Book(context.Background(), "aapl")
if err != nil {
log.Fatalf("Error getting book: %s", err)
}
assertString(t, "symbol", got.Quote.Symbol, "AAPL")
assertString(t, "company name", got.Quote.CompanyName, "Apple Inc")
assertScrambledString(t, "primary exchange", got.Quote.PrimaryExchange, "NASDAQ")
assertStringFromOptions(t, "calculation price", got.Quote.CalculationPrice,
[]string{"tops", "sip", "previousclose", "close", "iexlasttrade"})
isNotNegativeFloat64(t, "open", got.Quote.Open)
assertScrambledString(t, "open source", got.Quote.OpenSource, "official")
isPositiveFloat64(t, "latest price", got.Quote.LatestPrice)
}
func TestIntegrationHistoricalPrices(t *testing.T) {
timeframe := iex.OneMonthHistorical
histPrices, err := client.HistoricalPrices(context.Background(), "aapl", timeframe, nil)
if err != nil {
log.Fatalf("Error getting historical prices: %s", err)
}
got := histPrices[0]
isPositiveFloat64(t, "close", got.Close)
isPositiveFloat64(t, "high", got.High)
isPositiveFloat64(t, "low", got.Low)
isPositiveFloat64(t, "open", got.Open)
assertString(t, "symbol", got.Symbol, "AAPL")
isPositiveFloat64(t, "volume", got.Volume)
assertScrambledString(t, "id", got.ID, "HISTORICAL_PRICES")
assertScrambledString(t, "key", got.Key, "AAPL")
assertString(t, "subkey", got.Subkey, "")
}
func assertInt(t *testing.T, label string, got, want int) {
if got != want {
t.Errorf("\ngot = %d %s\nwant = %d", got, label, want)
}
}
func assertFloat64(t *testing.T, label string, got, want, tolerance float64) {
if diff := math.Abs(want - got); diff >= tolerance {
t.Errorf("\ngot = %f %s\ntwant = %f", got, label, want)
}
}
func assertBool(t *testing.T, label string, got, want bool) {
if got != want {
t.Errorf("\ngot = %t %s\nwant = %t", got, label, want)
}
}
func assertString(t *testing.T, label string, got, want string) {
if got != want {
t.Errorf("\ngot = %s %s\nwant = %s", got, label, want)
}
}
func assertStringFromOptions(t *testing.T, label string, got string, options []string) {
isAnOption := false
for _, option := range options {
if got == option {
isAnOption = true
break
}
}
if isAnOption == false {
t.Errorf("\ngot = %s %s\nwant one of %s", got, label, options)
}
}
// IEX scrambles their responses when using the testing sandbox. Therefore, the
// best we can do is assert that all the letters are there even if scrambled.
func assertScrambledString(t *testing.T, label string, got, want string) {
gotSorted := sortString(got)
wantSorted := sortString(want)
if gotSorted != wantSorted {
t.Errorf("\n got = %s %s\nwant = %s", got, label, want)
}
}
func isPositiveInt(t *testing.T, label string, got int) {
if got <= 0 {
t.Errorf("\n got = %d %s\nwant int > 0", got, label)
}
}
func isPositiveFloat64(t *testing.T, label string, got float64) {
if got <= 0.0 {
t.Errorf("\n got = %f %s\nwant float64 > 0.0", got, label)
}
}
func isNotNegativeFloat64(t *testing.T, label string, got float64) {
if got < 0.0 {
t.Errorf("\n got = %f %s\nwant float64 >= 0.0", got, label)
}
}
func isString(t *testing.T, label string, got string) {
if got == "" {
t.Errorf("\n got = %s %s\nwant non-empty string", got, label)
}
}
type sortRunes []rune
func (s sortRunes) Less(i, j int) bool {
return s[i] < s[j]
}
func (s sortRunes) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRunes) Len() int {
return len(s)
}
func sortString(s string) string {
r := []rune(s)
sort.Sort(sortRunes(r))
return string(r)
}