-
Notifications
You must be signed in to change notification settings - Fork 3
/
stocks_bulkquotes_test.go
114 lines (98 loc) · 3.37 KB
/
stocks_bulkquotes_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
package client
import (
"context"
"encoding/json"
"fmt"
"log"
"testing"
"time"
)
func ExampleBulkStockQuotesRequest_get() {
// This example demonstrates how to create a BulkStockQuotesRequest, set its parameters,
// and perform an actual request to fetch stock quotes for multiple symbols.
// Initialize a new BulkStockQuotesRequest and fetch stock quotes for "AAPL", "META", and "MSFT".
ctx := context.TODO()
symbols := []string{"AAPL", "META", "MSFT"}
bsqr, err := BulkStockQuotes().Symbols(symbols).Get(ctx)
if err != nil {
log.Fatalf("Failed to get bulk stock quotes: %v", err)
}
// Check if the response contains the symbols.
for _, quote := range bsqr {
fmt.Printf("Symbol: %s\n", quote.Symbol)
}
// Output: Symbol: AAPL
// Symbol: META
// Symbol: MSFT
}
func ExampleBulkStockQuotesRequest_packed() {
// This example demonstrates how to create a BulkStockQuotesRequest, set its parameters,
// and perform an actual request to fetch stock quotes for multiple symbols: "AAPL", "META", and "MSFT".
// Initialize a new BulkStockQuotesRequest and fetch stock quotes for the specified symbols.
ctx := context.TODO()
symbols := []string{"AAPL", "META", "MSFT"}
bsqr, err := BulkStockQuotes().Symbols(symbols).Packed(ctx)
if err != nil {
log.Fatalf("Failed to get bulk stock quotes: %v", err)
}
// Iterate and print all the symbols in the response.
for _, symbol := range bsqr.Symbol {
fmt.Printf("Symbol: %s\n", symbol)
}
// Output: Symbol: AAPL
// Symbol: META
// Symbol: MSFT
}
func ExampleBulkStockQuotesRequest_raw() {
// This example demonstrates how to create a BulkStockQuotesRequest, set its parameters,
// and perform an actual request to fetch stock quotes for multiple symbols: "AAPL", "META", and "MSFT".
// The response is converted to a raw string and we print out the string at the end of the test.
// Initialize a new BulkStockQuotesRequest and fetch stock quotes for "AAPL", "META", and "MSFT".
ctx := context.TODO()
symbols := []string{"AAPL", "META", "MSFT"}
bsqr, err := BulkStockQuotes().Symbols(symbols).Raw(ctx)
if err != nil {
log.Fatalf("Failed to get bulk stock quotes: %v", err)
}
// Convert the response to a string.
jsonStr := bsqr.String()
// Define a struct to match the JSON structure
type Response struct {
Symbol []string `json:"symbol"`
}
// Unmarshal the JSON into the struct
var resp Response
err = json.Unmarshal([]byte(jsonStr), &resp)
if err != nil {
log.Fatalf("Failed to parse JSON: %v", err)
}
// Print the symbols
for _, symbol := range resp.Symbol {
fmt.Printf("Symbol: %s\n", symbol)
}
// Output: Symbol: AAPL
// Symbol: META
// Symbol: MSFT
}
func TestBulkStockQuotesRequest_packed(t *testing.T) {
//client, _ := GetClient()
//client.Debug(true)
ctx := context.TODO()
symbols := []string{"AAPL", "META", "MSFT"}
bsqr, err := BulkStockQuotes().Symbols(symbols).Get(ctx)
if err != nil {
fmt.Print(err)
return
}
currentDate := time.Now()
for _, quote := range bsqr {
// Check if the symbol is one of the expected symbols
if quote.Symbol != "AAPL" && quote.Symbol != "META" && quote.Symbol != "MSFT" {
t.Errorf("Unexpected symbol %s found in response", quote.Symbol)
}
// Check if the UpdatedAt date is within the last 7 days
if currentDate.Sub(quote.Updated).Hours() > 168 { // 168 hours in 7 days
t.Errorf("Quote for symbol %s was not updated within the last 7 days", quote.Symbol)
}
}
}