-
Notifications
You must be signed in to change notification settings - Fork 15
/
create_test.go
221 lines (179 loc) · 4.99 KB
/
create_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package parse
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"testing"
"time"
)
func TestCreateRequiresPointer(t *testing.T) {
u := User{}
expected := "v must be a non-nil pointer"
if err := Create(u, false); err == nil {
t.Error("Create should return an error when argument is not a pointer")
} else if err.Error() != expected {
t.Errorf("Unexpected error message. Got [%s] expected [%s]\n", err, expected)
}
}
type TestUser struct {
FirstName string
LastName string
Email string
Ignore string `parse:"-"`
FCount int `parse:"followers"`
Base
}
func TestPayload(t *testing.T) {
tu := TestUser{
FirstName: "Kyle",
LastName: "M",
Email: "[email protected]",
Ignore: "shouldn't appear in payload",
FCount: 11,
}
cr := createT{
v: &tu,
}
e := map[string]interface{}{
"firstName": "Kyle",
"lastName": "M",
"email": "[email protected]",
"followers": 11,
}
expected := map[string]interface{}{}
eb, _ := json.Marshal(e)
_ = json.Unmarshal(eb, &expected)
actual := map[string]interface{}{}
b, err := cr.body()
if err != nil {
t.Errorf("unexpected error generating payload: %v\n", err)
t.FailNow()
}
err = json.Unmarshal([]byte(b), &actual)
if err != nil {
t.Errorf("unexpected error unmarshaling payload: %v\n", err)
t.FailNow()
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("payload different from expected. expected:\n%s\n\ngot:\n%s\n", eb, b)
}
}
func TestCreate(t *testing.T) {
setupTestServer(func(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get(AppIdHeader); h != "app_id" {
t.Errorf("request did not have App ID header set!")
}
if h := r.Header.Get(RestKeyHeader); h != "rest_key" {
t.Errorf("request did not have Rest Key header set!")
}
if h := r.Header.Get(SessionTokenHeader); h != "" {
t.Errorf("request had Session Token header set!")
}
if h := r.Header.Get(MasterKeyHeader); h != "" {
t.Errorf("request had Master Key header set!")
}
fmt.Fprintf(w, `{"createdAt":"2014-12-19T18:05:57Z","objectId":"abcDEF"}`)
})
defer teardownTestServer()
u := TestUser{
FirstName: "Kyle",
LastName: "M",
Email: "[email protected]",
FCount: 11,
}
err := Create(&u, false)
if err != nil {
t.Errorf("Unexpected error creating object: %v\n", err)
t.FailNow()
}
if u.Id != "abcDEF" {
t.Errorf("Create did not set proper id on instance. u.Id: %v\n", u.Id)
}
if u.CreatedAt != time.Date(2014, 12, 19, 18, 5, 57, 0, time.UTC) {
t.Errorf("Create did not set proper createdAt date. u.CreatedAt: %v\n", u.CreatedAt)
}
}
func TestCreateUseMasterKey(t *testing.T) {
setupTestServer(func(w http.ResponseWriter, r *http.Request) {
if h := r.Header.Get(AppIdHeader); h != "app_id" {
t.Errorf("request did not have App ID header set!")
}
if h := r.Header.Get(RestKeyHeader); h != "" {
t.Errorf("request had Rest Key header set!")
}
if h := r.Header.Get(SessionTokenHeader); h != "" {
t.Errorf("request had Session Token header set!")
}
if h := r.Header.Get(MasterKeyHeader); h != "master_key" {
t.Errorf("request did not have Master Key header set!")
}
fmt.Fprintf(w, `{"createdAt":"2014-12-19T18:05:57Z","objectId":"abcDEF"}`)
})
defer teardownTestServer()
u := TestUser{
FirstName: "Kyle",
LastName: "M",
Email: "[email protected]",
FCount: 11,
}
err := Create(&u, true)
if err != nil {
t.Errorf("unexpected error on create: %v\n", err)
}
}
type TestTypeOmitEmpty struct {
StrField string
OEStrField string `parse:",omitempty"`
BoolField bool
OEBoolField bool `parse:",omitempty"`
IntField int
OEIntField int `parse:",omitempty"`
ArrField []string
OEArrField []string `parse:",omitempty"`
EmptyArrField []string
OEEmptyArrField []string `parse:",omitempty"`
MapField map[string]interface{}
OEMapField map[string]interface{} `parse:",omitempty"`
EmptyMapField map[string]interface{}
OEEmptyMapField map[string]interface{} `parse:",omitempty"`
PtrField *User
OEPtrField *User `parse:",omitempty"`
}
func TestCreateOmitEmpty(t *testing.T) {
cr := &createT{
v: &TestTypeOmitEmpty{
EmptyArrField: []string{},
OEEmptyArrField: []string{},
EmptyMapField: map[string]interface{}{},
OEEmptyMapField: map[string]interface{}{},
},
}
e := map[string]interface{}{
"strField": "",
"boolField": false,
"intField": 0,
"arrField": nil,
"emptyArrField": []string{},
"mapField": nil,
"emptyMapField": map[string]interface{}{},
"ptrField": nil,
}
expected := map[string]interface{}{}
eb, _ := json.Marshal(e)
_ = json.Unmarshal(eb, &expected)
actual := map[string]interface{}{}
b, err := cr.body()
if err != nil {
t.Errorf("unexpected error generating payload: %v\n", err)
t.FailNow()
}
err = json.Unmarshal([]byte(b), &actual)
if err != nil {
t.Errorf("unexpected error unmarshaling payload: %v\n", err)
t.FailNow()
}
if !reflect.DeepEqual(actual, expected) {
t.Errorf("payload different from expected. expected:\n%s\n\ngot:\n%s\n", eb, b)
}
}