-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathhandlers_test.go
189 lines (178 loc) · 4.13 KB
/
handlers_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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package zoom
import (
"errors"
"reflect"
"testing"
"github.com/davecgh/go-spew/spew"
)
func TestAlwaysErrorHandler(t *testing.T) {
expectedErr := errors.New("This is an error")
handler := newAlwaysErrorHandler(expectedErr)
gotErr := handler([]byte("This is a reply"))
if gotErr.Error() != expectedErr.Error() {
t.Errorf("Expected %v but got %v", expectedErr, gotErr)
}
}
func TestScanIntHandler(t *testing.T) {
i := 5
expectedValue := 3
handler := NewScanIntHandler(&i)
if err := handler([]byte("3")); err != nil {
t.Fatal(err)
}
if i != expectedValue {
t.Errorf("Expected %v but got %v", expectedValue, i)
}
}
func TestScanBoolHandler(t *testing.T) {
b := false
expectedValue := true
handler := NewScanBoolHandler(&b)
if err := handler([]byte("true")); err != nil {
t.Fatal(err)
}
if b != expectedValue {
t.Errorf("Expected %v but got %v", expectedValue, b)
}
}
func TestScanStringHandler(t *testing.T) {
s := "foo"
expectedValue := "bar"
handler := NewScanStringHandler(&s)
if err := handler([]byte("bar")); err != nil {
t.Fatal(err)
}
if s != expectedValue {
t.Errorf("Expected %v but got %v", expectedValue, s)
}
}
func TestScanFloat64Handler(t *testing.T) {
f := 4.3
expectedValue := 42.0
handler := NewScanFloat64Handler(&f)
if err := handler([]byte("42.0")); err != nil {
t.Fatal(err)
}
if f != expectedValue {
t.Errorf("Expected %v but got %v", expectedValue, f)
}
}
func TestScanStringsHandler(t *testing.T) {
strings := []string{"foo", "bar", "biz"}
expectedValue := []string{"biz", "baz", "bar"}
handler := NewScanStringsHandler(&strings)
if err := handler([]interface{}{
[]byte("biz"),
[]byte("baz"),
[]byte("bar"),
}); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(strings, expectedValue) {
t.Errorf("Expected %v but got %v", expectedValue, strings)
}
}
func TestScanModelHandler(t *testing.T) {
testingSetUp()
defer testingTearDown()
model := testModel{
Int: 1,
String: "foo",
Bool: true,
}
expectedValue := testModel{
Int: 38,
String: "bar",
Bool: true,
RandomID: RandomID{
ID: "thisIsANewID",
},
}
fieldNames := []string{"String", "-", "Int"}
handler := NewScanModelHandler(fieldNames, &model)
if err := handler([]interface{}{
[]byte("bar"),
[]byte("thisIsANewID"),
[]byte("38"),
}); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(model, expectedValue) {
t.Errorf("Expected %v but got %v", expectedValue, model)
}
}
func TestScanModelsHandler(t *testing.T) {
testingSetUp()
defer testingTearDown()
models := []*testModel{}
expectedValue := []*testModel{
{
Int: 38,
String: "bar",
Bool: false,
RandomID: RandomID{
ID: "thisIsANewID",
},
},
{
Int: 37,
String: "biz",
Bool: false,
RandomID: RandomID{
ID: "thisIsAlsoANewID",
},
},
}
fieldNames := []string{"String", "-", "Int"}
handler := NewScanModelsHandler(testModels, fieldNames, &models)
if err := handler([]interface{}{
[]byte("bar"),
[]byte("thisIsANewID"),
[]byte("38"),
[]byte("biz"),
[]byte("thisIsAlsoANewID"),
[]byte("37"),
}); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(models, expectedValue) {
t.Errorf("\nExpected: %s\nBut got: %s\n",
spew.Sprint(expectedValue),
spew.Sprint(models),
)
}
}
func TestScanOneModelHandler(t *testing.T) {
testingSetUp()
defer testingTearDown()
expected := &testModel{
Int: 38,
String: "bar",
Bool: false,
RandomID: RandomID{
ID: "thisIsAnID",
},
}
fieldNames := []string{"String", "-", "Int"}
got := &testModel{}
handler := newScanOneModelHandler(testModels.NewQuery().query, testModels.spec, fieldNames, got)
if err := handler([]interface{}{
[]byte("bar"),
[]byte("thisIsAnID"),
[]byte("38"),
}); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expected, got) {
t.Errorf("\nExpected: %s\nBut got: %s\n",
spew.Sprint(expected),
spew.Sprint(got),
)
}
// If reply is nil, the ReplyHandler should return a ModelNotFoundError.
if err := handler(nil); err == nil {
t.Error("Expected error but got none")
} else if _, ok := err.(ModelNotFoundError); !ok {
t.Errorf("Expected ModelNotFoundError but got: %T: %v", err, err)
}
}