-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_test.go
executable file
·141 lines (119 loc) · 3.38 KB
/
all_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
package restcountries
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
)
func TestAllSimple(t *testing.T) {
testClient := New("TEST_API_KEY")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"name":"TestName", "capital": "testCap"}, {"name":"TestName2", "capital": "testCap2"}]`)
}))
defer server.Close()
testClient.SetApiRoot(server.URL)
testClient.SetTimeout(10 * time.Second)
result, _ := testClient.All(AllOptions{
Fields: []string{"Name", "Capital"},
})
got := len(result)
want := 2
if got != want {
t.Fatalf("got len %d; wanted %d", got, want)
}
}
func TestAllErrorUrl(t *testing.T) {
testClient := New("TEST_API_KEY")
testClient.SetApiRoot("not a url")
_, gotErr := testClient.All(AllOptions{})
wantErr := `Get "not%20a%20url/all?access_key=TEST_API_KEY&fields=": unsupported protocol scheme ""`
if gotErr == nil || gotErr.Error() != wantErr {
t.Fatalf("got %s; want %s", gotErr, wantErr)
}
}
func TestAll(t *testing.T) {
testClient := New("TEST_API_KEY")
tests := []struct {
input AllOptions
response string
want []Country
wantErr string
checkTypeAndEmpty string
}{
{
// not found
input: AllOptions{
Fields: []string{"Name", "Capital"},
},
response: `{"status": 404, "message": "Not Found"}`,
want: []Country{},
checkTypeAndEmpty: "[]restcountries.Country",
},
{
// invalid json
input: AllOptions{
Fields: []string{"Name", "Capital"},
},
response: `[{"name""Fran`,
wantErr: `invalid character '"' after object key`,
},
{
// custom error
input: AllOptions{
Fields: []string{"Name", "Capital"},
},
response: `{"status": 500, "message": "Custom Message"}`,
wantErr: `Custom Message`,
},
{
// single country
input: AllOptions{
Fields: []string{"Name", "Capital"},
},
response: `[{"name":"France", "capital": "Paris"}]`,
want: []Country{
{Name: "France", Capital: "Paris"},
},
},
{
// multiple countries
input: AllOptions{
Fields: []string{"Name", "Capital"},
},
response: `[{"name":"United States of America", "capital": "Washington DC"}, {"name":"United Arab Emirates", "capital": "Abu Dhabi"}]`,
want: []Country{
{Name: "United States of America", Capital: "Washington DC"},
{Name: "United Arab Emirates", Capital: "Abu Dhabi"},
},
},
}
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.All(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)
}
}
}