-
Notifications
You must be signed in to change notification settings - Fork 470
/
statement_test.go
81 lines (62 loc) · 2.02 KB
/
statement_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
package squirrel
import (
"database/sql"
"testing"
"github.com/lann/builder"
"github.com/stretchr/testify/assert"
)
func TestStatementBuilder(t *testing.T) {
db := &DBStub{}
sb := StatementBuilder.RunWith(db)
sb.Select("test").Exec()
assert.Equal(t, "SELECT test", db.LastExecSql)
}
func TestStatementBuilderPlaceholderFormat(t *testing.T) {
db := &DBStub{}
sb := StatementBuilder.RunWith(db).PlaceholderFormat(Dollar)
sb.Select("test").Where("x = ?").Exec()
assert.Equal(t, "SELECT test WHERE x = $1", db.LastExecSql)
}
func TestRunWithDB(t *testing.T) {
db := &sql.DB{}
assert.NotPanics(t, func() {
builder.GetStruct(Select().RunWith(db))
builder.GetStruct(Insert("t").RunWith(db))
builder.GetStruct(Update("t").RunWith(db))
builder.GetStruct(Delete("t").RunWith(db))
}, "RunWith(*sql.DB) should not panic")
}
func TestRunWithTx(t *testing.T) {
tx := &sql.Tx{}
assert.NotPanics(t, func() {
builder.GetStruct(Select().RunWith(tx))
builder.GetStruct(Insert("t").RunWith(tx))
builder.GetStruct(Update("t").RunWith(tx))
builder.GetStruct(Delete("t").RunWith(tx))
}, "RunWith(*sql.Tx) should not panic")
}
type fakeBaseRunner struct{}
func (fakeBaseRunner) Exec(query string, args ...interface{}) (sql.Result, error) {
return nil, nil
}
func (fakeBaseRunner) Query(query string, args ...interface{}) (*sql.Rows, error) {
return nil, nil
}
func TestRunWithBaseRunner(t *testing.T) {
sb := StatementBuilder.RunWith(fakeBaseRunner{})
_, err := sb.Select("test").Exec()
assert.NoError(t, err)
}
func TestRunWithBaseRunnerQueryRowError(t *testing.T) {
sb := StatementBuilder.RunWith(fakeBaseRunner{})
assert.Error(t, RunnerNotQueryRunner, sb.Select("test").QueryRow().Scan(nil))
}
func TestStatementBuilderWhere(t *testing.T) {
sb := StatementBuilder.Where("x = ?", 1)
sql, args, err := sb.Select("test").Where("y = ?", 2).ToSql()
assert.NoError(t, err)
expectedSql := "SELECT test WHERE x = ? AND y = ?"
assert.Equal(t, expectedSql, sql)
expectedArgs := []interface{}{1, 2}
assert.Equal(t, expectedArgs, args)
}