flash-gordons-ruby-plsql 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,505 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "Package variables /" do
6
+
7
+ describe "String" do
8
+ before(:all) do
9
+ plsql.connect! CONNECTION_PARAMS
10
+ plsql.execute <<-SQL
11
+ CREATE OR REPLACE PACKAGE test_package IS
12
+ varchar2_variable VARCHAR2(50);
13
+ varchar2_variable2 VARCHAR2(50); -- some comment
14
+ varchar2_default varchar2(50) := 'default' ;
15
+ varchar2_default2 varchar2(50) DEFAULT 'default';
16
+ varchar2_default3 varchar2(50) NOT NULL := 'default';
17
+ varchar2_3_char VARCHAR2(3 CHAR);
18
+ varchar2_3_byte VARCHAR2(3 BYTE);
19
+ char_variable char(10) ;
20
+ nvarchar2_variable NVARCHAR2(50);
21
+ nchar_variable NCHAR(10);
22
+ END;
23
+ SQL
24
+ plsql.execute <<-SQL
25
+ CREATE OR REPLACE PACKAGE BODY test_package IS
26
+ END;
27
+ SQL
28
+
29
+ end
30
+
31
+ after(:all) do
32
+ plsql.execute "DROP PACKAGE test_package"
33
+ plsql.logoff
34
+ end
35
+
36
+ it "should set and get VARCHAR2 variable" do
37
+ plsql.test_package.varchar2_variable = 'abc'
38
+ plsql.test_package.varchar2_variable.should == 'abc'
39
+ end
40
+
41
+ it "should set and get VARCHAR2 variable with comment" do
42
+ plsql.test_package.varchar2_variable2 = 'abc'
43
+ plsql.test_package.varchar2_variable2.should == 'abc'
44
+ end
45
+
46
+ it "should get VARCHAR2 variable default value" do
47
+ plsql.test_package.varchar2_default.should == 'default'
48
+ plsql.test_package.varchar2_default2.should == 'default'
49
+ plsql.test_package.varchar2_default3.should == 'default'
50
+ end
51
+
52
+ describe "with character or byte limit" do
53
+ before(:each) do
54
+ if !defined?(JRUBY_VERSION) && OCI8.properties.has_key?(:length_semantics)
55
+ @original_length_semantics = OCI8.properties[:length_semantics]
56
+ OCI8.properties[:length_semantics] = :char
57
+ end
58
+ end
59
+
60
+ after(:each) do
61
+ if !defined?(JRUBY_VERSION) && OCI8.properties.has_key?(:length_semantics)
62
+ OCI8.properties[:length_semantics] = @original_length_semantics
63
+ end
64
+ end
65
+
66
+ it "should set and get VARCHAR2(n CHAR) variable" do
67
+ plsql.test_package.varchar2_3_char = 'āčē'
68
+ plsql.test_package.varchar2_3_char.should == 'āčē'
69
+ lambda { plsql.test_package.varchar2_3_char = 'aceg' }.should raise_error(/buffer too small/)
70
+ end
71
+
72
+ it "should set and get VARCHAR2(n BYTE) variable" do
73
+ plsql.test_package.varchar2_3_byte = 'ace'
74
+ plsql.test_package.varchar2_3_byte.should == 'ace'
75
+ lambda { plsql.test_package.varchar2_3_byte = 'āce' }.should raise_error(/buffer too small/)
76
+ lambda { plsql.test_package.varchar2_3_byte = 'aceg' }.should raise_error(/buffer too small/)
77
+ end
78
+
79
+ end
80
+
81
+ it "should set and get CHAR variable" do
82
+ plsql.test_package.char_variable = 'abc'
83
+ plsql.test_package.char_variable.should == 'abc' + ' '*7
84
+ end
85
+
86
+ it "should set and get NVARCHAR2 variable" do
87
+ plsql.test_package.nvarchar2_variable = 'abc'
88
+ plsql.test_package.nvarchar2_variable.should == 'abc'
89
+ end
90
+
91
+ it "should set and get NCHAR variable" do
92
+ plsql.test_package.nchar_variable = 'abc'
93
+ plsql.test_package.nchar_variable.should == 'abc' + ' '*7
94
+ end
95
+
96
+ end
97
+
98
+ describe "Numeric" do
99
+ before(:all) do
100
+ plsql.connect! CONNECTION_PARAMS
101
+ plsql.execute <<-SQL
102
+ CREATE OR REPLACE PACKAGE test_package IS
103
+ integer_variable INTEGER;
104
+ integer10_variable NUMBER(10);
105
+ integer10_default NUMBER(10) := 1;
106
+ number_variable NUMBER;
107
+ number_with_scale NUMBER(15,2);
108
+ pls_int_variable PLS_INTEGER;
109
+ bin_int_variable BINARY_INTEGER;
110
+ END;
111
+ SQL
112
+ plsql.execute <<-SQL
113
+ CREATE OR REPLACE PACKAGE BODY test_package IS
114
+ END;
115
+ SQL
116
+
117
+ end
118
+
119
+ after(:all) do
120
+ plsql.execute "DROP PACKAGE test_package"
121
+ plsql.logoff
122
+ end
123
+
124
+ it "should set and get INTEGER variable" do
125
+ plsql.test_package.integer_variable = 1
126
+ plsql.test_package.integer_variable.should be_a Fixnum
127
+ plsql.test_package.integer_variable.should == 1
128
+ end
129
+
130
+ it "should set and get integer variable with precision" do
131
+ plsql.test_package.integer10_variable = 1
132
+ plsql.test_package.integer10_variable.should be_a Fixnum
133
+ plsql.test_package.integer10_variable.should == 1
134
+ end
135
+
136
+ it "should get integer variable default value" do
137
+ plsql.test_package.integer10_default.should == 1
138
+ end
139
+
140
+ it "should set and get PLS_INTEGER variable" do
141
+ plsql.test_package.pls_int_variable = 1
142
+ plsql.test_package.pls_int_variable.should be_a Fixnum
143
+ plsql.test_package.pls_int_variable.should == 1
144
+ end
145
+
146
+ it "should set and get BINARY_INTEGER variable" do
147
+ plsql.test_package.bin_int_variable = 1
148
+ plsql.test_package.bin_int_variable.should be_a Fixnum
149
+ plsql.test_package.bin_int_variable.should == 1
150
+ end
151
+
152
+ it "should set and get NUMBER variable" do
153
+ plsql.test_package.number_variable = 123.456
154
+ plsql.test_package.number_variable.should be_a BigDecimal
155
+ plsql.test_package.number_variable.should == 123.456
156
+ end
157
+
158
+ it "should set and get NUMBER variable with scale" do
159
+ plsql.test_package.number_with_scale = 123.456
160
+ plsql.test_package.number_with_scale.should == 123.46 # rounding to two decimal digits
161
+ end
162
+
163
+ end
164
+
165
+ describe "Date and Time" do
166
+ before(:all) do
167
+ plsql.connect! CONNECTION_PARAMS
168
+ plsql.execute <<-SQL
169
+ CREATE OR REPLACE PACKAGE test_package IS
170
+ date_variable DATE;
171
+ date_default DATE := TO_DATE('2009-12-21', 'YYYY-MM-DD');
172
+ timestamp_variable TIMESTAMP;
173
+ timestamptz_variable TIMESTAMP WITH TIME ZONE;
174
+ timestampltz_variable TIMESTAMP WITH LOCAL TIME ZONE;
175
+ END;
176
+ SQL
177
+ plsql.execute <<-SQL
178
+ CREATE OR REPLACE PACKAGE BODY test_package IS
179
+ END;
180
+ SQL
181
+ @date = Time.local(2009,12,21)
182
+ @timestamp = Time.local(2009,12,21,14,10,30,11)
183
+ end
184
+
185
+ after(:all) do
186
+ plsql.execute "DROP PACKAGE test_package"
187
+ plsql.logoff
188
+ end
189
+
190
+ it "should set and get DATE variable" do
191
+ plsql.test_package.date_variable = @date
192
+ plsql.test_package.date_variable.should be_a Time
193
+ plsql.test_package.date_variable.should == @date
194
+ end
195
+
196
+ it "should get DATE variable default value" do
197
+ plsql.test_package.date_default.should == @date
198
+ end
199
+
200
+ it "should set and get TIMESTAMP variable" do
201
+ plsql.test_package.timestamp_variable = @timestamp
202
+ plsql.test_package.timestamp_variable.should be_a Time
203
+ plsql.test_package.timestamp_variable.should == @timestamp
204
+ end
205
+
206
+ it "should set and get TIMESTAMP WITH TIME ZONE variable" do
207
+ plsql.test_package.timestamptz_variable = @timestamp
208
+ plsql.test_package.timestamptz_variable.should be_a Time
209
+ plsql.test_package.timestamptz_variable.should == @timestamp
210
+ end
211
+
212
+ it "should set and get TIMESTAMP WITH LOCAL TIME ZONE variable" do
213
+ plsql.test_package.timestampltz_variable = @timestamp
214
+ plsql.test_package.timestampltz_variable.should be_a Time
215
+ plsql.test_package.timestampltz_variable.should == @timestamp
216
+ end
217
+
218
+ end
219
+
220
+ describe "LOB" do
221
+ before(:all) do
222
+ plsql.connect! CONNECTION_PARAMS
223
+ plsql.execute <<-SQL
224
+ CREATE OR REPLACE PACKAGE test_package IS
225
+ clob_variable CLOB;
226
+ clob_default CLOB := 'default';
227
+ nclob_variable CLOB;
228
+ blob_variable BLOB;
229
+ END;
230
+ SQL
231
+ plsql.execute <<-SQL
232
+ CREATE OR REPLACE PACKAGE BODY test_package IS
233
+ END;
234
+ SQL
235
+
236
+ end
237
+
238
+ after(:all) do
239
+ plsql.execute "DROP PACKAGE test_package"
240
+ plsql.logoff
241
+ end
242
+
243
+ it "should set and get CLOB variable" do
244
+ plsql.test_package.clob_variable = 'abc'
245
+ plsql.test_package.clob_variable.should == 'abc'
246
+ end
247
+
248
+ it "should get CLOB variable default value" do
249
+ plsql.test_package.clob_default.should == 'default'
250
+ end
251
+
252
+ it "should set and get NCLOB variable" do
253
+ plsql.test_package.nclob_variable = 'abc'
254
+ plsql.test_package.nclob_variable.should == 'abc'
255
+ end
256
+
257
+ it "should set and get BLOB variable" do
258
+ plsql.test_package.blob_variable = "\000\001\003"
259
+ plsql.test_package.blob_variable.should == "\000\001\003"
260
+ end
261
+
262
+ end
263
+
264
+ describe "table column type" do
265
+ before(:all) do
266
+ plsql.connect! CONNECTION_PARAMS
267
+ plsql.execute <<-SQL
268
+ CREATE TABLE test_employees (
269
+ employee_id NUMBER(15),
270
+ first_name VARCHAR2(50),
271
+ last_name VARCHAR2(50),
272
+ hire_date DATE
273
+ )
274
+ SQL
275
+
276
+ plsql.execute <<-SQL
277
+ CREATE OR REPLACE PACKAGE test_package IS
278
+ employee_id test_employees.employee_id%TYPE;
279
+ first_name test_employees.first_name%TYPE;
280
+ hire_date test_employees.hire_date%TYPE;
281
+ END;
282
+ SQL
283
+ plsql.execute <<-SQL
284
+ CREATE OR REPLACE PACKAGE BODY test_package IS
285
+ END;
286
+ SQL
287
+
288
+ end
289
+
290
+ after(:all) do
291
+ plsql.execute "DROP PACKAGE test_package"
292
+ plsql.execute "DROP TABLE test_employees"
293
+ plsql.logoff
294
+ end
295
+
296
+ it "should set and get NUMBER variable" do
297
+ plsql.test_package.employee_id = 1
298
+ plsql.test_package.employee_id.should == 1
299
+ end
300
+
301
+ it "should set and get VARCHAR2 variable" do
302
+ plsql.test_package.first_name = 'First'
303
+ plsql.test_package.first_name.should == 'First'
304
+ end
305
+
306
+ it "should set and get DATE variable" do
307
+ today = Time.local(2009,12,22)
308
+ plsql.test_package.hire_date = today
309
+ plsql.test_package.hire_date.should == today
310
+ end
311
+
312
+ end
313
+
314
+ describe "constants" do
315
+ before(:all) do
316
+ plsql.connect! CONNECTION_PARAMS
317
+ plsql.execute <<-SQL
318
+ CREATE OR REPLACE PACKAGE test_package IS
319
+ integer_constant CONSTANT NUMBER(1) := 1;
320
+ string_constant CONSTANT VARCHAR2(10) := 'constant';
321
+ END;
322
+ SQL
323
+ plsql.execute <<-SQL
324
+ CREATE OR REPLACE PACKAGE BODY test_package IS
325
+ END;
326
+ SQL
327
+
328
+ end
329
+
330
+ after(:all) do
331
+ plsql.execute "DROP PACKAGE test_package"
332
+ plsql.logoff
333
+ end
334
+
335
+ it "should get NUMBER constant" do
336
+ plsql.test_package.integer_constant.should == 1
337
+ end
338
+
339
+ it "should get VARCHAR2 constant" do
340
+ plsql.test_package.string_constant.should == 'constant'
341
+ end
342
+
343
+ it "should raise error when trying to set constant" do
344
+ lambda {
345
+ plsql.test_package.integer_constant = 2
346
+ }.should raise_error(/PLS-00363/)
347
+ end
348
+
349
+ end
350
+
351
+ describe "object type" do
352
+ before(:all) do
353
+ plsql.connect! CONNECTION_PARAMS
354
+ plsql.execute "DROP TYPE t_employee" rescue nil
355
+ plsql.execute "DROP TYPE t_phones" rescue nil
356
+ plsql.execute <<-SQL
357
+ CREATE OR REPLACE TYPE t_address AS OBJECT (
358
+ street VARCHAR2(50),
359
+ city VARCHAR2(50),
360
+ country VARCHAR2(50)
361
+ )
362
+ SQL
363
+ plsql.execute <<-SQL
364
+ CREATE OR REPLACE TYPE t_phone AS OBJECT (
365
+ type VARCHAR2(10),
366
+ phone_number VARCHAR2(50)
367
+ )
368
+ SQL
369
+ plsql.execute <<-SQL
370
+ CREATE OR REPLACE TYPE t_phones AS TABLE OF T_PHONE
371
+ SQL
372
+ plsql.execute <<-SQL
373
+ CREATE OR REPLACE TYPE t_employee AS OBJECT (
374
+ employee_id NUMBER(15),
375
+ first_name VARCHAR2(50),
376
+ last_name VARCHAR2(50),
377
+ hire_date DATE,
378
+ address t_address,
379
+ phones t_phones
380
+ )
381
+ SQL
382
+ @phones = [{:type => 'mobile', :phone_number => '123456'}, {:type => 'home', :phone_number => '654321'}]
383
+ @employee = {
384
+ :employee_id => 1,
385
+ :first_name => 'First',
386
+ :last_name => 'Last',
387
+ :hire_date => Time.local(2000,01,31),
388
+ :address => {:street => 'Main street 1', :city => 'Riga', :country => 'Latvia'},
389
+ :phones => @phones
390
+ }
391
+
392
+ plsql.execute <<-SQL
393
+ CREATE OR REPLACE PACKAGE test_package IS
394
+ g_employee t_employee;
395
+ g_employee2 hr.t_employee;
396
+ g_phones t_phones;
397
+ END;
398
+ SQL
399
+ plsql.execute <<-SQL
400
+ CREATE OR REPLACE PACKAGE BODY test_package IS
401
+ END;
402
+ SQL
403
+
404
+ end
405
+
406
+ after(:all) do
407
+ plsql.execute "DROP PACKAGE test_package"
408
+ plsql.execute "DROP TYPE t_employee"
409
+ plsql.execute "DROP TYPE t_address"
410
+ plsql.execute "DROP TYPE t_phones"
411
+ plsql.execute "DROP TYPE t_phone"
412
+ plsql.logoff
413
+ end
414
+
415
+ it "should set and get object type variable" do
416
+ plsql.test_package.g_employee = @employee
417
+ plsql.test_package.g_employee.should == @employee
418
+ end
419
+
420
+ it "should set and get object type variable when schema prefix is used with type" do
421
+ plsql.hr.test_package.g_employee2 = @employee
422
+ plsql.hr.test_package.g_employee2.should == @employee
423
+ end
424
+
425
+ it "should set and get collection type variable" do
426
+ plsql.test_package.g_phones = @phones
427
+ plsql.test_package.g_phones.should == @phones
428
+ end
429
+
430
+ end
431
+
432
+ describe "table row type" do
433
+ before(:all) do
434
+ plsql.connect! CONNECTION_PARAMS
435
+ plsql.execute <<-SQL
436
+ CREATE TABLE test_employees (
437
+ employee_id NUMBER(15),
438
+ first_name VARCHAR2(50),
439
+ last_name VARCHAR2(50),
440
+ hire_date DATE
441
+ )
442
+ SQL
443
+ @employee = {
444
+ :employee_id => 1,
445
+ :first_name => 'First',
446
+ :last_name => 'Last',
447
+ :hire_date => Time.local(2000,01,31)
448
+ }
449
+
450
+ plsql.execute <<-SQL
451
+ CREATE OR REPLACE PACKAGE test_package IS
452
+ g_employee test_employees%ROWTYPE;
453
+ END;
454
+ SQL
455
+ plsql.execute <<-SQL
456
+ CREATE OR REPLACE PACKAGE BODY test_package IS
457
+ END;
458
+ SQL
459
+
460
+ end
461
+
462
+ after(:all) do
463
+ plsql.execute "DROP PACKAGE test_package"
464
+ plsql.execute "DROP TABLE test_employees"
465
+ plsql.logoff
466
+ end
467
+
468
+ it "should set and get table ROWTYPE variable" do
469
+ plsql.test_package.g_employee = @employee
470
+ plsql.test_package.g_employee.should == @employee
471
+ end
472
+
473
+ end
474
+
475
+ describe "booleans" do
476
+ before(:all) do
477
+ plsql.connect! CONNECTION_PARAMS
478
+ plsql.execute <<-SQL
479
+ CREATE OR REPLACE PACKAGE test_package IS
480
+ boolean_variable BOOLEAN;
481
+ END;
482
+ SQL
483
+ plsql.execute <<-SQL
484
+ CREATE OR REPLACE PACKAGE BODY test_package IS
485
+ END;
486
+ SQL
487
+
488
+ end
489
+
490
+ after(:all) do
491
+ plsql.execute "DROP PACKAGE test_package"
492
+ plsql.logoff
493
+ end
494
+
495
+ it "should set and get BOOLEAN variable" do
496
+ plsql.test_package.boolean_variable.should be_nil
497
+ plsql.test_package.boolean_variable = true
498
+ plsql.test_package.boolean_variable.should be_true
499
+ plsql.test_package.boolean_variable = false
500
+ plsql.test_package.boolean_variable.should be_false
501
+ end
502
+
503
+ end
504
+
505
+ end