-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickers_test.go
More file actions
123 lines (102 loc) · 2.67 KB
/
Copy pathtickers_test.go
File metadata and controls
123 lines (102 loc) · 2.67 KB
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
package marketstack
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetTickers(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/tickers" {
t.Errorf("expected path '/tickers', got '%s'", r.URL.Path)
}
query := r.URL.Query()
if query.Get("search") != "apple" {
t.Errorf("expected search 'apple', got '%s'", query.Get("search"))
}
response := TickersResponse{
Pagination: Pagination{
Limit: 10,
Offset: 0,
Count: 1,
Total: 1,
},
Data: []Ticker{
{
Name: "Apple Inc",
Symbol: "AAPL",
HasIntraday: true,
HasEOD: true,
Country: "US",
StockExchange: &StockExchange{
Name: "NASDAQ",
Acronym: "NASDAQ",
MIC: "XNAS",
},
},
},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client := NewClient("test-key", nil)
client.SetBaseURL(server.URL)
result, err := client.GetTickers(context.Background(), &TickersOptions{
Search: "apple",
Limit: 10,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Data) != 1 {
t.Fatalf("expected 1 ticker, got %d", len(result.Data))
}
if result.Data[0].Symbol != "AAPL" {
t.Errorf("expected symbol 'AAPL', got '%s'", result.Data[0].Symbol)
}
if result.Data[0].Name != "Apple Inc" {
t.Errorf("expected name 'Apple Inc', got '%s'", result.Data[0].Name)
}
}
func TestGetTicker(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/tickers/TSLA" {
t.Errorf("expected path '/tickers/TSLA', got '%s'", r.URL.Path)
}
response := TickerResponse{
Name: "Tesla Inc",
Symbol: "TSLA",
HasIntraday: true,
HasEOD: true,
Country: "US",
StockExchange: &StockExchange{
Name: "NASDAQ",
Acronym: "NASDAQ",
MIC: "XNAS",
},
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client := NewClient("test-key", nil)
client.SetBaseURL(server.URL)
result, err := client.GetTicker(context.Background(), "TSLA")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Symbol != "TSLA" {
t.Errorf("expected symbol 'TSLA', got '%s'", result.Symbol)
}
if result.Name != "Tesla Inc" {
t.Errorf("expected name 'Tesla Inc', got '%s'", result.Name)
}
if result.StockExchange == nil {
t.Fatal("expected stock exchange to be set")
}
if result.StockExchange.MIC != "XNAS" {
t.Errorf("expected MIC 'XNAS', got '%s'", result.StockExchange.MIC)
}
}