-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion_test.go
145 lines (124 loc) · 3.5 KB
/
region_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
package restcountries
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestRegionSimple(t *testing.T) {
testClient := New("TEST_API_KEY")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"name":"American Samoa", "capital": "Pago Pago"}]`)
}))
defer server.Close()
testClient.SetApiRoot(server.URL)
result, _ := testClient.Region(RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "Oceania",
})
got := result[0].Name
want := "American Samoa"
if got != want {
t.Fatalf("got %s; want %s", got, want)
}
}
func TestRegionErrorUrl(t *testing.T) {
testClient := New("TEST_API_KEY")
testClient.SetApiRoot("not a url")
_, gotErr := testClient.Region(RegionOptions{
Region: "Oceania",
})
wantErr := `Get "not%20a%20url/region/Oceania?access_key=TEST_API_KEY&fields=": unsupported protocol scheme ""`
if gotErr == nil || gotErr.Error() != wantErr {
t.Fatalf("got %s; want %s", gotErr, wantErr)
}
}
func TestRegion(t *testing.T) {
testClient := New("TEST_API_KEY")
tests := []struct {
input RegionOptions
response string
want []Country
wantErr string
checkTypeAndEmpty string
}{
{
// empty search term
input: RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "",
},
response: ``,
wantErr: `Search term is empty`,
},
{
// not found
input: RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "ABC",
},
response: `{"status": 404, "message": "Not Found"}`,
want: []Country{},
checkTypeAndEmpty: "[]restcountries.Country",
},
{
// invalid json
input: RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "Oceania",
},
response: `[{"name""Americ`,
wantErr: `invalid character '"' after object key`,
},
{
// custom error
input: RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "Oceania",
},
response: `{"status": 500, "message": "Custom Message"}`,
wantErr: `Custom Message`,
},
{
// multiple countries
input: RegionOptions{
Fields: []string{"Name", "Capital"},
Region: "Oceania",
},
response: `[{"name":"American Samoa", "capital": "Pago Pago"}, {"name":"Australia","capital":"Canberra"}, {"name":"Christmas Island","capital":"Flying Fish Cove"}]`,
want: []Country{
{Name: "American Samoa", Capital: "Pago Pago"},
{Name: "Australia", Capital: "Canberra"},
{Name: "Christmas Island", Capital: "Flying Fish Cove"},
},
},
}
for _, test := range tests {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, test.response)
}))
defer server.Close()
testClient.SetApiRoot(server.URL)
got, err := testClient.Region(test.input)
// checking for an error
if test.wantErr != "" {
if err.Error() != test.wantErr {
t.Fatalf("want err: %s, got: %s", test.wantErr, err.Error())
}
continue
}
if test.checkTypeAndEmpty != "" { // used for checking the slice type is correct and it is empty
typeStr := reflect.TypeOf(got).String()
if typeStr != test.checkTypeAndEmpty {
t.Fatalf("want: empty %v, got: %v", test.checkTypeAndEmpty, typeStr)
} else if len(got) != 0 {
t.Fatalf("want: empty %v with length 0, got: %v with length %d", test.checkTypeAndEmpty, typeStr, len(got))
}
continue
}
if !reflect.DeepEqual(test.want, got) {
t.Fatalf("want: %v, got: %v", test.want, got)
}
}
}