extralite-bundle 2.6 → 2.7

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_query.rb CHANGED
@@ -2,8 +2,9 @@
2
2
 
3
3
  require_relative 'helper'
4
4
  require 'date'
5
+ require 'json'
5
6
 
6
- class QueryTest < MiniTest::Test
7
+ class QueryTest < Minitest::Test
7
8
  def setup
8
9
  @db = Extralite::Database.new(':memory:')
9
10
  @db.query('create table if not exists t (x,y,z)')
@@ -17,10 +18,47 @@ class QueryTest < MiniTest::Test
17
18
 
18
19
  def test_prepare
19
20
  query = @db.prepare('select 1')
20
- assert_equal 1, query.next_single_column
21
+ assert_equal({ '1': 1 }, query.next)
21
22
 
22
23
  query = @db.prepare('select x from t where y = ?', 5)
23
- assert_equal 4, query.next_single_column
24
+ assert_equal({ x: 4 }, query.next)
25
+ end
26
+
27
+ def test_mode
28
+ query = @db.prepare('select 1')
29
+ assert_equal :hash, query.mode
30
+
31
+ query.mode = :argv
32
+ assert_equal :argv, query.mode
33
+
34
+ query.mode = :ary
35
+ assert_equal :ary, query.mode
36
+
37
+ assert_raises(Extralite::Error) { query.mode = :foo }
38
+ assert_equal :ary, query.mode
39
+
40
+ query.mode = :hash
41
+ assert_equal :hash, query.mode
42
+ end
43
+
44
+ def test_prepare_argv
45
+ query = @db.prepare_argv('select 1')
46
+ assert_equal :argv, query.mode
47
+
48
+ assert_equal 1, query.next
49
+ end
50
+
51
+ def test_prepare_argv_with_too_many_columns
52
+ q = @db.prepare_argv('select 1, 2, 3, 4, 5, 6, 7, 8, 9')
53
+
54
+ assert_raises(Extralite::Error) { q.next }
55
+ end
56
+
57
+ def test_prepare_ary
58
+ query = @db.prepare_ary('select 1')
59
+ assert_equal :ary, query.mode
60
+
61
+ assert_equal [1], query.next
24
62
  end
25
63
 
26
64
  def test_query_props
@@ -33,13 +71,13 @@ class QueryTest < MiniTest::Test
33
71
  def test_bind
34
72
  @db.query("insert into t values ('a', 'b', 'c')")
35
73
 
36
- q = @db.prepare('select * from t where `z` = :foo')
37
- results = q.bind(foo: 'c').to_a_ary
74
+ q = @db.prepare_ary('select * from t where `z` = :foo')
75
+ results = q.bind(foo: 'c').to_a
38
76
 
39
77
  assert_equal [['a', 'b', 'c']], results
40
78
 
41
79
  # try again with the same parameters
42
- results = q.to_a_ary
80
+ results = q.to_a
43
81
 
44
82
  assert_equal [['a', 'b', 'c']], results
45
83
  end
@@ -61,19 +99,6 @@ class QueryTest < MiniTest::Test
61
99
  # EOF should repeat
62
100
  v = query.next
63
101
  assert_nil v
64
-
65
- query = @db.prepare('select * from t')
66
- v = query.next_hash
67
- assert_equal({ x: 1, y: 2, z: 3}, v)
68
-
69
- v = query.next_hash
70
- assert_equal({ x: 4, y: 5, z: 6}, v)
71
-
72
- v = query.next_hash
73
- assert_equal({ x: 7, y: 8, z: 9}, v)
74
-
75
- v = query.next_hash
76
- assert_nil v
77
102
  end
78
103
 
79
104
  def test_query_next_with_row_count
@@ -134,97 +159,112 @@ class QueryTest < MiniTest::Test
134
159
  end
135
160
 
136
161
  def test_query_next_ary
137
- query = @db.prepare('select * from t')
138
- v = query.next_ary
162
+ query = @db.prepare_ary('select * from t')
163
+ v = query.next
139
164
  assert_equal([1, 2, 3], v)
