forked from briandowns/openweathermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
current_test.go
246 lines (216 loc) · 5.97 KB
/
current_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
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2015 Brian J. Downs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package openweathermap
import (
"log"
"net/http"
"os"
"reflect"
"testing"
"time"
)
// currentWeather holds the query and response
type currentWeather struct {
query string
weather CurrentWeatherData
}
// TestValidLanguageCode will verify that the language code passed in is indeed
// a valid one for use with the API
func TestValidLanguageCode(t *testing.T) {
testCodes := []string{"EN", "DE", "blah"}
for _, i := range testCodes {
if !ValidLangCode(i) {
t.Log("received expected bad code")
}
}
}
// TestNewCurrent will verify that a new instance of CurrentWeatherData is created
func TestNewCurrent(t *testing.T) {
t.Parallel()
for d := range DataUnits {
t.Logf("Data unit: %s", d)
if ValidDataUnit(d) {
c, err := NewCurrent(d, "en", os.Getenv("OWM_API_KEY"))
if err != nil {
t.Error(err)
}
_, err = NewCurrent(d, "blah", os.Getenv("OWM_API_KEY"))
if err != nil {
t.Log("received expected bad language code error")
}
if reflect.TypeOf(c).String() != "*openweathermap.CurrentWeatherData" {
t.Error("incorrect data type returned")
}
} else {
t.Errorf("unusable data unit - %s", d)
}
}
}
// TestNewCurrentWithCustomHttpClient will verify that a new instance of CurrentWeatherData
// is created with custom http client
func TestNewCurrentWithCustomHttpClient(t *testing.T) {
hc := http.DefaultClient
hc.Timeout = time.Duration(1) * time.Second
c, err := NewCurrent("c", "en", os.Getenv("OWM_API_KEY"), WithHttpClient(hc))
if err != nil {
t.Error(err)
}
if reflect.TypeOf(c).String() != "*openweathermap.CurrentWeatherData" {
t.Error("incorrect data type returned")
}
expected := time.Duration(1) * time.Second
if c.client.Timeout != expected {
t.Errorf("Expected Duration %v, but got %v", expected, c.client.Timeout)
}
}
// TestNewCurrentWithInvalidOptions will verify that returns an error with
// invalid option
func TestNewCurrentWithInvalidOptions(t *testing.T) {
optionsPattern := [][]Option{
{nil},
{nil, nil},
{WithHttpClient(&http.Client{}), nil},
{nil, WithHttpClient(&http.Client{})},
}
for _, options := range optionsPattern {
c, err := NewCurrent("c", "en", os.Getenv("OWM_API_KEY"), options...)
if err == errInvalidOption {
t.Logf("Received expected invalid option error. message: %s", err.Error())
} else if err != nil {
t.Errorf("Expected %v, but got %v", errInvalidOption, err)
}
if c != nil {
t.Errorf("Expected nil, but got %v", c)
}
}
}
// TestNewCurrentWithInvalidHttpClient will verify that returns an error with
// invalid http client
func TestNewCurrentWithInvalidHttpClient(t *testing.T) {
c, err := NewCurrent("c", "en", os.Getenv("OWM_API_KEY"), WithHttpClient(nil))
if err == errInvalidHttpClient {
t.Logf("Received expected bad client error. message: %s", err.Error())
} else if err != nil {
t.Errorf("Expected %v, but got %v", errInvalidHttpClient, err)
}
if c != nil {
t.Errorf("Expected nil, but got %v", c)
}
}
// TestCurrentByName will verify that current data can be retrieved for a give
// location by name
func TestCurrentByName(t *testing.T) {
t.Parallel()
testCities := []currentWeather{
{
query: "Philadelphia",
weather: CurrentWeatherData{
ID: 4560349,
Name: "Philadelphia",
Main: Main{
Temp: 35.6,
},
},
},
{
query: "Newark",
weather: CurrentWeatherData{
ID: 5101798,
Name: "Newark",
Main: Main{
Temp: 36.36,
},
},
},
{
query: "Helena",
weather: CurrentWeatherData{
ID: 5656882,
Name: "Helena",
Main: Main{
Temp: 42.8,
},
},
},
{
query: "San Diego, CA",
weather: CurrentWeatherData{
ID: 5391811,
Name: "San Diego",
Main: Main{
Temp: 56.53,
},
},
},
}
testBadCities := []string{"nowhere_", "somewhere_over_the_"}
c, err := NewCurrent("f", "ru", os.Getenv("OWM_API_KEY"))
if err != nil {
t.Error(err)
}
for _, city := range testCities {
c.CurrentByName(city.query)
if os.Getenv("RTCP_HOST") != "" {
if c.ID != city.weather.ID {
t.Errorf("Excpect CityID %ld, got %ld", city.weather.ID, c.ID)
}
if c.Name != city.weather.Name {
t.Errorf("Excpect City %s, got %s", city.weather.Name, c.Name)
}
if c.Main.Temp != city.weather.Main.Temp {
t.Errorf("Excpect Temp %.2f, got %.2f", city.weather.Main.Temp, c.Main.Temp)
}
}
}
for _, badCity := range testBadCities {
if err := c.CurrentByName(badCity); err != nil {
t.Log("received expected failure for bad city by name")
}
}
}
// TestCurrentByCoordinates will verify that current data can be retrieved for a
// given set of coordinates
func TestCurrentByCoordinates(t *testing.T) {
t.Parallel()
c, err := NewCurrent("f", "DE", os.Getenv("OWM_API_KEY"))
if err != nil {
t.Error("Error creating instance of CurrentWeatherData")
}
c.CurrentByCoordinates(
&Coordinates{
Longitude: -112.07,
Latitude: 33.45,
},
)
}
// TestCurrentByID will verify that current data can be retrieved for a given
// location id
func TestCurrentByID(t *testing.T) {
t.Parallel()
c, err := NewCurrent("c", "ZH", os.Getenv("OWM_API_KEY"))
if err != nil {
t.Error("Error creating instance of CurrentWeatherData")
}
c.CurrentByID(5344157)
}
func TestCurrentByZip(t *testing.T) {
w, err := NewCurrent("F", "EN", os.Getenv("OWM_API_KEY"))
if err != nil {
log.Fatalln(err)
}
if err := w.CurrentByZip(19125, "US"); err != nil {
t.Error(err)
}
}
func TestCurrentByArea(t *testing.T) {}