-
Notifications
You must be signed in to change notification settings - Fork 16
/
proteus_function_test.go
487 lines (466 loc) · 12.6 KB
/
proteus_function_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package proteus
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/jonbodner/proteus/logger"
)
func TestBuilder_BuildFunctionErrors(t *testing.T) {
b := NewBuilder(Postgres)
db := setupDbPostgres()
defer db.Close()
ctx := context.Background()
var f func()
var f2 func(context.Context, ContextExecutor)
data := []struct {
name string
f interface{}
query string
params []string
errMsg string
}{
{
name: "not a pointer",
f: func() {},
query: "",
params: nil,
errMsg: "not a pointer",
},
{
name: "not a func",
f: &struct{}{},
query: "",
params: nil,
errMsg: "not a pointer to func",
},
{
name: "not a valid func",
f: &f,
query: "",
params: nil,
errMsg: "need to supply an Executor or Querier parameter",
},
{
name: "not a valid query",
f: &f2,
query: "q:nope",
params: nil,
errMsg: "no query found for name nope",
},
{
name: "not a valid params",
f: &f2,
query: "SELECT * FROM PERSON WHERE id = :id:",
params: nil,
errMsg: "query Parameter id cannot be found in the incoming parameters",
},
}
for _, v := range data {
t.Run(v.name, func(t *testing.T) {
err := b.BuildFunction(ctx, v.f, v.query, v.params)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != v.errMsg {
t.Errorf("Expected error `%s`, got `%s`", v.errMsg, errMsg)
}
})
}
}
func TestBuilder_BuildFunction(t *testing.T) {
b := NewBuilder(Postgres)
db := setupDbPostgres()
defer db.Close()
ctx := context.Background()
var f func(c context.Context, e ContextExecutor, name string, age int) (int64, error)
err := b.BuildFunction(ctx, &f, "INSERT INTO PERSON(name, age) VALUES(:name:, :age:)", []string{"name", "age"})
if err != nil {
t.Fatalf("build function failed: %v", err)
}
rows, err := f(ctx, db, "Fred", 20)
if err != nil {
t.Fatalf("create failed: %v", err)
}
if rows != 1 {
t.Error("Expected 1 row modified, got ", rows)
}
err = b.BuildFunction(ctx, &f, "INSERT INTO PERSON(name, age) VALUES(:$1:, :$2:)", nil)
if err != nil {
t.Fatalf("build function failed: %v", err)
}
rows, err = f(ctx, db, "Fred", 20)
if err != nil {
t.Fatalf("create failed: %v", err)
}
if rows != 1 {
t.Error("Expected 1 row modified, got ", rows)
}
var g func(c context.Context, q ContextQuerier, id int) (*Person, error)
err = b.BuildFunction(ctx, &g, "SELECT * FROM PERSON WHERE id = :id:", []string{"id"})
if err != nil {
t.Fatalf("build function 2 failed: %v", err)
}
p, err := g(ctx, db, 1)
if err != nil {
t.Fatalf("get failed: %v", err)
}
if p == nil || *p != (Person{1, "Fred", 20}) {
t.Error("Unexpected output: ", p)
}
var lines []string
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
lines = append(lines, fmt.Sprintln(vals))
return nil
}))
SetLogLevel(logger.DEBUG)
err = b.BuildFunction(ctx, &g, "SELECT * FROM PERSON WHERE id = :id:", []string{"id"})
if err != nil {
t.Fatalf("build function 2a failed: %v", err)
}
if len(lines) != 3 {
t.Errorf("Expected %d lines, got %d: %#v", 3, len(lines), lines)
}
}
func TestBuilder_Execute(t *testing.T) {
b := NewBuilder(Postgres)
db := setupDbPostgres()
defer db.Close()
ctx := context.Background()
var lines []string
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
lines = append(lines, fmt.Sprintln(vals))
return nil
}))
data := []struct {
name string
ctx context.Context
logLevel logger.Level
query string
params map[string]interface{}
rows int64
errMsg string
lineCount int
}{
{
name: "insert",
query: "INSERT INTO PERSON(name, age) VALUES(:name:, :age:)",
params: map[string]interface{}{"name": "Fred", "age": 20},
rows: 1,
},
{
name: "insert struct",
query: "INSERT INTO PERSON(name, age) VALUES(:p.Name:, :p.Age:)",
params: map[string]interface{}{"p": Person{Name: "Bob", Age: 50}},
rows: 1,
},
{
name: "insert map",
query: "INSERT INTO PERSON(name, age) VALUES(:p.name:, :p.age:)",
params: map[string]interface{}{"p": map[string]interface{}{"name": "Julia", "age": 32}},
rows: 1,
},
{
name: "insert unnamed",
query: "INSERT INTO PERSON(name, age) VALUES(:$1:, :$2:)",
params: map[string]interface{}{"$1": "Pat", "$2": 37},
rows: 1,
},
{
name: "bad query",
query: "INSERT INTO PERSON(name, age VALUES(:name:, :age:)",
params: map[string]interface{}{"name": "Fred", "age": 20},
rows: 0,
errMsg: "pq: syntax error at or near \"VALUES\"",
},
{
name: "bad args",
query: "INSERT INTO PERSON(name, age) VALUES(:name stuff:, :age:)",
params: map[string]interface{}{"name": "Fred", "age": 20},
rows: 0,
errMsg: ". missing between parts of an identifier: name stuff",
},
{
name: "no such query",
query: "q:nope",
params: map[string]interface{}{"id": 1},
rows: 0,
errMsg: "no query found for name nope",
},
{
name: "logging context",
ctx: logger.WithLevel(ctx, logger.DEBUG),
query: "INSERT INTO PERSON(name, age) VALUES(:name:, :age:)",
params: map[string]interface{}{"name": "Fred", "age": 20},
rows: 1,
lineCount: 7,
},
{
name: "logging default",
logLevel: logger.DEBUG,
query: "INSERT INTO PERSON(name, age) VALUES(:name:, :age:)",
params: map[string]interface{}{"name": "Fred", "age": 20},
rows: 1,
lineCount: 7,
},
}
for _, v := range data {
t.Run(v.name, func(t *testing.T) {
lines = nil
SetLogLevel(v.logLevel)
c := ctx
if v.ctx != nil {
c = v.ctx
}
rows, err := b.Exec(c, db, v.query, v.params)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != v.errMsg {
t.Errorf("Expected error `%s`, got `%s`", v.errMsg, errMsg)
}
if rows != v.rows {
t.Error("Unexpected # rows: ", rows)
}
if v.lineCount != len(lines) {
t.Errorf("Expected %d lines of debug, got %d: %#v", v.lineCount, len(lines), lines)
}
lines = nil
result, err := b.ExecResult(c, db, v.query, v.params)
errMsg = ""
if err != nil {
errMsg = err.Error()
}
if errMsg != v.errMsg {
t.Errorf("Expected error `%s`, got `%s`", v.errMsg, errMsg)
}
if err == nil {
rowsAffected, err := result.RowsAffected()
if err != nil {
t.Fatal("unexpected error", err)
}
if rowsAffected != v.rows {
t.Error("Unexpected # rows: ", rows)
}
if v.lineCount != len(lines) {
t.Errorf("Expected %d lines of debug, got %d: %#v", v.lineCount, len(lines), lines)
}
}
})
}
}
func TestBuilder_Query(t *testing.T) {
b := NewBuilder(Postgres)
db := setupDbPostgres()
defer db.Close()
ctx := context.Background()
// set up some data
_, err := b.Exec(ctx, db,
"INSERT INTO PERSON(name, age) VALUES(:name1:, :age1:),(:name2:, :age2:),(:name3:, :age3:),(:name4:, :age4:)",
map[string]interface{}{
"name1": "Fred", "age1": 20,
"name2": "Bob", "age2": 50,
"name3": "Julia", "age3": 32,
"name4": "Pat", "age4": 37,
})
if err != nil {
panic(err)
}
type BadPerson struct {
Id int `prof:"name"`
Name string `prof:"id"`
Age string `prof:"age"`
}
var lines []string
logger.Config(logger.LoggerFunc(func(vals ...interface{}) error {
lines = append(lines, fmt.Sprintln(vals))
return nil
}))
data := []struct {
name string
ctx context.Context
logLevel logger.Level
query string
params map[string]interface{}
out interface{}
expected interface{}
errMsg string
lineCount int
}{
{
name: "person by id",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: &Person{},
expected: &Person{Id: 1, Name: "Fred", Age: 20},
},
{
name: "all people",
query: "SELECT * FROM PERSON",
params: nil,
out: &[]Person{},
expected: &[]Person{
{Id: 1, Name: "Fred", Age: 20},
{Id: 2, Name: "Bob", Age: 50},
{Id: 3, Name: "Julia", Age: 32},
{Id: 4, Name: "Pat", Age: 37},
},
},
{
name: "all people in ages",
query: "SELECT * from PERSON WHERE name=:name: and age in (:ages:) and id = :id:",
params: map[string]interface{}{"id": 1, "ages": []int{20, 32}, "name": "Fred"},
out: &[]Person{},
expected: &[]Person{
{Id: 1, Name: "Fred", Age: 20},
},
},
{
name: "all people in ages unnamed",
query: "SELECT * from PERSON WHERE name=:$1: and age in (:$2:) and id = :$3:",
params: map[string]interface{}{"$1": "Fred", "$2": []int{20, 32}, "$3": 1},
out: &[]Person{},
expected: &[]Person{
{Id: 1, Name: "Fred", Age: 20},
},
},
// or really non-typed
{
name: "id untyped out",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: &map[string]interface{}{},
expected: &map[string]interface{}{
"id": int64(1), "name": "Fred", "age": int64(20),
},
},
{
name: "slice age untyped out",
query: "SELECT * FROM PERSON where age > :$1: and age < :$2:",
params: map[string]interface{}{"$1": 20, "$2": 50},
out: &[]map[string]interface{}{},
expected: &[]map[string]interface{}{
{"id": int64(3), "name": "Julia", "age": int64(32)},
{"id": int64(4), "name": "Pat", "age": int64(37)},
},
},
{
name: "id from struct",
query: "SELECT * FROM PERSON WHERE id = :p.Id:",
params: map[string]interface{}{"p": Person{Id: 1}},
out: &map[string]interface{}{},
expected: &map[string]interface{}{
"id": int64(1), "name": "Fred", "age": int64(20),
},
},
{
name: "id from map",
query: "SELECT * FROM PERSON WHERE id = :p.id:",
params: map[string]interface{}{"p": map[string]interface{}{"id": 1}},
out: &map[string]interface{}{},
expected: &map[string]interface{}{
"id": int64(1), "name": "Fred", "age": int64(20),
},
},
{
name: "nothing returned",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 100},
out: &Person{},
expected: &Person{},
},
{
name: "nothing returned logging context",
ctx: logger.WithLevel(ctx, logger.DEBUG),
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 100},
out: &Person{},
expected: &Person{},
lineCount: 4,
},
{
name: "nothing returned logging default",
logLevel: logger.DEBUG,
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 100},
out: &Person{},
expected: &Person{},
lineCount: 4,
},
// errors
{
name: "not a pointer",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: Person{},
expected: Person{},
errMsg: "not a pointer",
},
{
name: "no such query",
query: "q:nope",
params: map[string]interface{}{"id": 1},
out: &Person{},
expected: &Person{},
errMsg: "no query found for name nope",
},
{
name: "bad query",
query: "SELECT * FROM PER SON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: &Person{},
expected: &Person{},
errMsg: `pq: relation "per" does not exist`,
},
{
name: "bad target",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: &map[int]interface{}{},
expected: &map[int]interface{}{},
errMsg: `only maps with string keys are supported`,
},
{
name: "bad mapping",
query: "SELECT * FROM PERSON WHERE id = :id:",
params: map[string]interface{}{"id": 1},
out: &BadPerson{},
expected: &BadPerson{},
errMsg: "unable to assign value Fred of type string to struct field Id of type int",
},
{
name: "bad in clause",
query: "SELECT * FROM PERSON WHERE id in (:p.id:)",
params: map[string]interface{}{"p": map[string]interface{}{"foo": "bar"}},
out: &Person{},
expected: &Person{},
errMsg: "cannot extract value; no such map key id",
},
}
for _, v := range data {
t.Run(v.name, func(t *testing.T) {
lines = nil
SetLogLevel(v.logLevel)
c := ctx
if v.ctx != nil {
c = v.ctx
}
err := b.Query(c, db, v.query, v.params, v.out)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != v.errMsg {
t.Errorf("Expected error `%s`, got `%s`", v.errMsg, errMsg)
}
if diff := cmp.Diff(v.out, v.expected); diff != "" {
t.Errorf(diff)
}
if v.lineCount != len(lines) {
t.Errorf("Expected %d lines of debug, got %d: %#v", v.lineCount, len(lines), lines)
}
})
}
}