140
165
 
141
- v = query.next_ary
166
+ v = query.next
142
167
  assert_equal([4, 5, 6], v)
143
168
 
144
- v = query.next_ary
169
+ v = query.next
145
170
  assert_equal([7, 8, 9], v)
146
171
 
147
- v = query.next_ary
172
+ v = query.next
148
173
  assert_nil v
149
174
 
150
- v = query.next_ary
175
+ v = query.next
151
176
  assert_nil v
152
177
  end
153
178
 
154
179
  def test_query_next_ary_with_row_count
155
- query = @db.prepare('select * from t')
156
- v = query.next_ary(1)
180
+ query = @db.prepare_ary('select * from t')
181
+ v = query.next(1)
157
182
  assert_equal([[1, 2, 3]], v)
158
183
 
159
- v = query.next_ary(2)
184
+ v = query.next(2)
160
185
  assert_equal([[4, 5, 6], [7, 8, 9]], v)
161
186
 
162
- v = query.next_ary(2)
187
+ v = query.next(2)
163
188
  assert_equal([], v)
164
189
 
165
- v = query.next_ary(2)
190
+ v = query.next(2)
166
191
  assert_nil v
167
192
  end
168
193
 
169
194
  def test_query_next_ary_with_block
170
- query = @db.prepare('select * from t')
195
+ query = @db.prepare_ary('select * from t')
171
196
  buf = []
172
- v = query.next_ary { |r| buf << r }
197
+ v = query.next { |r| buf << r }
173
198
  assert_equal query, v
174
199
  assert_equal [[1, 2, 3]], buf
175
200
 
176
201
  buf = []
177
- v = query.next_ary(2) { |r| buf << r }
202
+ v = query.next(2) { |r| buf << r }
178
203
  assert_equal query, v
179
204
  assert_equal [[4, 5, 6], [7, 8, 9]], buf
180
205
 
181
206
  buf = []
182
- v = query.next_ary(2) { |r| buf << r }
207
+ v = query.next(2) { |r| buf << r }
183
208
  assert_equal query, v
184
209
  assert_equal [], buf
185
210
  end
186
211
 
187
- def test_query_next_single_column
188
- query = @db.prepare('select x from t')
189
- v = query.next_single_column
212
+ def test_query_next_argv_single_column
213
+ query = @db.prepare_argv('select x from t')
214
+ v = query.next
190
215
  assert_equal(1, v)
191
216
 
192
- v = query.next_single_column
217
+ v = query.next
193
218
  assert_equal(4, v)
194
219
 
195
- v = query.next_single_column
220
+ v = query.next
196
221
  assert_equal(7, v)
197
222
 
198
- v = query.next_single_column
223
+ v = query.next
199
224
  assert_nil v
200
225
  end
201
226
 
202
- def test_query_next_single_column_with_row_count
203
- query = @db.prepare('select x from t')
204
- v = query.next_single_column(1)
227
+ def test_query_next_argv_multi_column
228
+ query = @db.prepare_argv('select x, y from t')
229
+ v = query.next
230
+ assert_equal([1, 2], v)
231
+
232
+ v = query.next
233
+ assert_equal([4, 5], v)
234
+
235
+ v = query.next
236
+ assert_equal([7, 8], v)
237
+
238
+ v = query.next
239
+ assert_nil v
240
+ end
241
+
242
+ def test_query_next_argv_with_row_count
243
+ query = @db.prepare_argv('select x from t')
244
+ v = query.next(1)
205
245
  assert_equal([1], v)
206
246
 
207
- v = query.next_single_column(3)
247
+ v = query.next(3)
208
248
  assert_equal([4, 7], v)
209
249
 
210
- v = query.next_single_column(2)
250
+ v = query.next(2)
211
251
  assert_nil v
212
252
  end
213
253
 
214
- def test_query_next_single_column_with_block
215
- query = @db.prepare('select x from t')
254
+ def test_query_next_argv_with_block
255
+ query = @db.prepare_argv('select x, y from t')
216
256
  buf = []
