-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder_test.go
80 lines (75 loc) · 2.6 KB
/
builder_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
package drivers
import (
"github.com/gohouse/gorose"
"testing"
)
type User struct {
Id int64 `db:"id,pk"`
Name string `db:"name"`
}
var dbg = gorose.Open(nil)
func db() gorose.Database {
return dbg.NewDatabase()
}
func TestDatabase_ToSqlTo(t *testing.T) {
var user = User{Id: 1}
prepare, values, err := db().ToSqlTo(&user)
assertsError(t, err)
var expect = "SELECT `id`, `name` FROM `User` WHERE `id` = ? LIMIT ?"
assertsEqual(t, expect, prepare)
var expectValues = []int{1, 1}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSqlToSlice(t *testing.T) {
var user []User
prepare, values, err := db().Where("id", ">", 1).OrderBy("id").Limit(10).Page(2).ToSqlTo(&user)
assertsError(t, err)
var expect = "SELECT `id`, `name` FROM `User` WHERE `id` > ? ORDER BY `id` LIMIT ? OFFSET ?"
assertsEqual(t, expect, prepare)
var expectValues = []int{1, 10, 10}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSql(t *testing.T) {
prepare, values, err := db().Table("users").Select("b").Where("c", 1).GroupBy("a").Having("a", 1).OrderBy("id").Limit(10).Page(2).ToSql()
assertsError(t, err)
var expect = "SELECT `b` FROM `users` WHERE `c` = ? GROUP BY `a` HAVING `a` = ? ORDER BY `id` LIMIT ? OFFSET ?"
assertsEqual(t, expect, prepare)
var expectValues = []int{1, 10, 10, 1}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSqlInsert(t *testing.T) {
var user = User{Name: "john"}
prepare, values, err := db().ToSqlInsert(&user, "", nil)
assertsError(t, err)
var expect = "INSERT INTO `User` (`name`) VALUES (?)"
assertsEqual(t, expect, prepare)
var expectValues = []string{"john"}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSqlInserts(t *testing.T) {
var user = []User{{Name: "John"}, {Name: "Alice"}}
prepare, values, err := db().ToSqlInsert(&user, "", nil)
assertsError(t, err)
var expect = "INSERT INTO `User` (`name`) VALUES (?),(?)"
assertsEqual(t, expect, prepare)
var expectValues = []string{"John", "Alice"}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSqlUpdate(t *testing.T) {
var user = User{Id: 1, Name: "john"}
prepare, values, err := db().ToSqlUpdate(&user)
assertsError(t, err)
var expect = "UPDATE `User` SET `name` = ? WHERE `id` = ?"
assertsEqual(t, expect, prepare)
var expectValues = []any{"john", 1}
assertsEqual(t, expectValues, values)
}
func TestDatabase_ToSqlDelete(t *testing.T) {
var user = User{Id: 1}
prepare, values, err := db().ToSqlDelete(&user)
assertsError(t, err)
var expect = "DELETE FROM `User` WHERE `id` = ?"
assertsEqual(t, expect, prepare)
var expectValues = []any{1}
assertsEqual(t, expectValues, values)
}