-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
executable file
·130 lines (99 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package apixu
import (
"os"
"testing"
"time"
)
const InvalidCityName = "INVALIDCITYNAME"
var cities = []string{
"Cairo",
"London",
"Paris",
"Berlin",
"New York",
}
func getAPIKey() string {
return os.Getenv("APIXU_KEY")
}
func TestInvalidAPIKey(t *testing.T) {
client := NewClient("Invalid_Key")
_, err := client.Current("Paris")
if err == nil {
t.Error("Worked with invalid key")
}
}
func TestCurrentWeatherValidCities(t *testing.T) {
client := NewClient(getAPIKey())
for _, city := range cities {
_, err := client.Current(city)
if err != nil {
t.Errorf("There was an error getting current weather of %s: %v", city, err)
}
}
}
func TestCurrentWeatherInValidCity(t *testing.T) {
client := NewClient(getAPIKey())
_, err := client.Current(InvalidCityName)
if err == nil {
t.Error("No errors getting current weather of invalid city name")
}
}
func TestForecastWeatherValidCities(t *testing.T) {
days := []int{1, 5, 10}
client := NewClient(getAPIKey())
for _, day := range days {
for _, city := range cities {
_, err := client.Forecast(city, day)
if err != nil {
t.Errorf("There was an error getting forecast weather of %s days %d: %v", city, day, err)
}
}
}
}
func TestForecastWeatherInValidCity(t *testing.T) {
days := []int{1, 5, 10}
client := NewClient(getAPIKey())
for _, day := range days {
_, err := client.Forecast(InvalidCityName, day)
if err == nil {
t.Error("No errors getting forecast weather of invalid city name")
}
}
}
func TestHistoryWeatherValidCities(t *testing.T) {
yesterday := time.Now().AddDate(0, 0, -1)
client := NewClient(getAPIKey())
for _, city := range cities {
_, err := client.History(city, yesterday.Format("2006-01-2"))
if err != nil {
t.Errorf("There was an error getting current weather of %s: %v", city, err)
}
}
}
func TestHistoryWeatherInValidCity(t *testing.T) {
yesterday := time.Now().AddDate(0, 0, -1)
client := NewClient(getAPIKey())
_, err := client.History(InvalidCityName, yesterday.Format("2006-01-2"))
if err == nil {
t.Error("No errors getting history weather of invalid city name")
}
}
func TestSearchValidCities(t *testing.T) {
client := NewClient(getAPIKey())
for _, city := range cities {
_, err := client.Search(city)
if err != nil {
t.Errorf("There was an error getting current weather of %s: %v", city, err)
}
}
}
func TestSearchInValidCity(t *testing.T) {
client := NewClient(getAPIKey())
matchedCities, err := client.Search(InvalidCityName)
if err != nil {
t.Error(err)
}
if len(*matchedCities) != 0 {
t.Error("Non-Empty array of matched cities for invalid city name")
}
}