-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagify_test.go
118 lines (94 loc) · 3.48 KB
/
agify_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
package agify
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestShouldCreateNewClient(t *testing.T) {
client := NewClient()
assert.NotNil(t, client)
}
func TestShouldGetPredictionForName(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Rate-Limit-Limit", "1000")
w.Header().Set("X-Rate-Limit-Remaining", "728")
w.Header().Set("X-Rate-Reset", "15281")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"name":"michael","age":70,"count":875,"country_id":"US"}`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL))
result, rateLimit, err := client.Predict("michael")
assert.Nil(t, err)
assert.Equal(t, 70, result.Age)
assert.Equal(t, 875, result.Count)
assert.Equal(t, "michael", result.Name)
assert.Equal(t, "US", result.Country)
assert.Equal(t, "1000", rateLimit.Limit)
assert.Equal(t, "728", rateLimit.Remaining)
assert.Equal(t, "15281", rateLimit.Reset)
}
func TestShouldGetErrorWhenUnauthorized(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{ "error": "Invalid API key" }`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL))
result, rateLimit, err := client.Predict("michael")
assert.Nil(t, result)
assert.NotNil(t, rateLimit)
assert.NotNil(t, err)
}
func TestShouldGetErrorWhenTooManyRequests(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
w.Write([]byte(`{ "error": "Request limit reached" }`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL))
result, rateLimit, err := client.Predict("michael")
assert.Nil(t, result)
assert.NotNil(t, rateLimit)
assert.NotNil(t, err)
}
func TestShouldGetErrorWhenUnprocessable(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte(`{ "error": "Too many requests" }`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL))
result, rateLimit, err := client.Predict("michael")
assert.Nil(t, result)
assert.NotNil(t, rateLimit)
assert.NotNil(t, err)
}
func TestShouldOverrideDefaults(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "test-key", r.URL.Query().Get("apikey"))
assert.Equal(t, "michael", r.URL.Query().Get("name"))
w.WriteHeader(http.StatusUnprocessableEntity)
w.Write([]byte(`{ "error": "testing" }`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL), WithClient(&http.Client{}), WithApiKey("test-key"))
assert.NotNil(t, client)
_, _, err := client.Predict("michael")
assert.NotNil(t, err)
}
func TestShouldHandleBatchPrediction(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
names := r.URL.Query()["name[]"]
assert.NotNil(t, names)
assert.Len(t, names, 3)
w.Write([]byte(`[{"name":"michael","age":70,"count":233482},{"name":"matthew","age":36,"count":34742},{"name":"jane","age":36,"count":35010}]`))
}))
defer server.Close()
client := NewClient(WithUrl(server.URL))
result, _, err := client.BatchPredict([]string{"michael", "matthew", "jane"})
assert.Nil(t, err)
assert.Len(t, result, 3)
}