forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_test.go
68 lines (53 loc) · 1.69 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
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))
}