-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation_test.go
227 lines (218 loc) · 5.96 KB
/
station_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
// SPDX-FileCopyrightText: 2023 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package meteologix
import (
"encoding/json"
"errors"
"testing"
)
func TestClient_StationSearchByLocation(t *testing.T) {
esid := "199942"
ak := getAPIKeyFromEnv(t)
if ak == "" {
t.Skip("no API_KEY found in environment")
}
c := New(WithAPIKey(ak))
if c == nil {
t.Errorf("failed to create new Client, got nil")
return
}
sl, err := c.StationSearchByLocation("Cologne, Germany")
if err != nil {
t.Errorf("StationSearchByLocation failed: %s", err)
return
}
if len(sl) < 1 {
t.Errorf("StationSearchByLocation failed, got no results")
}
if sl[0].ID != esid {
t.Errorf("StationSearchByLocation failed, expected ID: %s, got: %s",
esid, sl[0].ID)
}
}
func TestClient_StationSearchByLocation_Fail(t *testing.T) {
c := New(WithUsername("invalid"), WithPassword("invalid"))
if c == nil {
t.Errorf("failed to create new Client, got nil")
return
}
_, err := c.StationSearchByLocation("Cologne, Germany")
if err == nil {
t.Errorf("StationSearchByLocation was supposed to fail but didn't")
}
if err != nil && !errors.As(err, &APIError{}) {
t.Errorf("StationSearchByLocation was supposed to throw a APIError but didn't: %s",
err)
}
c = New(WithAPIKey("invalid"))
_, err = c.StationSearchByLocation("Cologne, Germany")
if err == nil {
t.Errorf("StationSearchByLocation was supposed to fail but didn't")
return
}
if err != nil && !errors.As(err, &APIError{}) {
t.Errorf("StationSearchByLocation was supposed to throw a APIError but didn't: %s",
err)
}
}
func TestClient_StationSearchByLocationWithRadius_Fail(t *testing.T) {
ak := getAPIKeyFromEnv(t)
if ak == "" {
t.Skip("no API_KEY found in environment, skipping test")
}
c := New(WithAPIKey(ak))
if c == nil {
t.Errorf("failed to create new Client, got nil")
return
}
_, err := c.StationSearchByLocationWithinRadius("Cologne, Germany", 0)
if err == nil {
t.Errorf("StationSearchByLocationWithRadius was supposed to fail but didn't")
}
if !errors.Is(err, ErrRadiusTooSmall) {
t.Errorf("StationSearchByLocationWithRadius was supposed to return ErrRadiusTooSmall, got: %s", err)
}
_, err = c.StationSearchByLocationWithinRadius("Cologne, Germany", 1000)
if err == nil {
t.Errorf("StationSearchByLocationWithRadius was supposed to fail but didn't")
}
}
func TestClient_StationSearchByCoordinates_Mock(t *testing.T) {
// Expected station data from mock API
p := PrecisionHigh
ty := "STATDEU6"
es := Station{
Altitude: 822,
Distance: 12.6,
ID: "106350",
Latitude: 50.221,
Longitude: 8.4469,
Name: "Feldberg/Taunus",
Precision: &p,
RecentlyActive: true,
Type: &ty,
}
c := New(withMockAPI())
if c == nil {
t.Errorf("failed to create new Client, got nil")
return
}
sl, err := c.StationSearchByCoordinates(es.Latitude, es.Longitude)
if err != nil {
t.Errorf("StationSearchByCoordinates failed: %s", err)
return
}
if len(sl) < 1 {
t.Errorf("StationSearchByCoordinates failed, got no results")
}
rs := sl[0]
if rs.Altitude != es.Altitude {
t.Errorf("StationSearchByCoordinates failed, expected altitude: %d, got: %d",
es.Altitude, rs.Altitude)
}
if rs.Distance != es.Distance {
t.Errorf("StationSearchByCoordinates failed, expected distance: %f, got: %f",
es.Distance, rs.Distance)
}
if rs.ID != es.ID {
t.Errorf("StationSearchByCoordinates failed, expected id: %s, got: %s",
es.ID, rs.ID)
}
if rs.Latitude != es.Latitude {
t.Errorf("StationSearchByCoordinates failed, expected latitude: %f, got: %f",
es.Latitude, rs.Latitude)
}
if rs.Longitude != es.Longitude {
t.Errorf("StationSearchByCoordinates failed, expected longitude: %f, got: %f",
es.Longitude, rs.Longitude)
}
if rs.Name != es.Name {
t.Errorf("StationSearchByCoordinates failed, expected name: %s, got: %s",
es.Name, rs.Name)
}
if rs.Precision.String() != es.Precision.String() {
t.Errorf("StationSearchByCoordinates failed, expected precision: %s, got: %s",
es.Precision, rs.Precision)
}
if rs.RecentlyActive != es.RecentlyActive {
t.Errorf("StationSearchByCoordinates failed, expected recently active: %t, got: %t",
es.RecentlyActive, rs.RecentlyActive)
}
if *rs.Type != *es.Type {
t.Errorf("StationSearchByCoordinates failed, expected type: %s, got: %s",
*es.Type, *rs.Type)
}
}
func TestPrecision_UnmarshalJSON(t *testing.T) {
type tj struct {
Precision Precision `json:"precision"`
}
tt := []struct {
// Test name
n string
// JSON data
d []byte
// Expected precision
p Precision
// Should fail
sf bool
}{
{
"Super high precision", []byte(`{"precision":"SUPER_HIGH"}`), PrecisionSuperHigh,
false,
},
{
"High precision", []byte(`{"precision":"HIGH"}`), PrecisionHigh,
false,
},
{
"Standard precision", []byte(`{"precision":"STANDARD"}`), PrecisionStandard,
false,
},
{
"Unknown precision", []byte(`{"precision":"TEST"}`), PrecisionUnknown,
false,
},
{
"No precision", []byte(`{"precision":null}`), PrecisionUnknown,
false,
},
}
for _, tc := range tt {
t.Run(tc.n, func(t *testing.T) {
var p tj
if err := json.Unmarshal(tc.d, &p); err != nil && !tc.sf {
t.Errorf("JSON unmarshal failed: %s", err)
return
}
if p.Precision != tc.p {
t.Errorf("UnmarshalJSON failed, expected: %s, got: %s", tc.p.String(),
p.Precision.String())
}
})
}
}
func TestPrecision_String(t *testing.T) {
tt := []struct {
// Test name
n string
// Precision value
p Precision
// Expected string
es string
}{
{"Super high precision", PrecisionSuperHigh, "SUPER_HIGH"},
{"High precision", PrecisionHigh, "HIGH"},
{"Standard precision", PrecisionStandard, "STANDARD"},
{"Unknown precision", PrecisionUnknown, "UNKNOWN"},
{"Undefined precision", 999, "UNKNOWN"},
}
for _, tc := range tt {
t.Run(tc.n, func(t *testing.T) {
if got := tc.p.String(); got != tc.es {
t.Errorf("String failed expected %s, got: %s", tc.es, got)
}
})
}
}