jdbc-helper 0.7.6 → 0.7.7

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb CHANGED
@@ -1,4 +1,9 @@
1
+ $VERBOSE = true
2
+
1
3
  require 'rubygems'
4
+ require 'test-unit'
5
+ require 'simplecov'
6
+ SimpleCov.start
2
7
  require 'bundler'
3
8
  require 'insensitive_hash'
4
9
  #require 'pry'
@@ -8,14 +8,21 @@ class TestConnection < Test::Unit::TestCase
8
8
 
9
9
  def get_one_two
10
10
  "
11
- select 1 one, 'two' two from dual
11
+ select 1 One, 'two' tWo from dual
12
12
  union all
13
- select 1 one, 'two' two from dual
13
+ select 1 One, 'two' tWo from dual
14
14
  "
15
15
  end
16
16
 
17
17
  def check_one_two(rec)
18
18
  assert_equal 2, rec.length
19
+ assert_equal %w[one two], rec.labels.map(&:downcase)
20
+ assert_equal %w[one two], rec.keys.map(&:downcase)
21
+ assert_equal [1, 'two'], rec.values
22
+
23
+ assert_equal 2, rec.to_h.length
24
+ assert_equal 1, rec.to_h.insensitive[:one]
25
+ assert_equal 'two', rec.to_h.insensitive[:two]
19
26
 
20
27
  assert_equal 1, rec.one
21
28
  assert_equal 1, rec[0]
@@ -65,7 +72,7 @@ class TestConnection < Test::Unit::TestCase
65
72
  end
66
73
 
67
74
  # ---------------------------------------------------------------
68
-
75
+
69
76
  def test_invalid_driver
