-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathonedb_test.go
119 lines (101 loc) · 2.82 KB
/
onedb_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
package onedb
import (
"errors"
"testing"
)
func TestQueryJson(t *testing.T) {
rows := NewRowsScanner([]SimpleData{{1, "hello"}})
db := &mockBackend{Rows: rows}
json, err := QueryJSON(db, "select * from TestTable")
if json != "[{\"IntVal\":1,\"StringVal\":\"hello\"}]" {
t.Error("expected different json back. Actual:", json, err)
}
if err != nil {
t.Error("didn't expect error")
}
db = &mockBackend{QueryErr: errors.New("fail")}
_, err = QueryJSON(db, "select * from TestTable")
if err == nil {
t.Error("expected error")
}
}
func TestQueryJsonRow(t *testing.T) {
rows := NewRowsScanner([]SimpleData{{1, "hello"}})
db := &mockBackend{Rows: rows}
json, err := QueryJSONRow(db, "select * from TestTable")
if json != "{\"IntVal\":1,\"StringVal\":\"hello\"}" {
t.Error("expected different json back. Actual:", json, err)
}
if err != nil {
t.Error("didn't expect error")
}
db = &mockBackend{QueryErr: errors.New("fail")}
_, err = QueryJSONRow(db, "select * from TestTable")
if err == nil {
t.Error("expected error")
}
}
func TestQueryStruct(t *testing.T) {
rows := NewRowsScanner([]SimpleData{{1, "hello"}})
db := &mockBackend{Rows: rows}
data := []SimpleData{}
// wrong receiver type
err := QueryStruct(db, data, "query")
if err == nil {
t.Error("expected error", err)
}
// success
err = QueryStruct(db, &data, "query")
if err != nil || len(data) != 1 || data[0].IntVal != 1 || data[0].StringVal != "hello" {
t.Error("expected success")
}
// query error
db = &mockBackend{QueryErr: errors.New("fail")}
err = QueryStruct(db, &data, "query")
if err == nil {
t.Error("expected error", err)
}
}
func TestQueryStructRow(t *testing.T) {
rows := NewRowsScanner([]SimpleData{{1, "hello"}})
db := &mockBackend{Rows: rows}
data := SimpleData{}
// wrong receiver type
err := QueryStructRow(db, data, "query")
if err == nil {
t.Error("expected error", err)
}
// success
err = QueryStructRow(db, &data, "query")
if err != nil || data.IntVal != 1 || data.StringVal != "hello" {
t.Error("expected success")
}
// query error
db = &mockBackend{QueryErr: errors.New("fail")}
err = QueryStructRow(db, &data, "query")
if err == nil {
t.Error("expected error", err)
}
}
func TestNewQuery(t *testing.T) {
q := NewQuery("query", "arg1", "arg2")
if q == nil || q.Query != "query" || len(q.Args) != 2 || q.Args[0] != "arg1" || q.Args[1] != "arg2" {
t.Error("expected success")
}
}
/******************** MOCKS ************************/
type mockBackend struct {
Rows RowsScanner
Row Scanner
ExecErr error
QueryErr error
}
func (b *mockBackend) Query(query string, args ...interface{}) (RowsScanner, error) {
return b.Rows, b.QueryErr
}
func (b *mockBackend) QueryRow(query string, args ...interface{}) Scanner {
if b.QueryErr != nil {
return &mockScanner{ScanErr: b.QueryErr}
}
return b.Row
}