forked from icodealot/noaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noaa_test.go
138 lines (120 loc) · 3.73 KB
/
noaa_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
//go:build !examples
// +build !examples
// Unit tests can be run with `go test -v` and require access to the API. Many of
// these tests are actually integration tests that call the weather.gov API and
// parse responses accordingly to confirm expected responses are returned.
//
// Thus, in the future if weather.gov changes the endpoints or responses, these
// tests should alert users of this wrapper SDK accordingly.
package noaa_test
import (
"net/http"
"testing"
"time"
"github.com/icodealot/noaa"
)
func TestBlank(t *testing.T) {
point, err := noaa.Points("", "")
if point == nil && err != nil {
return
}
t.Error("noaa.Points() should return a 404 error for a blank lat, lon.")
}
func TestBlankLat(t *testing.T) {
point, err := noaa.Points("", "-147.7390417")
if point == nil && err != nil {
return
}
t.Error("noaa.Points() should return a 404 error for a blank lat.")
}
func TestBlankLon(t *testing.T) {
point, err := noaa.Points("64.828421", "")
if point == nil && err != nil {
return
}
t.Error("noaa.Points() should return a 404 error for a blank lon.")
}
func TestZero(t *testing.T) {
point, err := noaa.Points("0", "0")
if point == nil && err != nil {
return
}
t.Error("noaa.Points() should return a 404 error for a zero lat, lon.")
}
func TestInternational(t *testing.T) {
point, err := noaa.Points("48.85660", "2.3522") // Paris, France
if point == nil && err != nil {
return
}
t.Error("noaa.Points() should return a 404 error for lat, lon outside the U.S. territories.")
}
func TestAlaska(t *testing.T) {
point, err := noaa.Points("64.828421", "-147.7390417")
if point != nil && err == nil {
return
}
t.Error("noaa.Points() should return valid points for parts of Alaska.")
}
func TestMetricUnits(t *testing.T) {
noaa.SetUnits("si")
forecast, err := noaa.Forecast("41.837", "-87.685")
if err != nil || forecast == nil {
t.Error("noaa.Forecast() should return valid data for Chicago.")
return
}
if forecast.Units != "si" {
t.Error("noaa.Forecast() should return valid data for Chicago in metric.")
}
}
func TestUSUnits(t *testing.T) {
noaa.SetUnits("us")
forecast, err := noaa.Forecast("41.837", "-87.685")
if err != nil {
t.Error("noaa.Forecast() should return valid data for Chicago.")
}
if forecast.Units != "us" {
t.Error("noaa.Forecast() should return valid data for Chicago in standard units.")
}
}
func TestChicagoOffice(t *testing.T) {
office, err := noaa.Office("LOT")
if office != nil && err == nil {
if office.Name == "Chicago, IL" {
return
}
}
t.Error("noaa.Office(\"LOT\") should return valid office information.")
}
func TestChicagoHourly(t *testing.T) {
hourly, err := noaa.HourlyForecast("41.837", "-87.685")
if err != nil {
t.Error("noaa.HourlyForecast() should return valid data for Chicago.")
}
if len(hourly.Periods) == 0 {
t.Error("expected at least one period")
}
}
func TestSetClient(t *testing.T) {
// Intentionally create a client with an absurd timeout value.
client := &http.Client{
Timeout: time.Millisecond,
}
// Don't set the client, to ensure this still works as normal.
_, err := noaa.HourlyForecast("41.837", "-87.685")
if err != nil {
t.Errorf("should have successfully returned a result instead of this error: %s", err)
}
// Set the client to test this feature.
noaa.SetClient(client)
// See if we can make a (failing) request with this.
_, err = noaa.HourlyForecast("41.837", "-87.685")
if err == nil {
t.Error("should have failed the request, 1 millisecond is too short a timeout to make the request")
}
// Test that setting to nil returns to http.DefaultClient.
noaa.SetClient(nil)
_, err = noaa.HourlyForecast("41.837", "-87.685")
if err != nil {
t.Errorf("should have successfully returned a result instead of this error: %s", err)
}
}