This repository has been archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_test.go
273 lines (230 loc) · 6.4 KB
/
utils_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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package controllers
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"testing"
"net/http/httptest"
"bytes"
"encoding/json"
)
func validate(t *testing.T, w *httptest.ResponseRecorder, r *http.Request, h http.HandlerFunc, where string) {
fmt.Println("Test:", where)
h(w, r)
if w.Code != 200 {
t.Errorf("expected 200, got: %d", w.Code)
}
_, err := ioutil.ReadAll(w.Body)
if err != nil {
t.Error("error on ioutil ReadAll", err)
}
}
func doValidGetRequest(t *testing.T, url string, where string) {
fmt.Println("Test:", where)
resp, err := http.Get(url)
if err != nil {
t.Error("expected no errors in Get")
}
if resp.StatusCode != 200 {
t.Errorf("expected 200, got %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("expected no errors in ioutil ReadAll")
}
}
func doValidPostRequest(t *testing.T, url string, r map[string]interface{}, where string) {
fmt.Println("Test:", where)
byt, err := json.Marshal(r)
if err != nil {
t.Error("expected no errors in json marshal, but was!")
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(byt))
if err != nil {
t.Error("expected no errors in Post")
}
if resp.StatusCode != 200 {
t.Errorf("expected 200, got %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("expected no errors in ioutil ReadAll")
}
}
func doValidDeleteRequest(t *testing.T, url string, where string) {
fmt.Println("Test:", where)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
t.Error("expected no errors in NewRequest, but was!")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Error("expected no errors in Do Request, but was!")
}
if resp.StatusCode != 200 {
t.Errorf("expected 200, got %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("expected no errors in ioutil ReadAll")
}
}
func doValidPutRequest(t *testing.T, url string, r map[string]interface{}, where string) {
fmt.Println("Test:", where)
byt, err := json.Marshal(r)
if err != nil {
t.Error("expected no errors in json marshal, but was!")
}
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(byt))
if err != nil {
t.Error("expected no errors in PUT")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Error("expected no errors in Do Request, but was!")
}
if resp.StatusCode != 200 {
t.Errorf("expected 200, got %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("expected no errors in ioutil ReadAll")
}
}
func doValidPatchRequest(t *testing.T, url string, r map[string]interface{}, where string) {
fmt.Println("Test:", where)
byt, err := json.Marshal(r)
if err != nil {
t.Error("expected no errors in json marshal, but was!")
}
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(byt))
if err != nil {
t.Error("expected no errors in PATCH")
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Error("expected no errors in Do Request, but was!")
}
if resp.StatusCode != 200 {
t.Errorf("expected 200, got %d", resp.StatusCode)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("expected no errors in ioutil ReadAll")
}
}
func doRequest(t *testing.T, url string, r interface{}, method string, expectedStatus int, where string, expectedBody ...string) {
fmt.Println("Test:", where)
var byt []byte
var err error
if r != nil {
byt, err = json.Marshal(r)
if err != nil {
t.Error("error on json marshal", err)
}
}
req, err := http.NewRequest(method, url, bytes.NewBuffer(byt))
if err != nil {
t.Error("error on New Request", err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
t.Error("error on Do Request", err)
}
if resp.StatusCode != expectedStatus {
t.Errorf("expected %d, got: %d", expectedStatus, resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error("error on ioutil ReadAll", err)
}
if len(expectedBody) > 0 {
if !containsStringInSlice(expectedBody, string(body)) {
t.Errorf("expected %q, got: %q", expectedBody, string(body))
}
}
}
func containsStringInSlice(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func createMockScripts(base string) {
log.Println("Create scripts on", base)
err := os.Mkdir(fmt.Sprint(base, "/fulltable"), 0777)
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/get_all.read.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/funcs.read.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/write_all.write.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/create_table.write.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/patch_all.update.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/put_all.update.sql"))
if err != nil {
log.Println(err)
}
_, err = os.Create(fmt.Sprint(base, "/fulltable/delete_all.delete.sql"))
if err != nil {
log.Println(err)
}
}
func writeMockScripts(base string) {
base = fmt.Sprint(base, "/fulltable/")
log.Println("Write scripts on", base)
write := func(sql, fileName string) {
var file, err = os.OpenFile(fmt.Sprint(base, fileName), os.O_RDWR, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// write some text to file
_, err = file.WriteString(sql)
if err != nil {
log.Fatal(err)
}
// save changes
err = file.Sync()
if err != nil {
log.Fatal(err)
}
}
write("SELECT * FROM test7 WHERE name = '{{defaultOrValue \"field1\" \"gopher\"}}'", "funcs.read.sql")
write("SELECT * FROM test7 WHERE name = '{{.field1}}'", "get_all.read.sql")
write("INSERT INTO test7 (name, surname) VALUES ('{{.field1}}', '{{.field2}}')", "write_all.write.sql")
write("CREATE TABLE {{.field1}};", "create_table.write.sql")
write("UPDATE test7 SET name = '{{.field1}}' WHERE surname = '{{.field2}}'", "patch_all.update.sql")
write("UPDATE test7 SET surname = '{{.field1}}' WHERE name = '{{.field2}}'", "put_all.update.sql")
write("DELETE FROM test7 WHERE name = '{{.field1}}'", "delete_all.delete.sql")
}
func removeMockScripts(base string) {
log.Println("Remove scripts on", base)
err := os.RemoveAll(base)
if err != nil {
log.Println(err)
}
}