-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_id_test.go
97 lines (83 loc) · 1.87 KB
/
convert_id_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
package brewerydb
import (
"fmt"
"net/http"
"testing"
)
var fakeDataConvertID = `{
"status" : "success",
"data" : [
{
"oldId" : 23,
"newId" : "DJcbV1"
},
{
"oldId" : 299,
"newId" : "n75rVD"
},
{
"oldId" : 599,
"newId" : "CXuX9r"
}
],
"message" : "Request Successful"
}`
func TestConvertIDs(t *testing.T) {
setup()
defer teardown()
var (
ids = []int{23, 299, 599}
sids = "23,299,599"
mapping = map[int]string{
23: "DJcbV1",
299: "n75rVD",
599: "CXuX9r",
}
)
convertBeer := true
mux.HandleFunc("/convertid", func(w http.ResponseWriter, r *http.Request) {
checkMethod(t, r, "POST")
if err := r.ParseForm(); err != nil {
http.Error(w, "failed to parse form", http.StatusBadRequest)
}
checkPostFormValue(t, r, "ids", sids)
tp := r.PostFormValue("type")
if tp != string(ConvertBeer) && tp != string(ConvertBrewery) {
http.Error(w, "invalid ID type", http.StatusNotFound)
return
}
if convertBeer {
checkPostFormValue(t, r, "type", string(ConvertBeer))
} else {
checkPostFormValue(t, r, "type", string(ConvertBrewery))
}
fmt.Fprint(w, fakeDataConvertID)
})
newIDs, err := client.ConvertID.ConvertIDs(ConvertBeer, ids...)
if err != nil {
t.Fatal(err)
}
for oldID, newID := range newIDs {
if mapping[oldID] != newID {
t.Fatalf("New ID = %v, want %v", newID, mapping[oldID])
}
}
convertBeer = false
newIDs, err = client.ConvertID.ConvertIDs(ConvertBrewery, ids...)
if err != nil {
t.Fatal(err)
}
for oldID, newID := range newIDs {
if mapping[oldID] != newID {
t.Fatalf("New ID = %v, want %v", newID, mapping[oldID])
}
}
_, err = client.ConvertID.ConvertIDs("event", ids...)
if err == nil {
t.Fatal("Expected HTTP 404 error")
}
testBadURL(t, func() error {
_, err = client.ConvertID.ConvertIDs(ConvertBrewery, ids...)
return err
})
}