-
Notifications
You must be signed in to change notification settings - Fork 3
/
query_test.go
77 lines (67 loc) · 1.72 KB
/
query_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
package yago_test
import (
"testing"
"github.com/orus-io/yago"
"github.com/orus-io/qb"
"github.com/stretchr/testify/assert"
)
type querySQLTests struct {
expect string
q yago.Query
}
func makeQuerySQLTests(db *yago.DB, model FixtureModel) []querySQLTests {
return []querySQLTests{
{
"SELECT X\nFROM person_struct\nWHERE (Y AND Z)",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
Filter(qb.SQLText("Y")).
Filter(qb.SQLText("Z")),
},
{
"SELECT X\nFROM person_struct\nWHERE (Y AND Z)",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
Where(qb.SQLText("Y"), qb.SQLText("Z")),
},
{
"SELECT X\nFROM person_struct\nORDER BY last_name ASC",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
OrderBy(model.PersonStruct.LastName),
},
{
"SELECT X\nFROM person_struct\nLIMIT 5",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
Limit(5),
},
{
"SELECT X\nFROM person_struct\nOFFSET 10",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
Offset(10),
},
{
"SELECT X\nFROM person_struct\nORDER BY last_name ASC\nLIMIT 5 OFFSET 10",
db.Query(model.PersonStruct).Select(qb.SQLText("X")).
OrderBy(model.PersonStruct.LastName).
LimitOffset(5, 10),
},
}
}
func TestGet(t *testing.T) {
db, model, cleanup := initModel(t)
defer cleanup()
p := PersonStruct{FirstName: "John"}
assert.Nil(t, db.Insert(&p))
t.Log(p.ID)
var p1 PersonStruct
assert.Nil(t, db.Query(model.PersonStruct).Get(&p1, p.ID))
assert.Equal(t, p.FirstName, p1.FirstName)
}
func TestQuerySQL(t *testing.T) {
db, model, cleanup := initModel(t)
defer cleanup()
for _, tt := range makeQuerySQLTests(db, model) {
assert.Equal(t,
tt.expect,
asSQL(tt.q),
)
}
}