-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
313 lines (275 loc) · 9.36 KB
/
Copy pathclient_test.go
File metadata and controls
313 lines (275 loc) · 9.36 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package go_weather
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func newTestClient(t *testing.T, handler http.HandlerFunc) (*Client, func()) {
t.Helper()
server := httptest.NewServer(handler)
client, err := NewClient("test-key", WithHTTPClient(server.Client()), WithBaseURL(server.URL))
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
return client, server.Close
}
func TestNewClient_EmptyAPIKey(t *testing.T) {
if _, err := NewClient(""); err == nil {
t.Fatal("expected error for empty API key, got nil")
}
}
func TestCurrentService_Get(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/current.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
if got := r.URL.Query().Get("q"); got != "New York" {
t.Errorf("unexpected q param: %s", got)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"location": map[string]any{"name": "New York"},
"current": map[string]any{"temp_c": 21.0},
})
})
defer cleanup()
resp, err := client.Current.Get(context.Background(), "New York")
if err != nil {
t.Fatalf("Current.Get failed: %v", err)
}
if resp.Location.Name != "New York" {
t.Errorf("expected location New York, got %s", resp.Location.Name)
}
if resp.Current.TempC != 21.0 {
t.Errorf("expected temp_c 21.0, got %f", resp.Current.TempC)
}
}
func TestCurrentService_Get_InvalidQuery(t *testing.T) {
client, err := NewClient("test-key")
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
if _, err := client.Current.Get(context.Background(), ""); err == nil {
t.Fatal("expected error for empty query, got nil")
}
}
func TestHistoryService_Get_DecodesForecastShape(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/history.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"location": map[string]any{"name": "London"},
"forecast": map[string]any{
"forecastday": []map[string]any{
{
"date": "2024-01-01",
"day": map[string]any{"avgtemp_c": 5.5},
"astro": map[string]any{
"sunrise": "08:00 AM",
},
},
},
},
})
})
defer cleanup()
resp, err := client.History.Get(context.Background(), HistoryRequest{
Query: "London",
Date: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
})
if err != nil {
t.Fatalf("History.Get failed: %v", err)
}
if len(resp.Forecast.ForecastDay) != 1 {
t.Fatalf("expected 1 forecastday, got %d", len(resp.Forecast.ForecastDay))
}
if resp.Forecast.ForecastDay[0].Day.AvgTempC != 5.5 {
t.Errorf("expected avgtemp_c 5.5, got %f", resp.Forecast.ForecastDay[0].Day.AvgTempC)
}
if resp.Forecast.ForecastDay[0].Astro.Sunrise != "08:00 AM" {
t.Errorf("expected sunrise 08:00 AM, got %s", resp.Forecast.ForecastDay[0].Astro.Sunrise)
}
}
func TestHistoryService_Get_InvalidDate(t *testing.T) {
client, err := NewClient("test-key")
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
if _, err := client.History.Get(context.Background(), HistoryRequest{Query: "London"}); err == nil {
t.Fatal("expected error for zero date, got nil")
}
tooOld := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
if _, err := client.History.Get(context.Background(), HistoryRequest{Query: "London", Date: tooOld}); err == nil {
t.Fatal("expected error for date before 2010-01-01, got nil")
}
}
func TestRawRequest_DecodesAPIError(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(map[string]any{
"error": map[string]any{"code": 1006, "message": "No matching location found."},
})
})
defer cleanup()
_, err := client.Current.Get(context.Background(), "nowhere")
if err == nil {
t.Fatal("expected error, got nil")
}
if got := err.Error(); got == "unexpected status code: 400" {
t.Errorf("expected decoded API error message, got fallback: %s", got)
}
}
func TestSearchService_Get_URLEscapesQuery(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("q"); got != "New York, USA" {
t.Errorf("unexpected q param after round-trip: %s", got)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode([]map[string]any{
{"name": "New York"},
})
})
defer cleanup()
results, err := client.Search.Get(context.Background(), "New York, USA")
if err != nil {
t.Fatalf("Search.Get failed: %v", err)
}
if len(results) != 1 || results[0].Name != "New York" {
t.Errorf("unexpected results: %+v", results)
}
}
func TestForecastService_Get_DefaultsDaysAndValidatesRange(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if got := r.URL.Query().Get("days"); got != "1" {
t.Errorf("expected default days=1, got %s", got)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{})
})
defer cleanup()
if _, err := client.Forecast.Get(context.Background(), ForecastRequest{Query: "Paris"}); err != nil {
t.Fatalf("Forecast.Get failed: %v", err)
}
if _, err := client.Forecast.Get(context.Background(), ForecastRequest{Query: "Paris", Days: 20}); err == nil {
t.Fatal("expected error for days > 14, got nil")
}
}
func TestTimezoneService_Get(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/timezone.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"location": map[string]any{"name": "Sydney", "localtime": "2024-01-01 10:00"},
})
})
defer cleanup()
resp, err := client.Timezone.Get(context.Background(), "Sydney")
if err != nil {
t.Fatalf("Timezone.Get failed: %v", err)
}
if resp.Location.Localtime != "2024-01-01 10:00" {
t.Errorf("unexpected localtime: %s", resp.Location.Localtime)
}
}
func TestIPService_Get(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ip.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{"ip": "1.2.3.4", "city": "Berlin"})
})
defer cleanup()
resp, err := client.IP.Get(context.Background(), "1.2.3.4")
if err != nil {
t.Fatalf("IP.Get failed: %v", err)
}
if resp.City != "Berlin" {
t.Errorf("unexpected city: %s", resp.City)
}
}
func TestMarineService_Get_DefaultsDaysAndValidatesRange(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/marine.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
if got := r.URL.Query().Get("days"); got != "1" {
t.Errorf("expected default days=1, got %s", got)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"location": map[string]any{"name": "Miami"},
"forecast": map[string]any{
"forecastday": []map[string]any{
{
"date": "2024-01-01",
"hour": []map[string]any{
{"time": "2024-01-01 00:00", "swell_ht_mt": 1.2, "water_temp_c": 24.5},
},
},
},
},
})
})
defer cleanup()
resp, err := client.Marine.Get(context.Background(), MarineRequest{Query: "Miami"})
if err != nil {
t.Fatalf("Marine.Get failed: %v", err)
}
if len(resp.Forecast.ForecastDay) != 1 {
t.Fatalf("expected 1 forecastday, got %d", len(resp.Forecast.ForecastDay))
}
if got := resp.Forecast.ForecastDay[0].Hour[0].SwellHeightMT; got != 1.2 {
t.Errorf("expected swell_ht_mt 1.2, got %f", got)
}
if got := resp.Forecast.ForecastDay[0].Hour[0].WaterTempC; got != 24.5 {
t.Errorf("expected water_temp_c 24.5, got %f", got)
}
if _, err := client.Marine.Get(context.Background(), MarineRequest{Query: "Miami", Days: 10}); err == nil {
t.Fatal("expected error for days > 7, got nil")
}
}
func TestMarineService_Get_InvalidQuery(t *testing.T) {
client, err := NewClient("test-key")
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
if _, err := client.Marine.Get(context.Background(), MarineRequest{}); err == nil {
t.Fatal("expected error for empty query, got nil")
}
}
func TestAstronomyService_Get(t *testing.T) {
client, cleanup := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/astronomy.json" {
t.Errorf("unexpected path: %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"astronomy": map[string]any{"astro": map[string]any{"sunrise": "07:00 AM"}},
})
})
defer cleanup()
resp, err := client.Astronomy.Get(context.Background(), AstronomyRequest{Query: "Paris"})
if err != nil {
t.Fatalf("Astronomy.Get failed: %v", err)
}
if resp.Astronomy.Astro.Sunrise != "07:00 AM" {
t.Errorf("unexpected sunrise: %s", resp.Astronomy.Astro.Sunrise)
}
}
func TestWithTimeout(t *testing.T) {
client, err := NewClient("test-key", WithTimeout(5*time.Second))
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
if client.httpClient.Timeout != 5*time.Second {
t.Errorf("expected timeout 5s, got %s", client.httpClient.Timeout)
}
}