ruby-plsql 0.4.0 → 0.4.1

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