forked from mgutz/dat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
select_doc_test.go
225 lines (204 loc) · 4.82 KB
/
select_doc_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package dat
import (
"testing"
"time"
"github.com/mgutz/str"
"gopkg.in/stretchr/testify.v1/assert"
)
func TestSelectDocSQLNoDocs(t *testing.T) {
sql, args := SelectDoc("b", "c").From("a").Where("d=$1", 4).ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT b,c
FROM a
WHERE (d=$1)
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Equal(t, []interface{}{4}, args)
}
func TestSelectDocSQLDocs(t *testing.T) {
sql, args := SelectDoc("b", "c").
Many("f", `SELECT g, h FROM f WHERE id= $1`, 4).
Many("x", `SELECT id, y, z FROM x`).
From("a").
Where("d=$1", 4).
ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT
b,
c,
(SELECT array_agg(dat__f.*) FROM (SELECT g,h FROM f WHERE id=$1) AS dat__f) AS "f",
(SELECT array_agg(dat__x.*) FROM (SELECT id,y,z FROM x) AS dat__x) AS "x"
FROM a
WHERE (d=$2)
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Equal(t, []interface{}{4, 4}, args)
}
func TestSelectDocSQLInnerSQL(t *testing.T) {
sql, args := SelectDoc("b", "c").
Many("f", `SELECT g, h FROM f WHERE id= $1`, 4).
Many("x", `SELECT id, y, z FROM x`).
InnerSQL(`
FROM a
WHERE d = $1
`, 4).
ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT
b,
c,
(SELECT array_agg(dat__f.*) FROM (SELECT g,h FROM f WHERE id=$1) AS dat__f) AS "f",
(SELECT array_agg(dat__x.*) FROM (SELECT id,y,z FROM x) AS dat__x) AS "x"
FROM a
WHERE d=$2
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Equal(t, []interface{}{4, 4}, args)
}
func TestSelectDocScope(t *testing.T) {
now := NullTimeFrom(time.Now())
sql, args := SelectDoc("e", "f").
From("matches m").
Scope(`
WHERE m.game_id = $1
AND (
m.id > $3
OR (m.id >= $2 AND m.id <= $3 AND m.updated_at > $4)
)
`, 100, 1, 2, now).
ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT e, f
FROM matches m
WHERE (m.game_id=$1
AND (
m.id > $3
OR (m.id >= $2 AND m.id<=$3 AND m.updated_at>$4)
))
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Equal(t, []interface{}{100, 1, 2, now}, args)
}
func TestDocScopeWhere(t *testing.T) {
published := `
INNER JOIN posts p on (p.author_id = u.id)
WHERE
p.state = $1
`
sql, args := SelectDoc("u.*, p.*").
From(`users u`).
Scope(published, "published").
Where(`u.id = $1`, 1).
ToSQL()
sql = str.Clean(sql)
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT u.*, p.*
FROM users u
INNER JOIN posts p on (p.author_id = u.id)
WHERE (u.id = $1) AND ( p.state = $2 )
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Exactly(t, args, []interface{}{1, "published"})
}
func TestDocDistinctOn(t *testing.T) {
published := `
INNER JOIN posts p on (p.author_id = u.id)
WHERE
p.state = $1
`
sql, args := SelectDoc("u.*, p.*").
DistinctOn("aa", "bb").
From(`users u`).
Scope(published, "published").
Where(`u.id = $1`, 1).
ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT DISTINCT ON (aa, bb)
u.*, p.*
FROM users u
INNER JOIN posts p on (p.author_id = u.id)
WHERE (u.id = $1) AND ( p.state = $2 )
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Exactly(t, args, []interface{}{1, "published"})
}
func TestNestedSelecDocWhere(t *testing.T) {
user := SelectDoc("id", "user_name").
Many("comments", `SELECT * FROM comments WHERE id = u.id`).
From("users u").
Where("u.id = $1", 1)
sql, args := SelectDoc("id").
One("user", user).
From(`games`).
Where(`id = $1`, 10).
ToSQL()
expected := `
SELECT row_to_json(dat__item.*)
FROM (
SELECT id,
(
SELECT row_to_json(dat__user.*)
FROM (
SELECT id, user_name,
(
SELECT array_agg(dat__comments.*)
FROM (SELECT * FROM comments WHERE id = u.id)
AS dat__comments
) AS "comments"
FROM users u
WHERE (u.id = $1)
) AS dat__user
) AS "user"
FROM games
WHERE (id = $2)
) as dat__item
`
assert.Equal(t, stripWS(expected), stripWS(sql))
assert.Exactly(t, args, []interface{}{1, 10})
}
func TestSelectDocColumns(t *testing.T) {
sql, args := SelectDoc("id, user_name").
From("users").
Columns("created_at").
ToSQL()
assert.Equal(t, stripWS(`
SELECT row_to_json(dat__item.*)
FROM (
SELECT id, user_name, created_at
FROM users
) as dat__item`), stripWS(sql))
assert.Nil(t, args)
}
func TestSelectDocFor(t *testing.T) {
sql, args := SelectDoc("id, user_name").
From("users").
Columns("created_at").
For("UPDATE").
ToSQL()
assert.Equal(t, stripWS(`
SELECT row_to_json(dat__item.*)
FROM (
SELECT id, user_name, created_at
FROM users
FOR UPDATE
) as dat__item`), stripWS(sql))
assert.Nil(t, args)
}