ruby-oci8 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,463 @@
1
+ require 'oci8'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/config'
4
+
5
+ conn = OCI8.new($dbuser, $dbpass, $dbname)
6
+ error_message = nil
7
+ begin
8
+ conn.describe_type('rb_test_int_array')
9
+ conn.describe_type('rb_test_flt_array')
10
+ conn.describe_type('rb_test_num_array')
11
+ conn.describe_type('rb_test_bdbl_array')
12
+ conn.describe_type('rb_test_bflt_array')
13
+ conn.describe_type('rb_test_str_array')
14
+ conn.describe_type('rb_test_raw_array')
15
+ conn.describe_type('rb_test_obj_elem_array')
16
+ conn.describe_type('rb_test_obj_elem_ary_of_ary')
17
+ conn.describe_type('rb_test_obj')
18
+ conn.describe_table('rb_test_obj_tab1')
19
+ conn.describe_table('rb_test_obj_tab2')
20
+
21
+ class RbTestObj < OCI8::Object::Base
22
+ end
23
+
24
+ begin
25
+ version = RbTestObj.test_object_version(conn)
26
+ error_message = "Invalid test object version" if version != 2
27
+ rescue NoMethodError
28
+ raise unless $!.to_s.include?('test_object_version')
29
+ error_message = "rb_test_obj.test_object_version is not declared."
30
+ end
31
+ rescue OCIError
32
+ raise if $!.code != 4043
33
+ error_message = $!.to_s
34
+ ensure
35
+ conn.logoff
36
+ end
37
+
38
+ raise <<EOS if error_message
39
+
40
+ #{error_message}
41
+ You need to execute SQL statements in #{File.dirname(__FILE__)}/setup_test_object.sql as follows:
42
+
43
+ $ sqlplus USERNAME/PASSWORD
44
+ SQL> @test/setup_test_object.sql
45
+
46
+ EOS
47
+
48
+
49
+ class RbTestIntArray < OCI8::Object::Base
50
+ end
51
+
52
+ class TestObj1 < Test::Unit::TestCase
53
+ Delta = 0.00001
54
+
55
+ def setup
56
+ @conn = get_oci8_connection
57
+ RbTestObj.default_connection = @conn
58
+ end
59
+
60
+ def teardown
61
+ @conn.logoff
62
+ end
63
+
64
+ class ExpectedValObjElem
65
+ def initialize(x, y)
66
+ @attributes = {:x => x, :y => y}
67
+ end
68
+ def ==(val)
69
+ @attributes[:x] == val.x and @attributes[:y] == val.y
70
+ end
71
+ end
72
+
73
+ class ExpectedVal
74
+ include Test::Unit::Assertions
75
+
76
+ attr_reader :n
77
+ attr_reader :int_val
78
+ attr_reader :flt_val
79
+ attr_reader :num_val
80
+ attr_reader :bdbl_val
81
+ attr_reader :bflt_val
82
+ attr_reader :str_val
83
+ attr_reader :raw_val
84
+ attr_reader :clob_val
85
+ attr_reader :nclob_val
86
+ attr_reader :blob_val
87
+ attr_reader :obj_val
88
+ attr_reader :int_array_val
89
+ attr_reader :flt_array_val
90
+ attr_reader :num_array_val
91
+ attr_reader :bdbl_array_val
92
+ attr_reader :bflt_array_val
93
+ attr_reader :str_array_val
94
+ attr_reader :raw_array_val
95
+ attr_reader :obj_array_val
96
+ attr_reader :obj_ary_of_ary_val
97
+ attr_reader :date_val
98
+ # attr_reader :date_array_val
99
+
100
+ def initialize
101
+ @n = 0.0
102
+ end
103
+
104
+ def to_test_date(n)
105
+ year = (1990 + n).round
106
+ month = (n.round * 5) % 12 + 1
107
+ mday = (n.round * 7) % 27 + 1
108
+ hour = (n.round * 9) % 24
109
+ minute = (n.round * 11) % 60
110
+ sec = (n.round * 13) % 60
111
+ convert_to_time(year, month, mday, hour, minute, sec, 0, nil)
112
+ end
113
+ private :to_test_date
114
+
115
+ def next
116
+ @n += 1.2
117
+ @n = (@n * 10).round / 10.0
118
+ @int_val = @n.round
119
+ @flt_val = @n
120
+ @num_val = @n
121
+ @bdbl_val = @n
122
+ @bflt_val = @n
123
+ @str_val = @n.to_s
124
+ @str_val = $` if /.0$/ =~ @str_val
125
+ @raw_val = @str_val
126
+ @clob_val = @str_val
127
+ @nclob_val = @str_val
128
+ @blob_val = @str_val
129
+ @obj_val = ExpectedValObjElem.new(@int_val, @int_val + 1)
130
+ @date_val = to_test_date(@n)
131
+ if @int_val == 1
132
+ @int_array_val = nil
133
+ @flt_array_val = nil
134
+ @num_array_val = nil
135
+ @bdbl_array_val = nil
136
+ @bflt_array_val = nil
137
+ @str_array_val = nil
138
+ @raw_array_val = nil
139
+ @obj_array_val = nil
140
+ @obj_ary_of_ary_val = nil
141
+ # @date_array_val = nil
142
+ else
143
+ @int_array_val = []
144
+ @flt_array_val = []
145
+ @num_array_val = []
146
+ @bdbl_array_val = []
147
+ @bflt_array_val = []
148
+ @str_array_val = []
149
+ @raw_array_val = []
150
+ @obj_array_val = []
151
+ @obj_ary_of_ary_val = []
152
+ # @date_array_val = []
153
+ 0.upto(2) do |i|
154
+ ival = @n.round
155
+ val = (@n + i).to_s
156
+ val = $` if /.0$/ =~ val
157
+ @int_array_val[i] = @int_val + i
158
+ @flt_array_val[i] = @n + i
159
+ @num_array_val[i] = @n + i
160
+ @bdbl_array_val[i] = @n + i
161
+ @bflt_array_val[i] = @n + i
162
+ @str_array_val[i] = val
163
+ @raw_array_val[i] = val
164
+ @obj_array_val[i] = ExpectedValObjElem.new(@int_val + i, @int_val + i + 1)
165
+ # @date_array_val[i] = to_test_date(@n + i)
166
+ end
167
+ @obj_ary_of_ary_val[0] = @obj_array_val
168
+ end
169
+ @n <= 20
170
+ end
171
+
172
+ def should_be_equal(val)
173
+ if val.is_a? Array
174
+ int_val = val[0]
175
+ flt_val = val[1]
176
+ num_val = val[2]
177
+ bdbl_val = val[3]
178
+ bflt_val = val[4]
179
+ str_val = val[5]
180
+ raw_val = val[6]
181
+ clob_val = val[7] && val[7].read
182
+ nclob_val = val[8] && val[8].read
183
+ blob_val = val[9] && val[9].read
184
+ obj_val = val[10]
185
+ int_array_val = val[11]
186
+ flt_array_val = val[12]
187
+ num_array_val = val[13]
188
+ bdbl_array_val = val[14]
189
+ bflt_array_val = val[15]
190
+ str_array_val = val[16]
191
+ raw_array_val = val[17]
192
+ obj_array_val = val[18]
193
+ obj_ary_of_ary_val = val[19]
194
+ date_val = val[20]
195
+ # date_array_val = val[18]
196
+ else
197
+ assert_instance_of(RbTestObj, val)
198
+ int_val = val.int_val
199
+ flt_val = val.flt_val
200
+ num_val = val.num_val
201
+ bdbl_val = val.bdbl_val
202
+ bflt_val = val.bflt_val
203
+ str_val = val.str_val
204
+ raw_val = val.raw_val
205
+ clob_val = val.clob_val && val.clob_val.read
206
+ nclob_val = val.nclob_val && val.nclob_val.read
207
+ blob_val = val.blob_val && val.blob_val.read
208
+ obj_val = val.obj_val
209
+ int_array_val = val.int_array_val
210
+ flt_array_val = val.flt_array_val
211
+ num_array_val = val.num_array_val
212
+ bdbl_array_val = val.bdbl_array_val
213
+ bflt_array_val = val.bflt_array_val
214
+ str_array_val = val.str_array_val
215
+ raw_array_val = val.raw_array_val
216
+ obj_array_val = val.obj_array_val
217
+ obj_ary_of_ary_val = val.obj_ary_of_ary_val
218
+ date_val = val.date_val
219
+ # date_array_val = val.date_array_val
220
+ end
221
+
222
+ assert_equal(@int_val, int_val)
223
+ assert_in_delta(@flt_val, flt_val, Delta)
224
+ assert_in_delta(@num_val, num_val, Delta)
225
+ assert_in_delta(@bdbl_val, bdbl_val, Delta)
226
+ assert_in_delta(@bflt_val, bflt_val, Delta)
227
+ assert_equal(@str_val, str_val)
228
+ assert_equal(@raw_val, raw_val)
229
+ assert_equal(@clob_val, clob_val)
230
+ assert_equal(@nclob_val, nclob_val)
231
+ assert_equal(@blob_val, blob_val)
232
+ assert_equal(@obj_val, obj_val)
233
+ assert_equal(@int_array_val, int_array_val && int_array_val.to_ary)
234
+ assert_array_in_delta(@flt_array_val, flt_array_val && flt_array_val.to_ary)
235
+ assert_array_in_delta(@num_array_val, num_array_val && num_array_val.to_ary)
236
+ assert_array_in_delta(@bdbl_array_val, bdbl_array_val && bdbl_array_val.to_ary)
237
+ assert_array_in_delta(@bflt_array_val, bflt_array_val && bflt_array_val.to_ary)
238
+ assert_equal(@str_array_val, str_array_val && str_array_val.to_ary)
239
+ assert_equal(@raw_array_val, raw_array_val && raw_array_val.to_ary)
240
+ assert_equal(@obj_array_val, obj_array_val && obj_array_val.to_ary)
241
+ assert_equal(@obj_ary_of_ary_val, obj_ary_of_ary_val && obj_ary_of_ary_val.to_ary.collect { |elem| elem.to_ary })
242
+ assert_equal(@date_val, date_val)
243
+ # assert_equal(@date_array_val, date_array_val && date_array_val.to_ary)
244
+ end
245
+
246
+ def assert_array_in_delta(exp, val)
247
+ if exp && val
248
+ assert_equal(exp.size, val.size)
249
+ exp.each_with_index do |elem, idx|
250
+ assert_in_delta(elem, val[idx], Delta)
251
+ end
252
+ else
253
+ assert_equal(exp, val)
254
+ end
255
+ end
256
+ end
257
+
258
+ def test_select1
259
+ expected_val = ExpectedVal.new
260
+ @conn.exec("select * from rb_test_obj_tab1 order by n") do |row|
261
+ assert(expected_val.next)
262
+
263
+ assert_in_delta(expected_val.n, row[0], Delta)
264
+ expected_val.should_be_equal(row[1])
265
+ end
266
+ assert(!expected_val.next)
267
+ end
268
+
269
+ def test_select2
270
+ expected_val = ExpectedVal.new
271
+ orig_val = OCI8::BindType::Mapping[:date]
272
+ begin
273
+ @conn.exec("select * from rb_test_obj_tab2 order by int_val") do |row|
274
+ assert(expected_val.next)
275
+ expected_val.should_be_equal(row)
276
+ end
277
+ assert(!expected_val.next)
278
+ ensure
279
+ OCI8::BindType::Mapping[:date] = orig_val
280
+ end
281
+ end
282
+
283
+ def test_select3
284
+ expected_val = ExpectedVal.new
285
+ @conn.exec("select value(p) from rb_test_obj_tab2 p order by int_val") do |row|
286
+ assert(expected_val.next)
287
+ expected_val.should_be_equal(row[0])
288
+ end
289
+ assert(!expected_val.next)
290
+ end
291
+
292
+ def _test_select4 # TODO
293
+ expected_val = ExpectedVal.new
294
+ @conn.exec("select ref(p) from rb_test_obj_tab2 p order by int_val") do |row|
295
+ assert(expected_val.next)
296
+
297
+ expected_val.should_be_equal(row[0])
298
+ end
299
+ assert(!expected_val.next)
300
+ end
301
+
302
+ def test_explicit_constructor
303
+ expected_val = ExpectedVal.new
304
+ while expected_val.next
305
+ obj = RbTestObj.new(expected_val.n)
306
+ expected_val.should_be_equal(obj)
307
+ assert_nothing_raised do
308
+ obj.inspect
309
+ end
310
+ end
311
+ end
312
+
313
+ def _test_implicit_constructor # TODO
314
+ expected_val = ExpectedVal.new
315
+ while expected_val.next
316
+ obj = RbTestObj.new(expected_val.int_val, expected_val.flt_val, expected_val.str_val, expected_val.raw_val, expected_val.str_array_val, expected_val.raw_array_val, expected_val.num_array_val)
317
+ expected_val.should_be_equal(obj)
318
+ end
319
+ end
320
+
321
+ def _test_implicit_constructor2 # TODO
322
+ obj = nil
323
+ assert_nothing_raised do
324
+ obj = RbTestObj.new(nil, nil, nil)
325
+ end
326
+ assert_nil(obj.int_val)
327
+ assert_nil(obj.flt_val)
328
+ assert_nil(obj.str_val)
329
+ end
330
+
331
+ def _test_class_func
332
+ expected_val = ExpectedVal.new
333
+ while expected_val.next
334
+ obj = RbTestObj.class_func(expected_val.n)
335
+ expected_val.should_be_equal(obj)
336
+ end
337
+ end
338
+
339
+ def test_class_proc1
340
+ expected_val = ExpectedVal.new
341
+ while expected_val.next
342
+ obj = RbTestObj.new(0)
343
+ RbTestObj.class_proc1(obj, expected_val.n)
344
+ expected_val.should_be_equal(obj)
345
+ end
346
+ end
347
+
348
+ def _test_class_proc2
349
+ expected_val = ExpectedVal.new
350
+ while expected_val.next
351
+ obj = RbTestObj.new
352
+ obj.int_val = expected_val.int_val - 1
353
+ obj.flt_val = expected_val.flt_val
354
+ obj.num_val = expected_val.num_val
355
+ obj.bdbl_val = expected_val.bdbl_val
356
+ obj.bflt_val = expected_val.bflt_val
357
+ obj.str_val = expected_val.str_val
358
+ obj.raw_val = expected_val.raw_val
359
+ obj.clob_val = expected_val.clob_val
360
+ obj.nclob_val = expected_val.nclob_val
361
+ obj.blob_val = expected_val.blob_val
362
+ obj.obj_val = expected_val.obj_val
363
+ obj.int_array_val = expected_val.int_array_val
364
+ obj.flt_array_val = expected_val.flt_array_val
365
+ obj.num_array_val = expected_val.num_array_val
366
+ obj.bdbl_array_val = expected_val.bdbl_array_val
367
+ obj.bflt_array_val = expected_val.bflt_array_val
368
+ obj.str_array_val = expected_val.str_array_val
369
+ obj.raw_array_val = expected_val.raw_array_val
370
+ obj.obj_array_val = expected_val.obj_array_val
371
+ obj.obj_ary_of_ary_val = expected_val.obj_ary_of_ary_val
372
+ RbTestObj.class_proc2(obj)
373
+ expected_val.should_be_equal(obj)
374
+ end
375
+ end
376
+
377
+ def test_member_func
378
+ expected_val = ExpectedVal.new
379
+ while expected_val.next
380
+ obj = RbTestObj.new(expected_val.n)
381
+ assert_equal(expected_val.int_val, obj.member_func)
382
+ end
383
+ end
384
+
385
+ def _test_plsql_member_func
386
+ expected_val = ExpectedVal.new
387
+ while expected_val.next
388
+ obj = RbTestObj.new(expected_val.n)
389
+ rv = @conn.exec(<<EOS, obj, [nil, Integer])
390
+ declare
391
+ obj rb_test_obj := :obj;
392
+ begin
393
+ :rv := obj.member_func;
394
+ end;
395
+ EOS
396
+ assert_equal(expected_val.int_val, rv[1])
397
+ end
398
+ end
399
+
400
+ def test_member_proc
401
+ expected_val = ExpectedVal.new
402
+ while expected_val.next
403
+ obj = RbTestObj.new
404
+ obj.member_proc(expected_val.int_val)
405
+ assert_equal(expected_val.int_val, obj.int_val)
406
+ end
407
+ end
408
+
409
+ def test_bind_nil
410
+ csr = @conn.parse(<<EOS)
411
+ DECLARE
412
+ obj RB_TEST_OBJ := :in;
413
+ BEGIN
414
+ IF obj IS NULL THEN
415
+ :out := 'IS NULL';
416
+ ELSE
417
+ :out := 'IS NOT NULL';
418
+ END IF;
419
+ END;
420
+ EOS
421
+ csr.bind_param(:in, nil, RbTestObj)
422
+ csr.bind_param(:out, nil, String, 11)
423
+ csr.exec
424
+ assert_equal('IS NULL', csr[:out])
425
+ csr[:in] = RbTestObj.new(@conn)
426
+ csr.exec
427
+ assert_equal('IS NOT NULL', csr[:out])
428
+ csr[:in] = nil
429
+ csr.exec
430
+ assert_equal('IS NULL', csr[:out])
431
+ end
432
+
433
+ def test_bind_array
434
+ csr = @conn.parse <<EOS
435
+ DECLARE
436
+ ary RB_TEST_INT_ARRAY := :in;
437
+ BEGIN
438
+ IF ary IS NULL THEN
439
+ :cnt := -1;
440
+ ELSE
441
+ :cnt := ary.count;
442
+ IF :cnt != 0 THEN
443
+ :out1 := ary(1);
444
+ :out2 := ary(2);
445
+ :out3 := ary(3);
446
+ END IF;
447
+ END IF;
448
+ END;
449
+ EOS
450
+ [nil, [], [1, nil, 3]].each do |ary|
451
+ csr.bind_param(:in, ary, RbTestIntArray)
452
+ csr.bind_param(:cnt, nil, Integer)
453
+ csr.bind_param(:out1, nil, Integer)
454
+ csr.bind_param(:out2, nil, Integer)
455
+ csr.bind_param(:out3, nil, Integer)
456
+ csr.exec
457
+ assert_equal(ary ? ary.length : -1, csr[:cnt])
458
+ assert_equal(ary ? ary[0] : nil, csr[:out1])
459
+ assert_equal(ary ? ary[1] : nil, csr[:out2])
460
+ assert_equal(ary ? ary[2] : nil, csr[:out3])
461
+ end
462
+ end
463
+ end