-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsql_query_spec.rb
More file actions
373 lines (314 loc) · 10.9 KB
/
sql_query_spec.rb
File metadata and controls
373 lines (314 loc) · 10.9 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# frozen_string_literal: true
require 'spec_helper'
describe SqlQuery do
let(:options) { { email: 'e@mail.dev' } }
let(:file_name) { :get_player_by_email }
let(:query) { described_class.new(file_name, options) }
class Model < ActiveRecord::Base
self.abstract_class = true
end
describe '#initialize' do
it 'sets instance variables for all options' do
expect(query.instance_variable_get(:@email)).to eq 'e@mail.dev'
end
context 'when options are set not in parentheses' do
let(:query) { described_class.new(file_name, email: 'e@mail.dev') }
it 'sets instance variables for all options' do
expect(query.instance_variable_get(:@email)).to eq 'e@mail.dev'
end
end
context 'when file_name argument is not Symbol or String' do
let(:file_name) { { do: 'something' } }
it 'raises ArgumentError' do
expect { query }
.to raise_error(ArgumentError,
'SQL file name should be String or Symbol')
end
end
context 'with db_connection option' do
let(:options) { { db_connection: Model.connection } }
it 'sets connection to requested' do
expect(query.connection).to eq(Model.connection)
end
end
end
describe '#file_path' do
context 'when there is only one file with name' do
it 'returns path to it' do
expect(query.send(:file_path)).to include('get_player_by_email.sql.erb')
end
end
context 'when there are no files' do
let(:file_name) { :not_exists }
it 'raises error' do
expect { query.send(:file_path) }
.to raise_error('File not found: not_exists')
end
end
context 'when there are more than one matching files' do
let(:file_name) { :duplicated }
it 'raises error' do
expect { query.send(:file_path) }
.to raise_error.with_message(/More than one file found:/)
end
end
end
describe '#path' do
context 'when there is sql_file_path option' do
let(:options) { { sql_file_path: '/path_to_file' } }
let(:file_name) { 'fname' }
it 'sets it as dir for file' do
expect(query.send(:path)).to eq('/path_to_file/fname.{sql.erb,erb.sql}')
end
end
end
describe '#sql' do
it 'returns query string' do
expect(query.sql)
.to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n")
end
context 'when file is .erb.sql' do
let(:options) { { fake: 12 } }
let(:file_name) { :erb_sql }
it 'returns query string' do
expect(query.sql).to eq "SELECT 12\n"
end
end
end
describe '#pretty_sql' do
it 'returns query string' do
expect(query.pretty_sql)
.to eq("SELECT *\nFROM players\nWHERE email = 'e@mail.dev'\n")
end
end
describe '#explain' do
let(:explain) { query.explain }
it 'returns explain string' do
expect(explain).to include 'EXPLAIN for:'
expect(explain).to include 'FROM players'
expect(explain).to include "WHERE email = 'e@mail.dev'"
expect(explain).to include 'QUERY PLAN'
expect(explain).to include 'Seq Scan on players'
end
end
describe '#execute' do
before do
ActiveRecord::Base.connection.execute(
"INSERT INTO players (email) VALUES ('e@mail.dev')"
)
end
after do
ActiveRecord::Base.connection.execute(
'DELETE FROM players'
)
end
it 'returns data from database' do
expect(query.execute).to eq [{ 'email' => 'e@mail.dev' }]
end
context 'when prepare argument is true' do
it 'executes prepared query for logs' do
expect(query).to receive(:prepared_for_logs) { '' }
query.execute
end
end
context 'when prepare argument is false' do
it 'executes unchanged sql query' do
expect(query).not_to receive(:prepared_for_logs)
query.execute(false)
end
end
end
describe '#exec_query' do
before do
ActiveRecord::Base.connection.execute(
"INSERT INTO players (email) VALUES ('e@mail.dev')"
)
end
after do
ActiveRecord::Base.connection.execute(
'DELETE FROM players'
)
end
it 'returns data from database' do
expect(query.exec_query).to eq [{ 'email' => 'e@mail.dev' }]
end
context 'when prepare argument is true' do
it 'executes prepared query for logs' do
expect(query).to receive(:prepared_for_logs) { '' }
query.exec_query
end
end
context 'when prepare argument is false' do
it 'executes unchanged sql query' do
expect(query).not_to receive(:prepared_for_logs)
query.exec_query(false)
end
end
end
describe '#partial' do
let(:file_name) { :get_player_by_email_with_template }
it 'resolves partials as parts of sql queries' do
expect(query.sql)
.to eq("SELECT *\nFROM players\nWHERE players.email = 'e@mail.dev'\n\n")
end
context 'when partial name is string with file path' do
let(:file_name) { :get_player_by_email }
it 'should find file by whole path and _name' do
query
expect(described_class)
.to receive(:new).with('some/path/to/_file.sql', options) { query }
query.partial('some/path/to/file.sql', {})
end
end
end
describe '#prepared_for_logs' do
it 'returns string without new lines' do
expect(query.prepared_for_logs)
.to eq("SELECT * FROM players WHERE email = 'e@mail.dev' ")
end
context 'when embedded params have multiple whitespaces' do
let(:options) { { email: ' e@mail.dev ' } }
let(:query) { described_class.new(file_name, options) }
it 'returns string without multiple whitespaces except embedded params' do
expect(query.prepared_for_logs)
.to eq("SELECT * FROM players WHERE email = ' e@mail.dev ' ")
end
end
context 'when template has multiline ERB blocks' do
let(:file_name) { :multiline_erb }
let(:options) { { email: 'test@dev.com', field1: 'val1', field2: 'val2' } }
let(:query) { described_class.new(file_name, options) }
it 'processes multiline ERB correctly without syntax errors' do
expect { query.prepared_for_logs }.not_to raise_error
end
it 'collapses SQL whitespace while preserving ERB processing' do
result = query.prepared_for_logs
expect(result).to include("'val1' as field1")
expect(result).to include("'val2' as field2")
expect(result).not_to include("\n") # All newlines should be collapsed
end
end
end
describe 'comment removal integration' do
after do
# Reset configuration to defaults
SqlQuery.configure do |config|
config.remove_comments = :all
config.remove_comments_from = :all
end
end
context 'with remove_comments_from: :all' do
before do
SqlQuery.configure do |config|
config.remove_comments = :all
config.remove_comments_from = :all
end
end
it 'removes comments from sql() method' do
query = described_class.new(:with_single_line_comments, email: 'test@example.com')
result = query.sql
expect(result).not_to include('--')
expect(result).to include('SELECT * FROM players')
end
it 'removes comments from prepared_for_logs() method' do
query = described_class.new(:with_multiline_comments, email: 'test@example.com')
result = query.prepared_for_logs
expect(result).not_to include('/*')
expect(result).not_to include('*/')
end
end
context 'with remove_comments_from: :prepared_for_logs' do
before do
SqlQuery.configure do |config|
config.remove_comments = :all
config.remove_comments_from = :prepared_for_logs
end
end
it 'keeps comments in sql() method' do
query = described_class.new(:with_single_line_comments, email: 'test@example.com')
result = query.sql
expect(result).to include('--')
end
it 'removes comments from prepared_for_logs() method' do
query = described_class.new(:with_single_line_comments, email: 'test@example.com')
result = query.prepared_for_logs
expect(result).not_to include('--')
end
end
context 'with remove_comments_from: :none' do
before do
SqlQuery.configure do |config|
config.remove_comments = :all
config.remove_comments_from = :none
end
end
it 'keeps comments in sql() method' do
query = described_class.new(:with_mixed_comments, email: 'test@example.com')
result = query.sql
expect(result).to include('--')
expect(result).to include('/*')
end
it 'keeps comments in prepared_for_logs() method' do
query = described_class.new(:with_mixed_comments, email: 'test@example.com')
result = query.prepared_for_logs
expect(result).to include('--')
expect(result).to include('/*')
end
end
context 'with remove_comments: :oneline' do
before do
SqlQuery.configure do |config|
config.remove_comments = :oneline
config.remove_comments_from = :all
end
end
it 'removes only single-line comments' do
query = described_class.new(:with_mixed_comments, email: 'test@example.com')
result = query.sql
expect(result).not_to include('--')
expect(result).to include('/*')
end
end
context 'with remove_comments: :multiline' do
before do
SqlQuery.configure do |config|
config.remove_comments = :multiline
config.remove_comments_from = :all
end
end
it 'removes only multi-line comments' do
query = described_class.new(:with_mixed_comments, email: 'test@example.com')
result = query.sql
expect(result).to include('--')
expect(result).not_to include('/*')
expect(result).not_to include('*/')
end
end
context 'preserving comments in quoted strings' do
before do
SqlQuery.configure do |config|
config.remove_comments = :all
config.remove_comments_from = :all
end
end
it 'preserves comment syntax in string literals' do
query = described_class.new(:with_comments_in_strings, email: 'test@example.com')
result = query.sql
expect(result).to include("'-- not a comment'")
expect(result).to include("'/* also not a comment */'")
expect(result).to include('"column--name"')
end
end
end
describe '.config' do
it 'returns configuration instance' do
expect(described_class.config).to be_kind_of(SqlQuery::Config)
end
it 'has default remove_comments setting' do
expect(described_class.config.remove_comments).to eq(:all)
end
it 'has default remove_comments_from setting' do
expect(described_class.config.remove_comments_from).to eq(:all)
end
end
end