70
77
  assert_raise(NameError) {
71
78
  JDBCHelper::Connection.new(:driver => 'xxx', :url => 'localhost')
@@ -251,7 +258,6 @@ class TestConnection < Test::Unit::TestCase
251
258
 
252
259
  # update
253
260
  assert_equal 1, conn.update(iq.call 0)
254
- assert_equal 1, conn.prev_stat.success_count
255
261
 
256
262
  # add_batch execute_batch
257
263
  reset_test_table conn
@@ -262,6 +268,7 @@ class TestConnection < Test::Unit::TestCase
262
268
  ins2.add_batch p, 'C'
263
269
  end
264
270
  conn.execute_batch
271
+ conn.execute_batch # Subsequent call has no effect
265
272
  assert_equal count * 3, conn.table(TEST_TABLE).count
266
273
  assert conn.table(TEST_TABLE).where("a >= #{count}", "a < #{count * 2}").map(&:b).all? { |e| e == 'B' }
267
274
  assert conn.table(TEST_TABLE).where("a >= #{count * 2}").map(&:b).all? { |e| e == 'C' }
@@ -279,6 +286,14 @@ class TestConnection < Test::Unit::TestCase
279
286
  ins1.execute_batch
280
287
  ins2.execute_batch
281
288
  assert_equal 0, conn.table(TEST_TABLE).count
289
+
290
+ # Return values
291
+ count.times do | p |
292
+ conn.update iq.call(p)
293
+ end
294
+ conn.add_batch("delete from #{TEST_TABLE} where a < 5")
295
+ conn.add_batch("delete from #{TEST_TABLE} where a < 7")
296
+ assert_equal 7, conn.execute_batch
282
297
  end
283
298
  end
284
299
 
@@ -290,14 +305,18 @@ class TestConnection < Test::Unit::TestCase
290
305
  assert_equal conn.prepared_statements.first, sel
291
306
 
292
307
  # Fetch size
293
- if iter == 0
294
- assert_nil conn.fetch_size
295
- assert_nil sel.fetch_size
296
- end
308
+ assert_nil conn.fetch_size if iter == 0
309
+ assert_nil sel.fetch_size if iter == 0
310
+
311
+ fsz = conn.fetch_size
297
312
  conn.fetch_size = 100
313
+ assert_equal 100, conn.fetch_size
314
+ assert_equal fsz, sel.fetch_size
315
+
298
316
  sel.fetch_size = 10
299
317
  assert_equal 100, conn.fetch_size
300
318
  assert_equal 10, sel.fetch_size
319
+
301
320
  sel.set_fetch_size 20
302
321
  assert_equal 100, conn.fetch_size
303
322
  assert_equal 20, sel.fetch_size
@@ -352,7 +371,6 @@ class TestConnection < Test::Unit::TestCase
352
371
  # update
353
372
  assert ins.closed? == false
354
373
  assert_equal 1, ins.update(0, 'A')
355
- assert_equal 1, conn.prev_stat.success_count
356
374
  ins.close
357
375
  assert_equal 0, conn.prepared_statements.length
358
376
 
@@ -405,7 +423,7 @@ class TestConnection < Test::Unit::TestCase
405
423
  end
406
424
  end
407
425
  end
408
-
426
+
409
427
  def test_transaction
410
428
  each_connection do | conn |
411
429
  reset_test_table conn
@@ -482,7 +500,7 @@ class TestConnection < Test::Unit::TestCase
482
500
  conn.prepare("insert into #{TEST_TABLE} (a) values (?)").update(ts)
483
501
  got = conn.query("select a from #{TEST_TABLE}")[0][0]
484
502
  arr = [
485
- ts.to_i * 1000,
503
+ ts.to_i * 1000,
486
504
  (ts.to_f * 1000).to_i,
487
505
  # SQL Server seems to round up the millisecond precision
488
506
  (ts.to_f * 1000).to_i / 10 * 10
@@ -516,7 +534,7 @@ class TestConnection < Test::Unit::TestCase
516
534
  end)
517
535
  result = cstmt_name.call(
518
536
  :i1 => 'hello', :i2 => 10,
519
- :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
537
+ :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
520
538
  :n1 => nil,
521
539
  :o1 => Float, 'o2' => String)
522
540
  assert_instance_of Hash, result
@@ -526,7 +544,7 @@ class TestConnection < Test::Unit::TestCase
526
544
  # Invalid parameters
527
545
  #assert_raise(NativeException) { cstmt_ord.call 1 }
528
546
  assert_raise(ArgumentError) { cstmt_ord.call({}, {}) }
529
- assert_raise(NativeException) { cstmt_name.call 1 }
547
+ assert_raise { cstmt_name.call 1 }
530
548
  assert_raise(ArgumentError) { cstmt_name.call({}, {}) }
531
549
 
532
550
  # Close
@@ -551,7 +569,7 @@ class TestConnection < Test::Unit::TestCase
551
569
  result = cstmt_name.call(
552
570
  #:i1 => 'hello',
553
571
  :i2 => 10,
554
- :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
572
+ :io1 => [100, Fixnum], 'io2' => [Time.now, Time],
555
573
  :n1 => nil,
556
574
  :o1 => Float, 'o2' => String)
557
575
  assert_instance_of Hash, result
@@ -578,12 +596,12 @@ class TestConnection < Test::Unit::TestCase
578
596
  def test_invalid_sql
579
597
  each_connection do | conn |
580
598
  reset_test_table conn
581
- assert_raise(NativeException) do
599
+ assert_raise do
582
600
  conn.query("delete from #{TEST_TABLE}")
583
601
  end
584
602
  omit "Oracle does not throw Exception when " +
585
603
  "select statement given to executeUpdate" if conn.driver =~ /oracle/
586
- assert_raise(NativeException) do
604
+ assert_raise do
587
605
  conn.update("select * from #{TEST_TABLE}")
588
606
  end
589
607
  end
@@ -593,7 +611,7 @@ class TestConnection < Test::Unit::TestCase
593
611
  each_connection do | conn |
594
612
  reset_test_table conn
595
613
 
596
- rse_class = JDBCHelper::Connection::ResultSetEnumerator
614
+ rse_class = JDBCHelper::Connection::ResultSetEnumerator
597
615
 
598
616
  # Connection#execute
599
617
  assert_equal 1, conn.execute("insert into #{TEST_TABLE} values (0, 'A')")
@@ -631,7 +649,7 @@ class TestConnection < Test::Unit::TestCase
631
649
  end
632
650
  end
633
651
  end
634
-
652
+
635
653
  def test_statement_pool_leakage
636
654
  q = "select * from #{TEST_TABLE}"
637
655
  u = "update #{TEST_TABLE} set a = 1"
@@ -89,6 +89,7 @@ class TestObjectWrapper < Test::Unit::TestCase
89
89
  # With symbol
90
90
  assert_kind_of JDBCHelper::ObjectWrapper, conn.table(:some_table)
91
91
  assert_instance_of JDBCHelper::TableWrapper, conn.table(:some_table)
92
+ assert_instance_of JDBCHelper::TableWrapper, conn[:some_table]
92
93
  assert_kind_of JDBCHelper::ObjectWrapper, conn.function(:some_func)
93
94
  assert_instance_of JDBCHelper::FunctionWrapper, conn.function(:some_func)
94
95
  assert_kind_of JDBCHelper::ObjectWrapper, conn.procedure(:some_proc)
@@ -132,18 +133,18 @@ class TestObjectWrapper < Test::Unit::TestCase
132
133
  }
133
134
  end
134
135
 
135
- def insert table, cnt = 100
136
+ def insert table, cnt = 100, offset = 1
136
137
  require 'java'
137
138
 
138
139
  params = insert_params.dup
139
140
  params.delete(:num_wtf) unless @type == :oracle
140
141
 
141
- (1..cnt).each do |pk|
142
+ cnt.times do |pk|
142
143
  icnt = table.
