-
Notifications
You must be signed in to change notification settings - Fork 18
/
builder_tests.rb
273 lines (215 loc) · 8.13 KB
/
builder_tests.rb
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# frozen_string_literal: true
module MiniSql::BuilderTests
class ValidatingParamEncoder
def initialize(old_encoder)
@old_encoder = old_encoder
end
def encode(sql, *params)
params[0].each do |(k, v)|
if !(Symbol === k)
raise "Attempting to use a String instead of Symbol as a key"
end
if !k.to_s.match?(/^[a-z]/i)
raise "Incompatible key used"
end
end
@old_encoder.encode(sql, *params)
end
end
def test_where
builder = @connection.build("select 1 as one /*where*/")
builder.where("1 = :zero")
l = builder.query(zero: 0).length
assert_equal(0, l)
end
def test_param_encoder_compat
ignore_warnings do
class << @connection
alias :old_param_encoder :param_encoder
def param_encoder
MiniSql::BuilderTests::ValidatingParamEncoder.new(old_param_encoder)
end
end
end
builder = @connection.build("select 1 as one /*where*/ /*offset*/ /*limit*/")
builder.where("1 = ?", 0)
builder.where("2 = :not_two", not_two: 1)
builder.offset(2)
builder.limit(2)
# all params replaced
refute_match(/:/, builder.to_sql)
ensure
ignore_warnings do
class << @connection
alias :param_encoder :old_param_encoder
end
end
end
def test_append_params
builder = @connection.build("select 1 as one /*where*/")
builder.where("1 = :zero", zero: 0)
assert_equal(0, builder.exec)
end
def test_mixing_params
builder = @connection.build("select 1 as one /*where*/")
builder.where("1 = ?", 1)
builder.where("1 = :one", one: 1)
assert_equal(1, builder.exec)
end
def test_where2
builder = @connection.build("select 1 as one /*where2*/")
builder.where2("1 = -1")
assert_equal(0, builder.exec)
end
def test_join
@connection.exec("create temporary table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta /*join*/")
builder.join("(select 1 as _one) as X on _one = x")
assert_equal(1, builder.exec)
builder.join("(select 2 as _two) as Y on _two = x")
assert_equal(0, builder.exec)
end
def test_left_join
@connection.exec("create temporary table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta /*left_join*/ /*order_by*/")
builder.left_join("(select 1 as id, 'hello' as name) AS j ON id = x")
builder.order_by('x asc')
r = builder.query
assert_equal('hello', r[0].name)
assert_nil(r[1].name)
assert_equal(3, r.length)
end
def test_group_by
@connection.exec("create temporary table ta(x int, t text)")
@connection.exec("insert into ta(x,t) values(1 ,'a'),(2,'a'),(3, 'b')")
builder = @connection.build("/*select*/ from ta /*group_by*/")
builder.select('t, SUM(x)')
builder.group_by("t")
r = builder.query_array.to_h
assert_equal({ 'a' => 3, 'b' => 3 }, r)
end
def test_set
@connection.exec("create temporary table ta(x int, y int)")
@connection.exec("insert into ta values(1,2)")
builder = @connection.build("update ta /*set*/")
builder.set('x = ?', 7)
builder.set('y = ?', 8)
assert_equal(1, builder.exec)
assert_equal([7, 8], @connection.query_single("select * from ta"))
end
def test_accepts_params_at_end
builder = @connection.build("select :bob as a /*where*/")
builder.where('1 = :one', one: 1)
r = builder.query_hash(bob: 1)
assert_equal([{ "a" => 1 }], r)
r = builder.query_hash(bob: 1, one: 2)
assert_equal([], r)
end
def test_offset_limit
@connection.exec("create temporary table ta(x int)")
@connection.exec("insert into ta(x) values(1),(2),(3)")
builder = @connection.build("select * from ta ORDER BY x /*limit*/ /*offset*/")
builder.limit(1)
builder.offset(1)
assert_equal([2], builder.query_single)
end
module ProductDecorator
def amount_price
price * quantity
end
end
def test_query_decorator
builder = @connection.build("select :price AS price, :quantity AS quantity /*where*/")
builder.where('1 = :one', one: 1)
r = builder.query_decorator(ProductDecorator, price: 20, quantity: 3).first
assert_equal(60, r.amount_price)
end
def test_where_or
builder = @connection.build("SELECT 1 as one /*where*/")
builder
.where_or("1 = ?", 2)
.where_or("1 = ?", 1)
assert_equal(builder.to_sql, "SELECT 1 as one WHERE ((1 = 2) OR (1 = 1))")
end
def test_where_or_and
builder = @connection.build("SELECT 1 as one /*where*/")
builder
.where_or("1 = ?", 2)
.where_or("1 = ?", 1)
.where("3 = ?", 3)
.where("4 = ?", 4)
assert_equal(builder.to_sql, "SELECT 1 as one WHERE (3 = 3) AND (4 = 4) AND ((1 = 2) OR (1 = 1))")
end
def test_to_sql_without_params
builder = @connection.build("SELECT price, quantity AS quantity FROM products /*where*/ /*limit*/ /*order_by*/")
builder.where('id = :id', id: 10)
builder.where('is_sale = ?', true)
builder.limit(50)
builder.order_by("created_at DESC")
sql = <<~SQL.gsub(/[[:space:]]+/, " ").strip
SELECT price, quantity AS quantity
FROM products
WHERE (id = 10) AND (is_sale = true)
LIMIT 50
ORDER BY created_at DESC
SQL
assert_equal(builder.to_sql, sql)
end
def test_to_sql_with_params
builder = @connection.build("SELECT price, quantity AS quantity FROM products WHERE id = :id AND is_sale = :is_sale /*limit*/ /*order_by*/")
builder.limit(50)
builder.order_by('created_at DESC')
sql = <<~SQL.gsub(/[[:space:]]+/, " ").strip
SELECT price, quantity AS quantity
FROM products
WHERE id = 10 AND is_sale = true
LIMIT 50
ORDER BY created_at DESC
SQL
assert_equal(builder.to_sql(id: 10, is_sale: true), sql)
end
def test_exception_when_section_not_defined
builder = @connection.build("SELECT * FROM products /*where2*/").where('id = ?', 10)
err = assert_raises(RuntimeError) { builder.to_sql }
assert_match 'The section for the /*where*/ clause was not found!', err.message
end
def test_sql_literal
builder = @connection.build("SELECT * FROM products /*product_where*/")
builder.sql_literal(product_where: 'WHERE id = 10')
assert_equal(builder.to_sql, 'SELECT * FROM products WHERE id = 10')
end
def test_sql_literal_multiple
builder = @connection.build("(select 1 AS x /*polymorphic_where*/) union all (select 2 AS x /*polymorphic_where*/)")
builder.sql_literal(polymorphic_where: 'where 1 = 1')
assert_equal(builder.to_sql, "(select 1 AS x where 1 = 1) union all (select 2 AS x where 1 = 1)")
end
def test_sql_literal_for_builder
user_builder = @connection.build("SELECT * FROM users /*where*/").where('id = ?', 10)
builder = @connection.build("SELECT * FROM (/*user_table*/) AS t")
builder.sql_literal(user_table: user_builder)
assert_equal(builder.to_sql, 'SELECT * FROM (SELECT * FROM users WHERE (id = 10)) AS t')
end
def test_sql_literal_predefined
builder = @connection.build("select 1 /*where*/")
err = assert_raises(RuntimeError) { builder.sql_literal(where: "where 1 = 1") }
assert_match '/*where*/ is predefined, use method `.where` instead `sql_literal`', err.message
end
def test_count
@connection.exec("create temporary table users(x int)")
@connection.exec("insert into users values (1),(2),(3),(5),(5)")
builder = @connection.build("/*select*/ FROM users")
assert_equal builder.count, 5
assert_equal builder.count('distinct x'), 4
assert_equal(builder.select('*').to_sql, 'SELECT * FROM users')
end
def test_dup_and_clone
base_builder = @connection.build("/*select*/ FROM users /*where*/").where('11 = ?', 11)
builder1 = base_builder.dup.where('id = ?', 10).select('id')
builder2 = base_builder.clone.where('id > ?', 5).select('count(*)')
assert_equal(base_builder.to_sql, '/*select*/ FROM users WHERE (11 = 11)')
assert_equal(builder1.to_sql, 'SELECT id FROM users WHERE (11 = 11) AND (id = 10)')
assert_equal(builder2.to_sql, 'SELECT count(*) FROM users WHERE (11 = 11) AND (id > 5)')
end
end