217
- v = query.next_single_column { |r| buf << r }
257
+ v = query.next { |x, y| buf << [x, y] }
218
258
  assert_equal query, v
219
- assert_equal [1], buf
259
+ assert_equal [[1, 2]], buf
220
260
 
221
261
  buf = []
222
- v = query.next_single_column(2) { |r| buf << r }
262
+ v = query.next(2) { |x, y| buf << [x, y] }
223
263
  assert_equal query, v
224
- assert_equal [4, 7], buf
264
+ assert_equal [[4, 5], [7, 8]], buf
225
265
 
226
266
  buf = []
227
- v = query.next_single_column(2) { |r| buf << r }
267
+ v = query.next(2) { |x, y| buf << [x, y] }
228
268
  assert_equal query, v
229
269
  assert_equal [], buf
230
270
  end
@@ -233,14 +273,13 @@ class QueryTest < MiniTest::Test
233
273
  assert_equal [{ x: 1, y: 2, z: 3 }], @query.bind(1).to_a
234
274
  assert_equal [{ x: 4, y: 5, z: 6 }], @query.bind(4).to_a
235
275
 
236
- assert_equal [{ x: 1, y: 2, z: 3 }], @query.bind(1).to_a_hash
237
- assert_equal [{ x: 4, y: 5, z: 6 }], @query.bind(4).to_a_hash
276
+ @query.mode = :ary
238
277
 
239
- assert_equal [[1, 2, 3]], @query.bind(1).to_a_ary
240
- assert_equal [[4, 5, 6]], @query.bind(4).to_a_ary
278
+ assert_equal [[1, 2, 3]], @query.bind(1).to_a
279
+ assert_equal [[4, 5, 6]], @query.bind(4).to_a
241
280
 
242
- query = @db.prepare('select y from t')
243
- assert_equal [2, 5, 8], query.to_a_single_column
281
+ query = @db.prepare_argv('select y from t')
282
+ assert_equal [2, 5, 8], query.to_a
244
283
  end
245
284
 
246
285
  def test_query_each
@@ -285,23 +324,24 @@ class QueryTest < MiniTest::Test
285
324
 
286
325
  def test_query_each_ary
287
326
  buf = []
288
- @query.bind(1).each_ary { |r| buf << r }
327
+ @query.mode = :ary
328
+ @query.bind(1).each { |r| buf << r }
289
329
  assert_equal [[1, 2, 3]], buf
290
330
 
291
331
  # each should reset the stmt
292
332
  buf = []
293
- @query.each_ary { |r| buf << r }
333
+ @query.each { |r| buf << r }
294
334
  assert_equal [[1, 2, 3]], buf
295
335
 
296
- query = @db.prepare('select * from t')
336
+ query = @db.prepare_ary('select * from t')
297
337
  buf = []
298
- query.each_ary { |r| buf << r }
338
+ query.each { |r| buf << r }
299
339
  assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]], buf
300
340
  end
301
341
 
302
342
  def test_query_each_ary_without_block
303
- query = @db.prepare('select * from t')
304
- iter = query.each_ary
343
+ query = @db.prepare_ary('select * from t')
344
+ iter = query.each
305
345
  assert_kind_of Extralite::Iterator, iter
306
346
 
307
347
  buf = []
@@ -310,26 +350,54 @@ class QueryTest < MiniTest::Test
310
350
  assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]], buf
311
351
  end
312
352
 
313
- def test_query_each_single_column
314
- query = @db.prepare('select x from t where x = ?')
353
+ def test_query_each_argv
315
354
  buf = []
