-
Notifications
You must be signed in to change notification settings - Fork 0
/
name_test.go
executable file
·167 lines (146 loc) · 3.9 KB
/
name_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package restcountries
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestNameSimple(t *testing.T) {
testClient := New("TEST_API_KEY")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"name":"France", "capital": "Paris"}]`)
}))
defer server.Close()
testClient.SetApiRoot(server.URL)
result, _ := testClient.Name(NameOptions{
Fields: []string{"Name", "Capital"},
Name: "France",
})
got := result[0].Name
want := "France"
if got != want {
t.Fatalf("got %s; want %s", got, want)
}
}
func TestNameErrorUrl(t *testing.T) {
testClient := New("TEST_API_KEY")
testClient.SetApiRoot("not a url")
_, gotErr := testClient.Name(NameOptions{
Name: "France",
})
wantErr := `Get "not%20a%20url/name/France?access_key=TEST_API_KEY&fields=": unsupported protocol scheme ""`
if gotErr == nil || gotErr.Error() != wantErr {
t.Fatalf("got %s; want %s", gotErr, wantErr)
}
}
func TestName(t *testing.T) {
testClient := New("TEST_API_KEY")
tests := []struct {
input NameOptions
response string
want []Country
wantErr string
checkTypeAndEmpty string
}{
{
// empty search term
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "",
},
response: ``,
wantErr: `Search term is empty`,
},
{
// not found
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "ABCDEF",
},
response: `{"status": 404, "message": "Not Found"}`,
want: []Country{},
checkTypeAndEmpty: "[]restcountries.Country",
},
{
// invalid json
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "France",
},
response: `[{"name""Fran`,
wantErr: `invalid character '"' after object key`,
},
{
// custom error
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "France",
},
response: `{"status": 500, "message": "Custom Message"}`,
wantErr: `Custom Message`,
},
{
// single country
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "France",
},
response: `[{"name":"France", "capital": "Paris"}]`,
want: []Country{
{Name: "France", Capital: "Paris"},
},
},
{
// multiple countries
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "United",
},
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"},
},
},
{
// FullText on (exact name match)
input: NameOptions{
Fields: []string{"Name", "Capital"},
Name: "France",
FullText: true,
},
response: `[{"name":"France", "capital": "Paris"}]`,
want: []Country{
{Name: "France", Capital: "Paris"},
},
},
}
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.Name(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)
}
}
}