-
Notifications
You must be signed in to change notification settings - Fork 6
/
client_test.go
113 lines (90 loc) · 2.6 KB
/
client_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
// Copyright © 2016 Aaron Longwell
//
// Use of this source code is governed by an MIT licese.
// Details in the LICENSE file.
package darksky
import (
"testing"
"time"
)
func TestGetForecast(t *testing.T) {
c := testClient()
c.BaseURL = mockResponse("forecast-response.json").URL
forecast, err := c.GetForecast("47.202", "-123.4167", Defaults)
if err != nil {
t.Fatal(err)
}
curr := forecast.Currently
if curr.Time.UTC().Format("2006-01-02 15:04:05") != "2016-09-28 03:46:47" {
t.Errorf("Got incorrect time '%s'", curr.Time.UTC())
}
if curr.Summary != "Clear" {
t.Errorf("Expected summary 'Clear'. Got '%s'.", forecast.Currently.Summary)
}
if curr.Icon != "clear-night" {
t.Errorf("Got incorrect icon '%s'.", curr.Icon)
}
if curr.NearestStormDistance != 3 {
t.Errorf("Got incorrect NearestStormDistance %f", curr.NearestStormDistance)
}
if curr.Temperature != 55.13 {
t.Errorf("Got incorrect temperature %.2f.", curr.Temperature)
}
minutely := forecast.Minutely
if len(minutely.Data) != 61 {
t.Errorf("Expected 61 Minutely entries. Got %d.", len(minutely.Data))
}
}
func TestGetMinimalForecast(t *testing.T) {
c := testClient()
c.BaseURL = mockResponse("minimal-with-alerts.json").URL
forecast, err := c.GetForecast("38.95663", "-77.396338", CurrentOnly)
if err != nil {
t.Fatal(err)
}
curr := forecast.Currently
if curr.Summary != "Clear" {
t.Errorf("Got incorrect summary '%s'.", curr.Summary)
}
alerts := forecast.Alerts
if len(alerts) != 2 {
t.Errorf("Expected 2 alerts. Got %d", len(alerts))
}
if alerts[0].Title != "Flash Flood Watch for Montgomery, MD" {
t.Errorf("Incorrect alert title: '%s'.", alerts[0].Title)
}
}
func TestGetTimeMachine(t *testing.T) {
c := testClient()
d := time.Date(2012, 6, 29, 23, 48, 5, 0, time.UTC)
c.BaseURL = mockResponse("time-machine-response.json").URL
forecast, err := c.GetTimeMachineForecast("38.95663", "-77.396338", d, Defaults)
if err != nil {
t.Error(err)
}
curr := forecast.Currently
if curr.Summary != "Rain and Breezy" {
t.Errorf("Incorrect summary '%s'.", curr.Summary)
}
if curr.Icon != "rain" {
t.Errorf("Incorrect icon '%s'.", curr.Icon)
}
if curr.Temperature != 71.59 {
t.Errorf("Got incorrect temperature %.2f.", curr.Temperature)
}
if forecast.Minutely != nil {
t.Error("Expected .Minutely to be absent.")
}
}
func TestGetWithBadURL(t *testing.T) {
c := testClient()
c.BaseURL = "gopher://test"
_, err := c.GetForecast("1.23", "-1.23", Defaults)
if err == nil {
t.Fatal("GetForecast() should fail with an invalid URL.")
}
}
func testClient() *Client {
c := NewClient("APIKEY")
return c
}