316
- query.bind(1).each_single_column { |r| buf << r }
355
+ @query.mode = :argv
356
+ @query.bind(1).each { |a, b, c| buf << [a, b, c] }
357
+ assert_equal [[1, 2, 3]], buf
358
+
359
+ # each should reset the stmt
360
+ buf = []
361
+ @query.each { |a, b, c| buf << [a, b, c] }
362
+ assert_equal [[1, 2, 3]], buf
363
+
364
+ query = @db.prepare_argv('select * from t')
365
+ buf = []
366
+ query.each { |a, b, c| buf << [a, b, c] }
367
+ assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]], buf
368
+ end
369
+
370
+ def test_query_each_argv_without_block
371
+ query = @db.prepare_argv('select * from t')
372
+ iter = query.each
373
+ assert_kind_of Extralite::Iterator, iter
374
+
375
+ buf = []
376
+ v = iter.each { |a, b, c| buf << [a, b, c] }
377
+ assert_equal iter, v
378
+ assert_equal [[1, 2, 3], [4, 5, 6], [7, 8, 9]], buf
379
+ end
380
+
381
+ def test_query_each_argv_single_column
382
+ query = @db.prepare_argv('select x from t where x = ?')
383
+ buf = []
384
+ query.bind(1).each { |r| buf << r }
317
385
  assert_equal [1], buf
318
386
 
319
387
  # each should reset the stmt
320
388
  buf = []
321
- query.each_single_column { |r| buf << r }
389
+ query.each { |r| buf << r }
322
390
  assert_equal [1], buf
323
391
 
324
- query = @db.prepare('select x from t')
392
+ query = @db.prepare_argv('select x from t')
325
393
  buf = []
326
- query.each_single_column { |r| buf << r }
394
+ query.each { |r| buf << r }
327
395
  assert_equal [1, 4, 7], buf
328
396
  end
329
397
 
330
- def test_query_each_single_column_without_block
331
- query = @db.prepare('select x from t')
332
- iter = query.each_single_column
398
+ def test_query_each_argv_single_column_without_block
399
+ query = @db.prepare_argv('select x from t')
400
+ iter = query.each
333
401
  assert_kind_of Extralite::Iterator, iter
334
402
 
335
403
  buf = []
@@ -416,33 +484,33 @@ class QueryTest < MiniTest::Test
416
484
  class Foo; end
417
485
 
418
486
  def test_parameter_binding_from_hash
419
- assert_equal 42, @db.prepare('select :bar').bind(foo: 41, bar: 42).next_single_column
420
- assert_equal 42, @db.prepare('select :bar').bind('foo' => 41, 'bar' => 42).next_single_column
421
- assert_equal 42, @db.prepare('select ?8').bind(7 => 41, 8 => 42).next_single_column
422
- assert_nil @db.prepare('select :bar').bind(foo: 41).next_single_column
487
+ assert_equal 42, @db.prepare_argv('select :bar').bind(foo: 41, bar: 42).next
488
+ assert_equal 42, @db.prepare_argv('select :bar').bind('foo' => 41, 'bar' => 42).next
489
+ assert_equal 42, @db.prepare_argv('select ?8').bind(7 => 41, 8 => 42).next
490
+ assert_nil @db.prepare_argv('select :bar').bind(foo: 41).next
423
491
 
424
- error = assert_raises(Extralite::ParameterError) { @db.prepare('select ?').bind(Foo.new => 42).next_single_column }
492
+ error = assert_raises(Extralite::ParameterError) { @db.prepare_argv('select ?').bind(Foo.new => 42).next }
425
493
  assert_equal error.message, 'Cannot bind parameter with a key of type QueryTest::Foo'
426
494
 
427
- error = assert_raises(Extralite::ParameterError) { @db.prepare('select ?').bind(%w[a b] => 42).next_single_column }
495
+ error = assert_raises(Extralite::ParameterError) { @db.prepare_argv('select ?').bind(%w[a b] => 42).next }
428
496
  assert_equal error.message, 'Cannot bind parameter with a key of type Array'
429
497
  end
430
498
 
431
499
  def test_parameter_binding_from_struct
432
- foo_bar = Struct.new(:":foo", :bar)
500
+ foo_bar = Struct.new(:':foo', :bar)
433
501
  value = foo_bar.new(41, 42)
