forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 38
/
sqrl_test.go
123 lines (97 loc) · 2.52 KB
/
sqrl_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
package sqrl
import (
"context"
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
type DBStub struct {
res sql.Result
err error
LastPrepareSql string
PrepareCount int
LastExecSql string
LastExecArgs []interface{}
LastQuerySql string
LastQueryArgs []interface{}
LastQueryRowSql string
LastQueryRowArgs []interface{}
}
func (s *DBStub) Prepare(query string) (*sql.Stmt, error) {
s.LastPrepareSql = query
s.PrepareCount++
return nil, nil
}
func (s *DBStub) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) {
s.LastPrepareSql = query
s.PrepareCount++
return nil, nil
}
func (s *DBStub) Exec(query string, args ...interface{}) (sql.Result, error) {
s.LastExecSql = query
s.LastExecArgs = args
return s.res, s.err
}
func (s *DBStub) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
s.LastExecSql = query
s.LastExecArgs = args
return s.res, s.err
}
func (s *DBStub) Query(query string, args ...interface{}) (*sql.Rows, error) {
s.LastQuerySql = query
s.LastQueryArgs = args
return nil, nil
}
func (s *DBStub) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
s.LastQuerySql = query
s.LastQueryArgs = args
return nil, nil
}
func (s *DBStub) QueryRow(query string, args ...interface{}) RowScanner {
s.LastQueryRowSql = query
s.LastQueryRowArgs = args
return &Row{RowScanner: &RowStub{}}
}
func (s *DBStub) QueryRowContext(ctx context.Context, query string, args ...interface{}) RowScanner {
s.LastQueryRowSql = query
s.LastQueryRowArgs = args
return &Row{RowScanner: &RowStub{}}
}
type resultStub struct {
rowsAffected int64
lastInsertId int64
err error
}
func (r *resultStub) RowsAffected() (int64, error) {
return r.rowsAffected, r.err
}
func (r *resultStub) LastInsertId() (int64, error) {
return r.lastInsertId, r.err
}
var sqlizer = Select("test")
var sqlStr = "SELECT test"
func TestExecWith(t *testing.T) {
db := &DBStub{}
ExecWith(db, sqlizer)
assert.Equal(t, sqlStr, db.LastExecSql)
}
func TestQueryWith(t *testing.T) {
db := &DBStub{}
QueryWith(db, sqlizer)
assert.Equal(t, sqlStr, db.LastQuerySql)
}
func TestQueryRowWith(t *testing.T) {
db := &DBStub{}
QueryRowWith(db, sqlizer)
assert.Equal(t, sqlStr, db.LastQueryRowSql)
}
func TestWithToSqlErr(t *testing.T) {
db := &DBStub{}
sqlizer := Select()
_, err := ExecWith(db, sqlizer)
assert.Error(t, err)
_, err = QueryWith(db, sqlizer)
assert.Error(t, err)
err = QueryRowWith(db, sqlizer).Scan()
assert.Error(t, err)
}