ruby-plsql 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -34,7 +34,14 @@ describe "Schema connection" do
34
34
 
35
35
  it "should return schema name" do
36
36
  plsql.connection = @conn
37
- plsql.schema_name.should == 'HR'
37
+ plsql.schema_name.should == DATABASE_USERS_AND_PASSWORDS[0][0].upcase
38
+ end
39
+
40
+ it "should return new schema name after reconnection" do
41
+ plsql.connection = @conn
42
+ plsql.schema_name.should == DATABASE_USERS_AND_PASSWORDS[0][0].upcase
43
+ plsql.connection = get_connection(1)
44
+ plsql.schema_name.should == DATABASE_USERS_AND_PASSWORDS[1][0].upcase
38
45
  end
39
46
 
40
47
  it "should return nil schema name if not connected" do
@@ -117,4 +124,4 @@ describe "ActiveRecord connection" do
117
124
  it "should have the same connection as default schema" do
118
125
  plsql.hr.connection.should == plsql.connection
119
126
  end
120
- end
127
+ end
@@ -0,0 +1,67 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Table" do
4
+ before(:all) do
5
+ plsql.connection = get_connection
6
+ plsql.connection.autocommit = false
7
+ plsql.execute "CREATE SEQUENCE test_employees_seq"
8
+ end
9
+
10
+ after(:all) do
11
+ plsql.execute "DROP SEQUENCE test_employees_seq"
12
+ plsql.logoff
13
+ end
14
+
15
+ after(:each) do
16
+ plsql.rollback
17
+ end
18
+
19
+ describe "find" do
20
+
21
+ it "should find existing sequence" do
22
+ PLSQL::Sequence.find(plsql, :test_employees_seq).should_not be_nil
23
+ end
24
+
25
+ it "should not find nonexisting table" do
26
+ PLSQL::Sequence.find(plsql, :qwerty123456).should be_nil
27
+ end
28
+
29
+ it "should find existing sequence in schema" do
30
+ plsql.test_employees_seq.should be_a(PLSQL::Sequence)
31
+ end
32
+
33
+ end
34
+
35
+ describe "synonym" do
36
+
37
+ before(:all) do
38
+ plsql.connection.exec "CREATE SYNONYM test_employees_seq_synonym FOR hr.test_employees_seq"
39
+ end
40
+
41
+ after(:all) do
42
+ plsql.connection.exec "DROP SYNONYM test_employees_seq_synonym" rescue nil
43
+ end
44
+
45
+ it "should find synonym to sequence" do
46
+ PLSQL::Sequence.find(plsql, :test_employees_seq_synonym).should_not be_nil
47
+ end
48
+
49
+ it "should find sequence using synonym in schema" do
50
+ plsql.test_employees_seq_synonym.should be_a(PLSQL::Sequence)
51
+ end
52
+
53
+ end
54
+
55
+ describe "values" do
56
+ it "should get next value from sequence" do
57
+ next_value = plsql.select_one "SELECT test_employees_seq.NEXTVAL FROM dual"
58
+ plsql.test_employees_seq.nextval.should == next_value + 1
59
+ end
60
+
61
+ it "should get current value from sequence" do
62
+ next_value = plsql.test_employees_seq.nextval
63
+ plsql.test_employees_seq.currval.should == next_value
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "SQL statements /" do
4
+ before(:all) do
5
+ plsql.connection = get_connection
6
+ plsql.connection.autocommit = false
7
+ end
8
+
9
+ after(:all) do
10
+ plsql.logoff
11
+ end
12
+
13
+ after(:each) do
14
+ plsql.rollback
15
+ end
16
+
17
+ describe "SELECT" do
18
+ before(:all) do
19
+ plsql.execute "DROP TABLE test_employees" rescue nil
20
+ plsql.execute <<-SQL
21
+ CREATE TABLE test_employees (
22
+ employee_id NUMBER(15),
23
+ first_name VARCHAR2(50),
24
+ last_name VARCHAR2(50),
25
+ hire_date DATE
26
+ )
27
+ SQL
28
+ plsql.execute <<-SQL
29
+ CREATE OR REPLACE PROCEDURE test_insert_employee(p_employee test_employees%ROWTYPE)
30
+ IS
31
+ BEGIN
32
+ INSERT INTO test_employees
33
+ VALUES p_employee;
34
+ END;
35
+ SQL
36
+ @employees = (1..10).map do |i|
37
+ {
38
+ :employee_id => i,
39
+ :first_name => "First #{i}",
40
+ :last_name => "Last #{i}",
41
+ :hire_date => Time.local(2000,01,i)
42
+ }
43
+ end
44
+ end
45
+
46
+ before(:each) do
47
+ @employees.each do |e|
48
+ plsql.test_insert_employee(e)
49
+ end
50
+ end
51
+
52
+ after(:all) do
53
+ plsql.execute "DROP PROCEDURE test_insert_employee"
54
+ plsql.execute "DROP TABLE test_employees"
55
+ end
56
+
57
+ it "should select first result" do
58
+ plsql.select(:first, "SELECT * FROM test_employees WHERE employee_id = :employee_id",
59
+ @employees.first[:employee_id]).should == @employees.first
60
+ end
61
+
62
+ it "should select one value" do
63
+ plsql.select_one("SELECT count(*) FROM test_employees").should == @employees.size
64
+ end
65
+
66
+ it "should return nil when selecting non-existing one value" do
67
+ plsql.select_one("SELECT employee_id FROM test_employees WHERE 1=2").should be_nil
68
+ end
69
+
70
+ it "should select all results" do
71
+ plsql.select(:all, "SELECT * FROM test_employees ORDER BY employee_id").should == @employees
72
+ plsql.select("SELECT * FROM test_employees ORDER BY employee_id").should == @employees
73
+ end
74
+
75
+ end
76
+
77
+ # describe "INSERT" do
78
+ # before(:all) do
79
+ # plsql.execute "DROP TABLE test_employees" rescue nil
80
+ # plsql.execute <<-SQL
81
+ # CREATE TABLE test_employees (
82
+ # employee_id NUMBER(15),
83
+ # first_name VARCHAR2(50),
84
+ # last_name VARCHAR2(50),
85
+ # hire_date DATE
86
+ # )
87
+ # SQL
88
+ # @employees = (1..10).map do |i|
89
+ # {
90
+ # :employee_id => i,
91
+ # :first_name => "First #{i}",
92
+ # :last_name => "Last #{i}",
93
+ # :hire_date => Time.local(2000,01,i)
94
+ # }
95
+ # end
96
+ # end
97
+ #
98
+ # after(:all) do
99
+ # plsql.execute "DROP TABLE test_employees"
100
+ # end
101
+ #
102
+ # # it "should insert a record in table" do
103
+ # # plsql.test_employees.insert @employees.first
104
+ # # plsql.test_employees.select(:all).should == [@employees.first]
105
+ # # end
106
+ #
107
+ # end
108
+
109
+ end
@@ -0,0 +1,269 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe "Table" do
4
+ before(:all) do
5
+ plsql.connection = get_connection
6
+ plsql.connection.autocommit = false
7
+ plsql.execute <<-SQL
8
+ CREATE TABLE test_employees (
9
+ employee_id NUMBER(15),
10
+ first_name VARCHAR2(50),
11
+ last_name VARCHAR2(50),
12
+ hire_date DATE
13
+ )
14
+ SQL
15
+
16
+ plsql.connection.exec <<-SQL
17
+ CREATE OR REPLACE TYPE t_address AS OBJECT (
18
+ street VARCHAR2(50),
19
+ city VARCHAR2(50),
20
+ country VARCHAR2(50)
21
+ )
22
+ SQL
23
+ plsql.connection.exec <<-SQL
24
+ CREATE OR REPLACE TYPE t_phone AS OBJECT (
25
+ type VARCHAR2(10),
26
+ phone_number VARCHAR2(50)
27
+ )
28
+ SQL
29
+ plsql.connection.exec <<-SQL
30
+ CREATE OR REPLACE TYPE t_phones AS VARRAY(10) OF T_PHONE
31
+ SQL
32
+ plsql.connection.exec <<-SQL
33
+ CREATE TABLE test_employees2 (
34
+ employee_id NUMBER(15),
35
+ first_name VARCHAR2(50),
36
+ last_name VARCHAR2(50),
37
+ hire_date DATE,
38
+ address t_address,
39
+ phones t_phones
40
+ )
41
+ SQL
42
+ @employees = (1..10).map do |i|
43
+ {
44
+ :employee_id => i,
45
+ :first_name => "First #{i}",
46
+ :last_name => "Last #{i}",
47
+ :hire_date => Time.local(2000,01,i)
48
+ }
49
+ end
50
+ @employees2 = (1..10).map do |i|
51
+ {
52
+ :employee_id => i,
53
+ :first_name => "First #{i}",
54
+ :last_name => "Last #{i}",
55
+ :hire_date => Time.local(2000,01,i),
56
+ :address => {:street => "Street #{i}", :city => "City #{i}", :country => "County #{i}"},
57
+ :phones => [{:type => "mobile", :phone_number => "Mobile#{i}"}, {:type => "fixed", :phone_number => "Fixed#{i}"}]
58
+ }
59
+ end
60
+ end
61
+
62
+ after(:all) do
63
+ plsql.execute "DROP TABLE test_employees"
64
+ plsql.execute "DROP TABLE test_employees2"
65
+ plsql.execute "DROP TYPE t_phones"
66
+ plsql.execute "DROP TYPE t_phone"
67
+ plsql.execute "DROP TYPE t_address"
68
+ plsql.logoff
69
+ end
70
+
71
+ after(:each) do
72
+ plsql.rollback
73
+ end
74
+
75
+ describe "find" do
76
+
77
+ it "should find existing table" do
78
+ PLSQL::Table.find(plsql, :test_employees).should_not be_nil
79
+ end
80
+
81
+ it "should not find nonexisting table" do
82
+ PLSQL::Table.find(plsql, :qwerty123456).should be_nil
83
+ end
84
+
85
+ it "should find existing table in schema" do
86
+ plsql.test_employees.should be_a(PLSQL::Table)
87
+ end
88
+
89
+ end
90
+
91
+ describe "synonym" do
92
+
93
+ before(:all) do
94
+ plsql.connection.exec "CREATE SYNONYM test_employees_synonym FOR hr.test_employees"
95
+ end
96
+
97
+ after(:all) do
98
+ plsql.connection.exec "DROP SYNONYM test_employees_synonym" rescue nil
99
+ end
100
+
101
+ it "should find synonym to table" do
102
+ PLSQL::Table.find(plsql, :test_employees_synonym).should_not be_nil
103
+ end
104
+
105
+ it "should find table using synonym in schema" do
106
+ plsql.test_employees_synonym.should be_a(PLSQL::Table)
107
+ end
108
+
109
+ end
110
+
111
+ describe "public synonym" do
112
+
113
+ it "should find public synonym to table" do
114
+ PLSQL::Table.find(plsql, :dual).should_not be_nil
115
+ end
116
+
117
+ it "should find table using public synonym in schema" do
118
+ plsql.dual.should be_a(PLSQL::Table)
119
+ end
120
+
121
+ end
122
+
123
+ describe "columns" do
124
+
125
+ it "should get columns metadata for table" do
126
+ plsql.test_employees.columns.should == {
127
+ :employee_id =>
128
+ {:position=>1, :data_type=>"NUMBER", :data_length=>22, :data_precision=>15, :data_scale=>0, :char_used=>nil, :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
129
+ :first_name =>
130
+ {:position=>2, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B", :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
131
+ :last_name =>
132
+ {:position=>3, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B", :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
133
+ :hire_date =>
134
+ {:position=>4, :data_type=>"DATE", :data_length=>7, :data_precision=>nil, :data_scale=>nil, :char_used=>nil, :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil}
135
+ }
136
+ end
137
+
138
+ it "should get columns metadata for table with object columns" do
139
+ plsql.test_employees2.columns.should == {
140
+ :employee_id =>
141
+ {:position=>1, :data_type=>"NUMBER", :data_length=>22, :data_precision=>15, :data_scale=>0, :char_used=>nil, :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
142
+ :first_name =>
143
+ {:position=>2, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B", :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
144
+ :last_name =>
145
+ {:position=>3, :data_type=>"VARCHAR2", :data_length=>50, :data_precision=>nil, :data_scale=>nil, :char_used=>"B", :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
146
+ :hire_date =>
147
+ {:position=>4, :data_type=>"DATE", :data_length=>7, :data_precision=>nil, :data_scale=>nil, :char_used=>nil, :type_owner=>nil, :type_name=>nil, :sql_type_name=>nil},
148
+ :address =>
149
+ {:position=>5, :data_type=>"OBJECT", :data_length=>nil, :data_precision=>nil, :data_scale=>nil, :char_used=>nil, :type_owner=>"HR", :type_name=>"T_ADDRESS", :sql_type_name=>"HR.T_ADDRESS"},
150
+ :phones =>
151
+ {:position=>6, :data_type=>"TABLE", :data_length=>nil, :data_precision=>nil, :data_scale=>nil, :char_used=>nil, :type_owner=>"HR", :type_name=>"T_PHONES", :sql_type_name=>"HR.T_PHONES"}
152
+ }
153
+ end
154
+
155
+ end
156
+
157
+ describe "insert" do
158
+ it "should insert a record in table" do
159
+ plsql.test_employees.insert @employees.first
160
+ plsql.test_employees.all.should == [@employees.first]
161
+ end
162
+
163
+ it "should insert array of records in table" do
164
+ plsql.test_employees.insert @employees
165
+ plsql.test_employees.all("ORDER BY employee_id").should == @employees
166
+ end
167
+
168
+ it "should insert a record in table with object types" do
169
+ plsql.test_employees2.insert @employees2.first
170
+ plsql.test_employees2.all.should == [@employees2.first]
171
+ end
172
+
173
+ it "should insert array of records in table with object types" do
174
+ plsql.test_employees2.insert @employees2
175
+ plsql.test_employees2.all("ORDER BY employee_id").should == @employees2
176
+ end
177
+
178
+ end
179
+
180
+ describe "select" do
181
+ before(:each) do
182
+ plsql.test_employees.insert @employees
183
+ end
184
+
185
+ it "should select first record in table" do
186
+ plsql.test_employees.select(:first, "ORDER BY employee_id").should == @employees.first
187
+ plsql.test_employees.first("ORDER BY employee_id").should == @employees.first
188
+ end
189
+
190
+ it "should select all records in table" do
191
+ plsql.test_employees.select(:all, "ORDER BY employee_id").should == @employees
192
+ plsql.test_employees.all("ORDER BY employee_id").should == @employees
193
+ plsql.test_employees.all(:order_by => :employee_id).should == @employees
194
+ end
195
+
196
+ it "should select record in table using WHERE condition" do
197
+ plsql.test_employees.select(:first, "WHERE employee_id = :1", @employees.first[:employee_id]).should == @employees.first
198
+ plsql.test_employees.first("WHERE employee_id = :1", @employees.first[:employee_id]).should == @employees.first
199
+ plsql.test_employees.first(:employee_id => @employees.first[:employee_id]).should == @employees.first
200
+ end
201
+
202
+ it "should count records in table" do
203
+ plsql.test_employees.select(:count).should == @employees.size
204
+ plsql.test_employees.count.should == @employees.size
205
+ end
206
+
207
+ it "should count records in table using condition" do
208
+ plsql.test_employees.select(:count, "WHERE employee_id <= :1", @employees[2][:employee_id]).should == 3
209
+ plsql.test_employees.count("WHERE employee_id <= :1", @employees[2][:employee_id]).should == 3
210
+ end
211
+
212
+ end
213
+
214
+ describe "update" do
215
+ it "should update a record in table" do
216
+ employee_id = @employees.first[:employee_id]
217
+ plsql.test_employees.insert @employees.first
218
+ plsql.test_employees.update :first_name => 'Test', :where => {:employee_id => employee_id}
219
+ plsql.test_employees.first(:employee_id => employee_id)[:first_name].should == 'Test'
220
+ end
221
+
222
+ it "should update a record in table using String WHERE condition" do
223
+ employee_id = @employees.first[:employee_id]
224
+ plsql.test_employees.insert @employees
225
+ plsql.test_employees.update :first_name => 'Test', :where => "employee_id = #{employee_id}"
226
+ plsql.test_employees.first(:employee_id => employee_id)[:first_name].should == 'Test'
227
+ # all other records should not be changed
228
+ plsql.test_employees.all("WHERE employee_id > :1", employee_id) do |employee|
229
+ employee[:first_name].should_not == 'Test'
230
+ end
231
+ end
232
+
233
+ it "should update all records in table" do
234
+ plsql.test_employees.insert @employees
235
+ plsql.test_employees.update :first_name => 'Test'
236
+ plsql.test_employees.all do |employee|
237
+ employee[:first_name].should == 'Test'
238
+ end
239
+ end
240
+
241
+ it "should update a record in table with object type" do
242
+ employee = @employees2[0]
243
+ employee2 = @employees2[1]
244
+ plsql.test_employees2.insert employee
245
+ plsql.test_employees2.update :address => employee2[:address], :phones => employee2[:phones], :where => {:employee_id => employee[:employee_id]}
246
+ updated_employee = plsql.test_employees2.first(:employee_id => employee[:employee_id])
247
+ updated_employee[:address].should == employee2[:address]
248
+ updated_employee[:phones].should == employee2[:phones]
249
+ end
250
+
251
+ end
252
+
253
+ describe "delete" do
254
+ it "should delete record from table" do
255
+ employee_id = @employees.first[:employee_id]
256
+ plsql.test_employees.insert @employees
257
+ plsql.test_employees.delete :employee_id => employee_id
258
+ plsql.test_employees.first(:employee_id => employee_id).should be_nil
259
+ plsql.test_employees.all(:order_by => :employee_id).should == @employees[1, @employees.size-1]
260
+ end
261
+
262
+ it "should delete all records from table" do
263
+ plsql.test_employees.insert @employees
264
+ plsql.test_employees.delete
265
+ plsql.test_employees.all.should be_empty
266
+ end
267
+ end
268
+
269
+ end