434
- assert_equal 41, @db.prepare('select :foo').bind(value).next_single_column
435
- assert_equal 42, @db.prepare('select :bar').bind(value).next_single_column
436
- assert_nil @db.prepare('select :baz').bind(value).next_single_column
502
+ assert_equal 41, @db.prepare_argv('select :foo').bind(value).next
503
+ assert_equal 42, @db.prepare_argv('select :bar').bind(value).next
504
+ assert_nil @db.prepare_argv('select :baz').bind(value).next
437
505
  end
438
506
 
439
507
  def test_parameter_binding_from_data_class
440
508
  skip "Data isn't supported in Ruby < 3.2" if RUBY_VERSION < '3.2'
441
509
 
442
- foo_bar = Data.define(:":foo", :bar)
443
- value = foo_bar.new(":foo": 41, bar: 42)
444
- assert_equal 42, @db.prepare('select :bar').bind(value).next_single_column
445
- assert_nil @db.prepare('select :baz').bind(value).next_single_column
510
+ foo_bar = Data.define(:':foo', :bar)
511
+ value = foo_bar.new(':foo': 41, bar: 42)
512
+ assert_equal 42, @db.prepare_argv('select :bar').bind(value).next
513
+ assert_nil @db.prepare_argv('select :baz').bind(value).next
446
514
  end
447
515
 
448
516
  def test_query_columns
@@ -451,10 +519,10 @@ class QueryTest < MiniTest::Test
451
519
  end
452
520
 
453
521
  def test_query_columns_with_parameterized_sql
454
- q = @db.prepare('select * from t where z = :z')
522
+ q = @db.prepare_ary('select * from t where z = :z')
455
523
  q.bind(z: 9)
456
524
  assert_equal [:x, :y, :z], q.columns
457
- assert_equal [[7, 8, 9]], q.to_a_ary
525
+ assert_equal [[7, 8, 9]], q.to_a
458
526
  end
459
527
 
460
528
  def test_query_close
@@ -664,16 +732,16 @@ class QueryTest < MiniTest::Test
664
732
  [[3, 3]]
665
733
  ], results
666
734
 
667
- q = @db.prepare('update foo set b = ? returning *')
735
+ q = @db.prepare_ary('update foo set b = ? returning *')
668
736
 
669
- results = q.batch_query_ary([42, 43])
737
+ results = q.batch_query([42, 43])
670
738
  assert_equal [
671
739
  [[1, 42], [2, 42], [3, 42]],
672
740
  [[1, 43], [2, 43], [3, 43]]
673
741
  ], results
674
742
 
675
743
  array = []
676
- changes = q.batch_query_ary([44, 45]) do |rows|
744
+ changes = q.batch_query([44, 45]) do |rows|
677
745
  array << rows
678
746
  end
679
747
  assert_equal 6, changes
@@ -695,16 +763,16 @@ class QueryTest < MiniTest::Test
695
763
  [[3, 3]]
696
764
  ], results
697
765
 
698
- q = @db.prepare('update foo set b = ? returning *')
766
+ q = @db.prepare_ary('update foo set b = ? returning *')
699
767
 
700
- results = q.batch_query_ary(42..43)
768
+ results = q.batch_query(42..43)
701
769
  assert_equal [
702
770
  [[1, 42], [2, 42], [3, 42]],
703
771
  [[1, 43], [2, 43], [3, 43]]
704
772
  ], results
705
773
 
706
774
  array = []
707
- changes = q.batch_query_ary(44..45) do |rows|
775
+ changes = q.batch_query(44..45) do |rows|
708
776
  array << rows
709
777
  end
710
778
  assert_equal 6, changes
@@ -726,10 +794,10 @@ class QueryTest < MiniTest::Test
726
794
  [[3, 3]]
727
795
  ], results
728
796
 
729
- q = @db.prepare('update foo set b = ? returning *')
797
+ q = @db.prepare_ary('update foo set b = ? returning *')
730
798
 
731
799
  pr = parameter_source_proc([42, 43])
