generated from bitfield/weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_test.go
234 lines (206 loc) · 5.98 KB
/
weather_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
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
package weather_test
import (
"bytes"
"fmt"
"math"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/aleury/weather"
"github.com/rogpeppe/go-internal/testscript"
"github.com/google/go-cmp/cmp"
)
func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"weather": weather.RunCLI,
}))
}
func TestRunCLIScripts(t *testing.T) {
testscript.Run(t, testscript.Params{
Dir: "testdata/scripts",
})
}
func TestFormatURL_ReturnsURLWithLocationAndToken(t *testing.T) {
t.Parallel()
type testCase struct {
location string
token string
want string
}
tests := []testCase{
{location: "London", token: "dummy_token", want: "https://api.openweathermap.org/data/2.5/weather?q=London&appid=dummy_token"},
{location: "London,UK", token: "dummy_token", want: "https://api.openweathermap.org/data/2.5/weather?q=London%2CUK&appid=dummy_token"},
{location: "New York City", token: "dummy_token", want: "https://api.openweathermap.org/data/2.5/weather?q=New+York+City&appid=dummy_token"},
{location: "New York City,US", token: "dummy_token", want: "https://api.openweathermap.org/data/2.5/weather?q=New+York+City%2CUS&appid=dummy_token"},
}
for _, tc := range tests {
client := weather.NewOpenWeatherClient(tc.token)
got := client.FormatURL(tc.location)
if tc.want != got {
t.Errorf("want %q, got %q", tc.want, got)
}
}
}
func TestParseJSON_ReturnsWeatherConditonsForValidInput(t *testing.T) {
t.Parallel()
f, err := os.Open("testdata/london.json")
if err != nil {
t.Fatal(err)
}
defer f.Close()
want := weather.Conditions{
Summary: "Drizzle",
Temperature: weather.Temperature(280.32),
}
got, err := weather.ParseJSON(f)
if err != nil {
t.Fatal("didn't expect an error parsing json")
}
if !cmp.Equal(want.Summary, got.Summary) {
t.Error(cmp.Diff(want, got))
}
if !closeEnough(float64(want.Temperature), float64(got.Temperature)) {
t.Error(cmp.Diff(want.Temperature, got.Temperature))
}
}
func TestParseJSON_ReturnsErrorForInvalidResponse(t *testing.T) {
t.Parallel()
response := bytes.NewBuffer([]byte(`{"error": "something went wrong"}`))
_, err := weather.ParseJSON(response)
if err == nil {
t.Error("expected to get an error")
}
}
func TestParseJSON_ReturnsErrorForInvalidJSON(t *testing.T) {
t.Parallel()
response := bytes.NewBuffer([]byte(`{`))
_, err := weather.ParseJSON(response)
if err == nil {
t.Error("expected to get an error")
}
}
func TestConditions_CanBeFormattedAsAString(t *testing.T) {
conditions := weather.Conditions{
Summary: "Drizzle",
Temperature: weather.Temperature(280.32),
}
want := "Drizzle 7.2ºC"
got := conditions.String()
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestCurrent_ReturnsPresentWeatherConditonsOfAValidLocation(t *testing.T) {
t.Parallel()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, err := os.ReadFile("testdata/london.json")
if err != nil {
t.Fatalf("reading test data: %s", err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(data)
}))
defer server.Close()
client := weather.NewOpenWeatherClient("dummy_token")
client.BaseURL = server.URL
client.HttpClient = server.Client()
want := weather.Conditions{
Summary: "Drizzle",
Temperature: weather.Temperature(280.32),
}
got, err := client.Current("London,UK")
if err != nil {
t.Fatalf("didn't expect an error")
}
if !cmp.Equal(want.Summary, got.Summary) {
t.Error(cmp.Diff(want, got))
}
if !closeEnough(float64(want.Temperature), float64(got.Temperature)) {
t.Error(cmp.Diff(want.Temperature, got.Temperature))
}
}
func TestCurrent_ReturnsErrorWhenAPIRespondsWithUnknownLocation(t *testing.T) {
t.Parallel()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"error": "unknown location"}`))
}))
defer server.Close()
client := weather.NewOpenWeatherClient("dummy_token")
client.BaseURL = server.URL
client.HttpClient = server.Client()
_, err := client.Current("unknown")
if err == nil {
t.Fatal("expected to get an error")
}
}
func TestCurrent_ReturnsErrorWhenAPIRespondsWithInvalidJSON(t *testing.T) {
t.Parallel()
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{`))
}))
defer server.Close()
client := weather.NewOpenWeatherClient("dummy_token")
client.BaseURL = server.URL
client.HttpClient = server.Client()
_, err := client.Current("London")
if err == nil {
t.Fatal("expected to get an error")
}
}
func TestCurrent_ReturnsErrorForInvalidURL(t *testing.T) {
t.Parallel()
client := weather.NewOpenWeatherClient("dummy_token")
client.BaseURL = "bogus"
_, err := client.Current("unknown")
if err == nil {
t.Fatal("expected to get an error")
}
}
func TestCelsius_ReturnsValueOfTemperatureInCelsius(t *testing.T) {
t.Parallel()
temperature := weather.Temperature(280.35)
want := 7.2
got := temperature.Celsius()
if !closeEnough(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func closeEnough(a, b float64) bool {
return math.Abs(a-b) <= 0.001
}
func ExampleOpenWeatherClient_FormatURL() {
client := weather.NewOpenWeatherClient("dummy_token")
url := client.FormatURL("London,UK")
fmt.Println(url)
// Output:
// https://api.openweathermap.org/data/2.5/weather?q=London%2CUK&appid=dummy_token
}
func ExampleParseJSON() {
f, err := os.Open("testdata/london.json")
if err != nil {
panic(err)
}
defer f.Close()
conditions, err := weather.ParseJSON(f)
if err != nil {
panic(err)
}
fmt.Println(conditions.String())
// Output:
// Drizzle 7.2ºC
}
func ExampleConditions_String() {
conditions := weather.Conditions{
Summary: "Drizzle",
Temperature: weather.Temperature(280.32),
}
fmt.Println(conditions.String())
// Output:
// Drizzle 7.2ºC
}