-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstocks_quotes_test.go
102 lines (84 loc) · 2.81 KB
/
stocks_quotes_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
package client
import (
"context"
"fmt"
"log"
"regexp"
"testing"
"time"
)
func ExampleStockQuoteRequest_get() {
// This example demonstrates how to create a StockQuoteRequest, set its parameters,
// and perform an actual request to fetch stock quotes for the "AAPL" symbol.
// Initialize a new StockQuoteRequest and fetch a stock quote.
ctx := context.TODO()
sqr, err := StockQuote().Symbol("AAPL").Get(ctx)
if err != nil {
log.Fatalf("Failed to get stock quotes: %v", err)
}
// Check if the response contains the "AAPL" symbol.
for _, quote := range sqr {
fmt.Printf("Symbol: %s\n", quote.Symbol)
}
// Output: Symbol: AAPL
}
func ExampleStockQuoteRequest_packed() {
// This example demonstrates how to create a StockQuoteRequest, set its parameters,
// and perform an actual request to fetch stock quotes for the "AAPL" symbol.
// Initialize a new StockQuoteRequest and fetch a stock quote.
ctx := context.TODO()
sqr, err := StockQuote().Symbol("AAPL").Packed(ctx)
if err != nil {
log.Fatalf("Failed to get stock quotes: %v", err)
}
// Iterate and print all the symbols in the slice.
for _, symbol := range sqr.Symbol {
fmt.Printf("Symbol: %s\n", symbol)
}
// Output: Symbol: AAPL
}
func ExampleStockQuoteRequest_raw() {
// This example demonstrates how to create a StockQuoteRequest, set its parameters,
// and perform an actual request to fetch stock quotes for the "AAPL" symbol.
// The response is converted to a raw string and we print out the string at the end of the test.
// Initialize a new StockQuoteRequest and fetch a stock quote.
ctx := context.TODO()
sqr, err := StockQuote().Symbol("AAPL").Raw(ctx)
if err != nil {
log.Fatalf("Failed to get stock quotes: %v", err)
}
// Convert the response to a string.
sqrStr := sqr.String()
// Use regex to find the symbol in the response string.
re := regexp.MustCompile(`"symbol":\["(.*?)"\]`)
matches := re.FindStringSubmatch(sqrStr)
if len(matches) < 2 {
log.Fatalf("Failed to extract symbol from response")
}
// Print the extracted symbol.
fmt.Printf("Symbol: %s\n", matches[1])
// Output: Symbol: AAPL
}
func TestStockQuoteRequest(t *testing.T) {
ctx := context.TODO()
sqr, err := StockQuote().Symbol("AAPL").FiftyTwoWeek(true).Get(ctx)
if err != nil {
fmt.Print(err)
return
}
for _, quote := range sqr {
if quote.Symbol != "AAPL" {
t.Errorf("Expected symbol 'AAPL', got %s", quote.Symbol)
}
if quote.High52 != nil && *quote.High52 <= 0 {
t.Errorf("Expected High52 to be a positive number, got %f", *quote.High52)
}
if quote.Low52 != nil && *quote.Low52 <= 0 {
t.Errorf("Expected Low52 to be a positive number, got %f", *quote.Low52)
}
sevenDaysAgo := time.Now().AddDate(0, 0, -7)
if quote.Updated.Before(sevenDaysAgo) {
t.Errorf("Expected Updated to be within the last 7 days, got %s", quote.Updated)
}
}
}