732
- results = q.batch_query_ary(pr)
800
+ results = q.batch_query(pr)
733
801
  assert_equal [
734
802
  [[1, 42], [2, 42], [3, 42]],
735
803
  [[1, 43], [2, 43], [3, 43]]
@@ -737,7 +805,7 @@ class QueryTest < MiniTest::Test
737
805
 
738
806
  array = []
739
807
  pr = parameter_source_proc([44, 45])
740
- changes = q.batch_query_ary(pr) do |rows|
808
+ changes = q.batch_query(pr) do |rows|
741
809
  array << rows
742
810
  end
743
811
  assert_equal 6, changes
@@ -759,16 +827,16 @@ class QueryTest < MiniTest::Test
759
827
  [[3, 3]]
760
828
  ], results
761
829
 
762
- q = @db.prepare('update foo set b = ? returning b * 10 + a')
830
+ q = @db.prepare_argv('update foo set b = ? returning b * 10 + a')
763
831
 
764
- results = q.batch_query_single_column([42, 43])
832
+ results = q.batch_query([42, 43])
765
833
  assert_equal [
766
834
  [421, 422, 423],
767
835
  [431, 432, 433]
768
836
  ], results
769
837
 
770
838
  array = []
771
- changes = q.batch_query_single_column([44, 45]) do |rows|
839
+ changes = q.batch_query([44, 45]) do |rows|
772
840
  array << rows
773
841
  end
774
842
  assert_equal 6, changes
@@ -790,16 +858,16 @@ class QueryTest < MiniTest::Test
790
858
  [[3, 3]]
791
859
  ], results
792
860
 
793
- q = @db.prepare('update foo set b = ? returning b * 10 + a')
861
+ q = @db.prepare_argv('update foo set b = ? returning b * 10 + a')
794
862
 
795
- results = q.batch_query_single_column(42..43)
863
+ results = q.batch_query(42..43)
796
864
  assert_equal [
797
865
  [421, 422, 423],
798
866
  [431, 432, 433]
799
867
  ], results
800
868
 
801
869
  array = []
802
- changes = q.batch_query_single_column(44..45) do |rows|
870
+ changes = q.batch_query(44..45) do |rows|
803
871
  array << rows
804
872
  end
805
873
  assert_equal 6, changes
@@ -821,10 +889,10 @@ class QueryTest < MiniTest::Test
821
889
  [[3, 3]]
822
890
  ], results
823
891
 
824
- q = @db.prepare('update foo set b = ? returning b * 10 + a')
892
+ q = @db.prepare_argv('update foo set b = ? returning b * 10 + a')
825
893
 
826
894
  pr = parameter_source_proc([42, 43])
