forked from src-d/go-kallax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.go
119 lines (96 loc) · 3.19 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
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
package kallax
import (
"testing"
"unsafe"
"github.com/stretchr/testify/suite"
)
func TestBaseQuery(t *testing.T) {
suite.Run(t, new(QuerySuite))
}
type QuerySuite struct {
suite.Suite
q *BaseQuery
}
func (s *QuerySuite) SetupTest() {
s.q = NewBaseQuery(ModelSchema)
}
func (s *QuerySuite) TestSelect() {
s.q.Select(f("a"), f("b"), f("c"))
s.Equal(columnSet{f("a"), f("b"), f("c")}, s.q.columns)
}
func (s *QuerySuite) TestSelectNot() {
s.q.SelectNot(f("a"), f("b"), f("c"))
s.Equal(columnSet{f("a"), f("b"), f("c")}, s.q.excludedColumns)
}
func (s *QuerySuite) TestSelectNotSelectSelectNot() {
s.q.SelectNot(f("a"), f("b"))
s.q.Select(f("a"), f("c"))
s.q.SelectNot(f("a"))
s.Equal([]SchemaField{f("c")}, s.q.selectedColumns())
}
func (s *QuerySuite) TestSelectSelectNot() {
s.q.Select(f("a"), f("c"))
s.q.SelectNot(f("a"))
s.Equal([]SchemaField{f("c")}, s.q.selectedColumns())
}
func (s *QuerySuite) TestCopy() {
s.q.Select(f("a"), f("b"), f("c"))
s.q.SelectNot(f("foo"))
s.q.BatchSize(30)
s.q.Limit(2)
s.q.Offset(30)
copy := s.q.Copy()
s.Equal(s.q, copy)
s.NotEqual(unsafe.Pointer(s.q), unsafe.Pointer(copy))
}
func (s *QuerySuite) TestSelectedColumns() {
s.q.Select(f("a"), f("b"), f("c"))
s.q.SelectNot(f("b"))
s.Equal([]SchemaField{f("a"), f("c")}, s.q.selectedColumns())
}
func (s *QuerySuite) TestOrder() {
s.q.Select(f("foo"))
s.q.Order(Asc(f("bar")))
s.q.Order(Desc(f("baz")))
s.assertSql("SELECT __model.foo FROM model __model ORDER BY __model.bar ASC, __model.baz DESC")
}
func (s *QuerySuite) TestWhere() {
s.q.Select(f("foo"))
s.q.Where(Eq(f("foo"), 5))
s.q.Where(Eq(f("bar"), "baz"))
s.assertSql("SELECT __model.foo FROM model __model WHERE __model.foo = $1 AND __model.bar = $2")
}
func (s *QuerySuite) TestString() {
s.q.Select(f("foo"))
s.Equal("SELECT __model.foo FROM model __model", s.q.String())
}
func (s *QuerySuite) TestToSql() {
s.q.Select(f("foo"))
s.q.Where(Eq(f("foo"), 5))
s.q.Where(Eq(f("bar"), "baz"))
sql, args, err := s.q.ToSql()
s.Equal("SELECT __model.foo FROM model __model WHERE __model.foo = $1 AND __model.bar = $2", sql)
s.Equal([]interface{}{5, "baz"}, args)
s.Equal(err, nil)
}
func (s *QuerySuite) TestAddRelation() {
s.Nil(s.q.AddRelation(RelSchema, "rel", OneToOne, nil))
s.Equal("SELECT __model.id, __model.name, __model.email, __model.age, __rel_rel.id, __rel_rel.model_id, __rel_rel.foo FROM model __model LEFT JOIN rel __rel_rel ON (__rel_rel.model_id = __model.id)", s.q.String())
}
func (s *QuerySuite) TestAddRelation_Inverse() {
s.Nil(s.q.AddRelation(RelSchema, "rel_inv", OneToOne, nil))
s.Equal("SELECT __model.id, __model.name, __model.email, __model.age, __rel_rel_inv.id, __rel_rel_inv.model_id, __rel_rel_inv.foo FROM model __model LEFT JOIN rel __rel_rel_inv ON (__rel_rel_inv.id = __model.model_id)", s.q.String())
}
func (s *QuerySuite) TestAddRelation_ManyToMany() {
err := s.q.AddRelation(RelSchema, "rel", ManyToMany, nil)
s.Equal(ErrManyToManyNotSupported, err)
}
func (s *QuerySuite) TestAddRelation_FKNotFound() {
s.Error(s.q.AddRelation(RelSchema, "fooo", OneToOne, nil))
}
func (s *QuerySuite) assertSql(sql string) {
_, builder := s.q.compile()
result, _, err := builder.ToSql()
s.Nil(err)
s.Equal(sql, result)
}