143
144
  default(:gamma => 'hello world').
144
145
  default(:alpha => 200).
145
146
  insert(params.merge(
146
- :id => pk,
147
+ :id => pk + offset,
147
148
  :delta => blob_data)
148
149
  )
149
150
  assert_equal 1, icnt unless table.batch?
@@ -171,7 +172,7 @@ class TestObjectWrapper < Test::Unit::TestCase
171
172
  each_connection do |conn, conn_info|
172
173
  next unless [:mysql, :oracle].include?(@type) # TODO: postgres / sqlserver
173
174
 
174
- {
175
+ {
175
176
  :proc => @procedure_name,
176
177
  :db_proc => [conn_info['database'], @procedure_name].join('.')
177
178
  }.each do |mode, prname|
@@ -191,7 +192,7 @@ class TestObjectWrapper < Test::Unit::TestCase
191
192
 
192
193
  result = pr.call(
193
194
  :io1 => [100, Fixnum],
194
- 'io2' => [Time.now, Time],
195
+ 'io2' => [Time.now, Time],
195
196
  :i2 => 10,
196
197
  :i1 => 'hello',
197
198
  :o1 => Float, 'o2' => String)
@@ -207,7 +208,7 @@ class TestObjectWrapper < Test::Unit::TestCase
207
208
  pend("Not tested") do
208
209
  result = pr.call(
209
210
  :io1 => [100, Fixnum],
210
- 'io2' => [Time.now, Time],
211
+ 'io2' => [Time.now, Time],
211
212
  #:i2 => 10,
212
213
  :i1 => 'hello',
213
214
  :o1 => Float, 'o2' => String)
@@ -270,8 +271,8 @@ class TestObjectWrapper < Test::Unit::TestCase
270
271
  table = conn.table(@table_name)
271
272
  params = {
272
273
  :id => 1,
273
- :alpha => 100,
274
- :beta => JDBCHelper::SQL('0.1 + 0.2'),
274
+ :alpha => 100,
275
+ :beta => JDBCHelper::SQL('0.1 + 0.2'),
275
276
  :gamma => 'hello world' }
276
277
 
277
278
  100.times do
@@ -290,7 +291,7 @@ class TestObjectWrapper < Test::Unit::TestCase
290
291
  table = conn.table(@table_name)
291
292
  params = {
292
293
  :id => 1,
293
- :beta => JDBCHelper::SQL('0.1 + 0.2'),
294
+ :beta => JDBCHelper::SQL('0.1 + 0.2'),
294
295
  :gamma => 'hello world' }
295
296
 
296
297
  100.times do |i|
@@ -338,7 +339,7 @@ class TestObjectWrapper < Test::Unit::TestCase
338
339
 
339
340
  # Alias
340
341
  cnt = 0
341
- table.select('alpha omega') do |row|
342
+ table.select('alpha OMega') do |row|
342
343
  cnt += 1
343
344
  assert_equal 100, row.omega
344
345
  assert_equal ['omega'], row.labels.map(&:downcase)
@@ -483,7 +484,19 @@ class TestObjectWrapper < Test::Unit::TestCase
483
484
 
484
485
  insert table.batch, 50
485
486
  assert_equal 0, table.count
486
- conn.execute_batch
487
+ table.execute_batch :delete
488
+ assert_equal 0, table.count
489
+ table.execute_batch :update
490
+ assert_equal 0, table.count
491
+
492
+ table.batch.delete
493
+ table.execute_batch :update, :insert
494
+ assert_equal 50, table.count
495
+ table.clear_batch
496
+
497
+ insert table.batch, 50
498
+ assert_equal 50, table.count
499
+ table.clear_batch
487
500
  assert_equal 50, table.count
488
501
 
489
502
  table.batch.update(:alpha => JDBCHelper::SQL('alpha * 2'))
@@ -493,10 +506,29 @@ class TestObjectWrapper < Test::Unit::TestCase
493
506
  table.delete(:id => 1..10)
494
507
  assert_equal 40, table.count
495
508
 
496
- # Finally
497
- conn.execute_batch
509
+ # Finally with TableWrapper#execute_batch
510
+ insert table.batch, 60, 1000
511
+ assert_equal 40, table.count
512
+ table.execute_batch :delete, :update
513
+ assert_equal 40, table.count
514
+ table.execute_batch :insert
515
+ assert_equal 100, table.count
498
516
 
499
517
  assert_equal 200, table.select(:alpha).to_a.first.alpha.to_i
518
+
519
+ # Order of execution
520
+ table.batch.update(:alpha => JDBCHelper::SQL('alpha * 4'))
521
+ insert table.batch, 50, 2000
522
+ table.batch.delete
523
+ ret = table.execute_batch :delete, :insert, :update
524
+ omit_if(conn.driver =~ /oracle/) do
525
+ # https://forums.oracle.com/forums/thread.jspa?threadID=532945
526
+ assert_equal 100, ret[:delete]
527
+ assert_equal 50, ret[:insert]
528
+ assert_equal 50, ret[:update]
529
+ end
530
+ assert_equal 50, table.count
531
+ assert_equal 400, table.select(:alpha).to_a.first.alpha.to_i
500
532
  end
501
533
  end
502
534
 
@@ -685,5 +717,21 @@ class TestObjectWrapper < Test::Unit::TestCase
685
717
  assert_equal 100, t.count
686
718
  end
687
719
  end
720
+
721
+ def test_fetch_size
722
+ each_connection do |conn|
723
+ create_table conn
724
+
725
+ fsz = 100
726
+ conn.fetch_size = fsz
727
+ cnt = cnt2 = 0
728
+ conn.table(@table_name).fetch_size(fsz) { |row| cnt += 1 }
729
+ conn.table(@table_name).fetch_size(fsz).each { |row| cnt2 += 1 }
730
+ assert_equal cnt, conn.table(@table_name).count
731
+ assert_equal cnt2, conn.table(@table_name).count
732
+
733
+ conn.table(@table_name).fetch_size("No").count
734
+ end
735
+ end
688
736
  end
689
737
 
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: jdbc-helper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.6
5
+ version: 0.7.7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Junegunn Choi
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-26 00:00:00.000000000 Z
12
+ date: 2013-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: insensitive_hash
@@ -18,18 +18,12 @@ dependencies:
18
18
  - - ! '>='
19
19
  - !ruby/object:Gem::Version
20
20
  version: 0.2.4
21
- - - <
22
- - !ruby/object:Gem::Version
23
- version: 0.4.0
24
21
  none: false
25
22
  requirement: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.2.4
30
- - - <
31
- - !ruby/object:Gem::Version
32
- version: 0.4.0
33
27
  none: false
34
28
  prerelease: false
35
29
  type: :runtime
@@ -37,31 +31,35 @@ dependencies:
37
31
  name: bundler
38
32
  version_requirements: !ruby/object:Gem::Requirement
39
33
  requirements:
40
- - - ~>
34
+ - - ! '>='
41
35
  - !ruby/object:Gem::Version
42
- version: 1.1.4
36
+ version: !binary |-
37
+ MA==
43
38
  none: false
44
39
  requirement: !ruby/object:Gem::Requirement
45
40
  requirements:
46
- - - ~>
41
+ - - ! '>='
47
42
  - !ruby/object:Gem::Version
48
- version: 1.1.4
43
+ version: !binary |-
44
+ MA==
49
45
  none: false
50
46
  prerelease: false
51
47
  type: :development
52
48
  - !ruby/object:Gem::Dependency
53
- name: rcov
49
+ name: simplecov
54
50
  version_requirements: !ruby/object:Gem::Requirement
55
51
  requirements:
56
- - - ~>
52
+ - - ! '>='
57
53
  - !ruby/object:Gem::Version
58
- version: '0.9'
54
+ version: !binary |-
55
+ MA==
59
56
  none: false
60
57
  requirement: !ruby/object:Gem::Requirement
61
58
  requirements:
62
- - - ~>
59
+ - - ! '>='
63
60
  - !ruby/object:Gem::Version
64
- version: '0.9'
61
+ version: !binary |-
62
+ MA==
65
63
  none: false
66
64
  prerelease: false
67
65
  type: :development
@@ -71,13 +69,15 @@ dependencies:
71
69
  requirements:
72
70
  - - ! '>='
73
71
  - !ruby/object:Gem::Version
74
- version: 2.3.0
72
+ version: !binary |-
73
+ MA==
75
74
  none: false
76
75
  requirement: !ruby/object:Gem::Requirement
77
76
  requirements:
78
77
  - - ! '>='
79
78
  - !ruby/object:Gem::Version
80
- version: 2.3.0
79
+ version: !binary |-
80
+ MA==
81
81
  none: false
82
82
  prerelease: false
83
83
  type: :development
@@ -141,13 +141,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
141
  requirements:
142
142
  - - ! '>='
143
143
  - !ruby/object:Gem::Version
144
- version: '0'
144
+ version: !binary |-
145
+ MA==
145
146
  none: false
146
147
  required_rubygems_version: !ruby/object:Gem::Requirement
147
148
  requirements:
148
149
  - - ! '>='
149
150
  - !ruby/object:Gem::Version
150
- version: '0'
151
+ version: !binary |-
152
+ MA==
151
153
  none: false
152
154
  requirements: []
153
155
  rubyforge_project:
@@ -164,4 +166,3 @@ test_files:
164
166
  - test/test_object_wrapper.rb
165
167
  - test/test_sql.rb
166
168
  - test/testrb
167
- ...