827
- results = q.batch_query_single_column(pr)
895
+ results = q.batch_query(pr)
828
896
  assert_equal [
829
897
  [421, 422, 423],
830
898
  [431, 432, 433]
@@ -832,7 +900,7 @@ class QueryTest < MiniTest::Test
832
900
 
833
901
  array = []
834
902
  pr = parameter_source_proc([44, 45])
835
- changes = q.batch_query_single_column(pr) do |rows|
903
+ changes = q.batch_query(pr) do |rows|
836
904
  array << rows
837
905
  end
838
906
  assert_equal 6, changes
@@ -868,7 +936,7 @@ class QueryTest < MiniTest::Test
868
936
  end
869
937
 
870
938
  def test_query_eof
871
- query = @db.prepare('select x from t')
939
+ query = @db.prepare_argv('select x from t')
872
940
  assert_equal false, query.eof?
873
941
 
874
942
  query.next
@@ -889,13 +957,13 @@ class QueryTest < MiniTest::Test
889
957
  query.next
890
958
  assert_equal false, query.eof?
891
959
 
892
- assert_equal [1, 4, 7], query.to_a_single_column
960
+ assert_equal [1, 4, 7], query.to_a
893
961
  assert_equal true, query.eof?
894
962
  end
895
963
 
896
964
  def test_query_inspect
897
965
  q = @db.prepare('select x from t')
898
- assert_match /^\#\<Extralite::Query:0x[0-9a-f]+ #{q.sql.inspect}\>$/, q.inspect
966
+ assert_match(/^\#\<Extralite::Query:0x[0-9a-f]+ #{q.sql.inspect}\>$/, q.inspect)
899
967
  end
900
968
 
901
969
  def test_query_clone
@@ -915,6 +983,175 @@ class QueryTest < MiniTest::Test
915
983
  assert_kind_of Extralite::Query, q2
916
984
  assert_equal @db, q2.database
917
985
  assert_equal q1.sql, q2.sql
918
- refute_equal q1, q2
986
+ refute_same q1, q2
987
+
988
+ q1 = @db.prepare_argv('select x from t')
989
+ q2 = q1.dup
990
+
991
+ assert_kind_of Extralite::Query, q2
992
+ assert_equal @db, q2.database
993
+ assert_equal q1.sql, q2.sql
994
+ refute_same q1, q2
995
+ assert_equal :argv, q2.mode
996
+ end
997
+
998
+ def test_query_dup_with_transform
999
+ q1 = @db.prepare_ary('select x, y from t') { |a| a * 2 }
1000
+ q2 = q1.dup
1001
+
1002
+ assert_equal [
1003
+ [1, 2, 1, 2],
1004
+ [4, 5, 4, 5],
1005
+ [7, 8, 7, 8],
1006
+ ], q2.to_a
1007
+ end
1008
+ end
1009
+
1010
+ class QueryTransformTest < Minitest::Test
1011
+ def setup
1012
+ @db = Extralite::Database.new(':memory:')
1013
+ @db.query('create table t (a, b, c)')
1014
+
1015
+ @q1 = @db.prepare_argv('select c from t where a = ?')
1016
+ @q2 = @db.prepare_argv('select c from t order by a')
1017
+
1018
+ @q3 = @db.prepare('select * from t where a = ?')
1019
+ @q4 = @db.prepare('select * from t order by a')
1020
+
1021
+ @q5 = @db.prepare('select a, b from t where a = ?')
1022
+ @q6 = @db.prepare('select a, b from t order by a')
1023
+
1024
+ @db.batch_execute('insert into t (a, b, c) values (?, ?, ?)', [
1025
+ [1, 2, { foo: 42, bar: 43 }.to_json],
1026
+ [4, 5, { foo: 45, bar: 46 }.to_json]
1027
+ ])
1028
+ end
1029
+
1030
+ class MyModel
1031
+ def initialize(h)
1032
+ @h = h
1033
+ end
1034
+
1035
+ def values
1036
+ @h
1037
+ end
1038
+ end
1039
+
1040
+ def test_transform_hash
1041
+ q = @q5.transform { |h| MyModel.new(h) }
1042
+ assert_equal @q5, q
1043
+
1044
+ o = @q5.bind(1).next
1045
+ assert_kind_of MyModel, o
1046
+ assert_equal({ a: 1, b: 2 }, o.values)
1047
+
1048
+ o = @q5.bind(4).next
1049
+ assert_kind_of MyModel, o
1050
+ assert_equal({ a: 4, b: 5 }, o.values)
1051
+
1052
+ assert_equal [
1053
+ [{ a: 1, b: 2 }],
1054
+ [{ a: 4, b: 5 }]
1055
+ ], @q5.batch_query([[1], [4]]).map { |a| a.map(&:values) }
1056
+
1057
+ @q6.transform { |h| MyModel.new(h) }
1058
+ assert_equal [
1059
+ { a: 1, b: 2 },
1060
+ { a: 4, b: 5 }
1061
+ ], @q6.to_a.map(&:values)
1062
+
1063
+ buf = []
1064
+ @q6.each { |r| buf << r.values }
1065
+ assert_equal [
1066
+ { a: 1, b: 2 },
1067
+ { a: 4, b: 5 }
1068
+ ], buf
1069
+ end
1070
+
1071
+ def test_transform_ary
1072
+ @q5.mode = :ary
1073
+ q = @q5.transform { |h| MyModel.new(h) }
1074
+ assert_equal @q5, q
1075
+
1076
+ o = @q5.bind(1).next
1077
+ assert_kind_of MyModel, o
1078
+ assert_equal([1, 2], o.values)
1079
+
1080
+ o = @q5.bind(4).next
1081
+ assert_kind_of MyModel, o
1082
+ assert_equal([4, 5], o.values)
1083
+
1084
+ assert_equal [
1085
+ [[1, 2]],
1086
+ [[4, 5]]
1087
+ ], @q5.batch_query([[1], [4]]).map { |a| a.map(&:values) }
1088
+
1089
+ @q6.mode = :ary
1090
+ @q6.transform { |h| MyModel.new(h) }
1091
+ assert_equal [
1092
+ [1, 2],
1093
+ [4, 5]
1094
+ ], @q6.to_a.map(&:values)
1095
+
1096
+ buf = []
1097
+ @q6.each { |r| buf << r.values }
1098
+ assert_equal [
1099
+ [1, 2],
1100
+ [4, 5]
1101
+ ], buf
1102
+ end
1103
+
1104
+ def test_transform_argv_single_column
1105
+ q = @q1.transform { |c| JSON.parse(c, symbolize_names: true) }
1106
+ assert_equal @q1, q
1107
+
1108
+ assert_equal({ foo: 42, bar: 43 }, @q1.bind(1).next)
1109
+ assert_equal({ foo: 45, bar: 46 }, @q1.bind(4).next)
1110
+
1111
+ assert_equal [
1112
+ [{ foo: 42, bar: 43 }],
1113
+ [{ foo: 45, bar: 46 }]
1114
+ ], @q1.batch_query([[1], [4]])
1115
+
1116
+ @q2.transform { |c| JSON.parse(c, symbolize_names: true) }
1117
+ assert_equal [
1118
+ { foo: 42, bar: 43 },
1119
+ { foo: 45, bar: 46 }
1120
+ ], @q2.to_a
1121
+
1122
+ buf = []
1123
+ @q2.each { |r| buf << r }
1124
+ assert_equal [
1125
+ { foo: 42, bar: 43 },
1126
+ { foo: 45, bar: 46 }
1127
+ ], buf
1128
+ end
1129
+
1130
+ def test_transform_argv_multi_column
1131
+ @q3.mode = :argv
1132
+ q = @q3.transform { |a, b, c| { a: a, b: b, c: JSON.parse(c, symbolize_names: true) } }
1133
+ assert_equal @q3, q
1134
+
1135
+ assert_equal({ a: 1, b: 2, c: { foo: 42, bar: 43 }}, @q3.bind(1).next)
1136
+ assert_equal({ a: 4, b: 5, c: { foo: 45, bar: 46 }}, @q3.bind(4).next)
1137
+
1138
+ assert_equal [
1139
+ [{ a: 1, b: 2, c: { foo: 42, bar: 43 }}],
1140
+ [{ a: 4, b: 5, c: { foo: 45, bar: 46 }}]
1141
+ ], @q3.batch_query([[1], [4]])
1142
+
1143
+ @q4.mode = :argv
1144
+ @q4.transform { |a, b, c| { a: a, b: b, c: JSON.parse(c, symbolize_names: true) } }
1145
+ assert_equal [
1146
+ { a: 1, b: 2, c: { foo: 42, bar: 43 }},
1147
+ { a: 4, b: 5, c: { foo: 45, bar: 46 }}
1148
+ ], @q4.to_a
1149
+
1150
+ buf = []
1151
+ @q4.each { |r| buf << r }
1152
+ assert_equal [
1153
+ { a: 1, b: 2, c: { foo: 42, bar: 43 }},
1154
+ { a: 4, b: 5, c: { foo: 45, bar: 46 }}
1155
+ ], buf
919
1156
  end
920
1157
  end