This repository was archived by the owner on Nov 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdarksky_test.go
144 lines (107 loc) · 3.64 KB
/
darksky_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
package darksky
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestForecastRequest_Get(t *testing.T) {
usingTestServer(validForecastHandler, func(testURL string) {
resp := MakeRequest(key, 41.8781, -87.6297).WithBaseURL(testURL).Get()
if resp.Error != nil {
t.Error(resp.Error)
return
}
if resp.APICallCount != 1 {
t.Errorf("Expected APICallCount to be %v but was %v.", 1, resp.APICallCount)
}
forecast := resp.Forecast
if len(forecast.Alerts) != 3 {
t.Error("Expected 3 Alerts.")
}
})
}
func TestForecastRequest_Get_InvalidArgs(t *testing.T) {
resp := MakeRequest("", 41.0, -87.62).Get()
if resp.Error == nil || resp.Error.Error() != KeyRequired {
t.Error("Expected empty key to be invalid.")
}
resp = MakeRequest("abc", -91.0, 0).Get()
if resp.Error == nil || resp.Error.Error() != LatitudeInvalid {
t.Error("Expected latitude to be invalid.")
}
resp = MakeRequest("abc", 91.0, 0).Get()
if resp.Error == nil || resp.Error.Error() != LatitudeInvalid {
t.Error("Expected latitude to be invalid.")
}
resp = MakeRequest("abc", 0, -181.0).Get()
if resp.Error == nil || resp.Error.Error() != LongitudeInvalid {
t.Error("Expected longitude to be invalid.")
}
resp = MakeRequest("abc", 0, 181.0).Get()
if resp.Error == nil || resp.Error.Error() != LongitudeInvalid {
t.Error("Expected longitude to be invalid.")
}
}
func TestForecastRequest_Get_WithError(t *testing.T) {
usingTestServer(errorForecastHandler, func(testURL string) {
resp := MakeRequest(key, 41.8781, -87.6297).WithBaseURL(testURL).Get()
if resp.Error == nil {
t.Error("Expected an HTTP Error Response to result in an error.")
}
if resp.Error.Error() != "A Server Error Occurred." {
t.Error("Error() was not the expected value.")
}
})
}
func TestForecastRequest_URL(t *testing.T) {
req := MakeRequest("foo", 41.1234, -81.1234)
verifyURL := func(r *ForecastRequest, expectedURL string) {
u, err := req.URL()
if err != nil {
t.Error(err)
}
if u != expectedURL {
t.Errorf("Got: %v\nExpected: %v", u, expectedURL)
}
}
verifyURL(req, "https://api.darksky.net/forecast/foo/41.1234,-81.1234?lang=en&units=us")
req.WithLang(Spanish)
verifyURL(req, "https://api.darksky.net/forecast/foo/41.1234,-81.1234?lang=es&units=us")
req.WithUnits(SI)
verifyURL(req, "https://api.darksky.net/forecast/foo/41.1234,-81.1234?lang=es&units=si")
req.WithTime(12345)
verifyURL(req, "https://api.darksky.net/forecast/foo/41.1234,-81.1234,12345?lang=es&units=si")
}
func TestDataPoint_WindDirection(t *testing.T) {
dp := DataPoint{WindBearing: 147}
if dp.WindDirection() != "SE" {
t.Errorf("Expected WindBearing of %v to be SE, was %v.", dp.WindBearing, dp.WindDirection())
}
dp.WindBearing = 0
if dp.WindDirection() != "N" {
t.Errorf("Expected WindBearing of %v to be N, was %v.", dp.WindBearing, dp.WindDirection())
}
dp.WindBearing = 336
if dp.WindDirection() != "NW" {
t.Errorf("Expected WindBearing of %v to be NW, was %v.", dp.WindBearing, dp.WindDirection())
}
}
var key string = "test_key"
var validForecastHandler http.HandlerFunc = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
jsonBytes, _ := ioutil.ReadFile("testdata/chicago_forecast.json")
resp.Header().Add(APICallsHeader, "1")
resp.WriteHeader(200)
resp.Write(jsonBytes)
})
var errorForecastHandler http.HandlerFunc = http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
resp.WriteHeader(500)
resp.Write([]byte("A Server Error Occurred."))
})
func usingTestServer(handler http.HandlerFunc, runTest func(testURL string)) {
ts := httptest.NewServer(handler)
defer func() {
ts.Close()
}()
runTest(ts.URL)
}