-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_struct_test.go
166 lines (164 loc) · 4.16 KB
/
query_struct_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package dal
import (
"github.com/dal-go/dalgo/constant"
"github.com/stretchr/testify/assert"
"testing"
)
func TestSelect(t *testing.T) {
tests := []struct {
name string
q theQuery
want string
}{
{
name: "select_1",
q: theQuery{
columns: []Column{
{Expression: constant.Int(1)},
},
},
want: "SELECT 1",
},
{
name: "select_'abc'_AS_first_col",
q: theQuery{
columns: []Column{
{Expression: constant.Str("abc"), Alias: "first_col"},
},
},
want: "SELECT 'abc' AS first_col",
},
{
name: "select_*_from_User",
q: theQuery{
from: &CollectionRef{Name: "User"},
},
want: "SELECT * FROM [User]",
},
{
name: "select_top_10_*_from_User",
q: theQuery{
from: &CollectionRef{Name: "User"},
limit: 10,
},
want: "SELECT TOP 10 * FROM [User]",
},
{
name: "select_top_10_*_from_User_offset_20",
q: theQuery{
from: &CollectionRef{Name: "User"},
limit: 10,
offset: 20,
},
want: "SELECT TOP 10 * FROM [User]\nOFFSET 20",
},
{
name: "select_*_from_Users_where_SomeID_=_123",
q: theQuery{
from: &CollectionRef{Name: "Users"},
where: ID("SomeID", 123),
},
want: "SELECT * FROM [Users] WHERE SomeID = 123",
},
{
name: "select_*_from_User_where_Email_=_'[email protected]'",
q: theQuery{
from: &CollectionRef{Name: "User"},
columns: []Column{
{Expression: Field("id")},
{Expression: Field("name")},
{Expression: Field("email")},
},
where: NewComparison(FieldRef{Name: "Email"}, Equal, String("[email protected]")),
},
want: "SELECT\n\tid,\n\tname,\n\temail\nFROM [User]\nWHERE Email = '[email protected]'",
},
{
name: "select top 7 * from User",
q: theQuery{
from: &CollectionRef{Name: "User"},
limit: 7,
},
want: "SELECT TOP 7 * FROM [User]",
},
{
name: "select top 7 * from User order by Email, Created DESC",
q: theQuery{
from: &CollectionRef{Name: "User"},
limit: 7,
orderBy: []OrderExpression{
Ascending(Field("Email")),
Descending(Field("Created")),
},
},
want: "SELECT TOP 7 * FROM [User]\nORDER BY Email, Created DESC",
},
{ // TODO: Demo test generation
name: "select top 7 * from User order by Email, Created DESC group by Email, Created",
q: theQuery{
from: &CollectionRef{Name: "User"},
limit: 7,
orderBy: []OrderExpression{
Ascending(Field("Email")),
Descending(Field("Created")),
},
groupBy: []Expression{
Field("Email"),
Field("Created"),
},
},
want: "SELECT TOP 7 * FROM [User]\nGROUP BY Email, Created\nORDER BY Email, Created DESC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Run("From", func(t *testing.T) {
assert.Equal(t, tt.q.from, tt.q.From())
})
t.Run("Where", func(t *testing.T) {
assert.Equal(t, tt.q.where, tt.q.Where())
})
t.Run("GroupBy", func(t *testing.T) {
assert.Equal(t, tt.q.groupBy, tt.q.GroupBy())
})
t.Run("OrderBy", func(t *testing.T) {
assert.Equal(t, tt.q.orderBy, tt.q.OrderBy())
})
t.Run("Columns", func(t *testing.T) {
assert.Equal(t, tt.q.columns, tt.q.Columns())
})
t.Run("Into", func(t *testing.T) {
if tt.q.into == nil {
assert.Nil(t, tt.q.Into())
return
}
assert.Equal(t, tt.q.into(), tt.q.Into()())
})
t.Run("IDKind", func(t *testing.T) {
assert.Equal(t, tt.q.idKind, tt.q.IDKind())
})
t.Run("StartFrom", func(t *testing.T) {
assert.Equal(t, tt.q.startCursor, tt.q.StartFrom())
})
t.Run("Offset", func(t *testing.T) {
assert.Equal(t, tt.q.offset, tt.q.Offset())
})
t.Run("Limit", func(t *testing.T) {
assert.Equal(t, tt.q.limit, tt.q.Limit())
})
t.Run("String", func(t *testing.T) {
if got := tt.q.String(); got != tt.want {
t.Errorf("Got:\n%v\n\nWant:\n%v", got, tt.want)
}
})
t.Run("And", func(t *testing.T) {
q := tt.q.And(NewComparison(FieldRef{Name: "Email"}, Equal, String("[email protected]")))
assert.NotEqual(t, tt.q, q)
})
t.Run("Or", func(t *testing.T) {
q := tt.q.Or(NewComparison(FieldRef{Name: "Email"}, Equal, String("[email protected]")))
assert.NotEqual(t, tt.q, q)
})
})
}
}