-
Notifications
You must be signed in to change notification settings - Fork 58
/
package_spec.rb
172 lines (137 loc) · 4.55 KB
/
package_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require "spec_helper"
describe "Package" do
before(:all) do
plsql.connection = get_connection
plsql.execute <<-SQL
CREATE OR REPLACE PACKAGE test_package IS
test_variable NUMBER;
FUNCTION test_procedure ( p_string VARCHAR2 )
RETURN VARCHAR2;
END;
SQL
plsql.execute <<-SQL
CREATE OR REPLACE PACKAGE BODY test_package IS
FUNCTION test_procedure ( p_string VARCHAR2 )
RETURN VARCHAR2
IS
BEGIN
RETURN UPPER(p_string);
END test_procedure;
END;
SQL
end
after(:all) do
plsql.execute "DROP PACKAGE test_package"
plsql.logoff
end
before(:each) do
end
it "should find existing package" do
expect(PLSQL::Package.find(plsql, :test_package)).not_to be_nil
end
it "should not find nonexisting package" do
expect(PLSQL::Package.find(plsql, :qwerty123456)).to be_nil
end
it "should find existing package in schema" do
expect(plsql.test_package.class).to eq(PLSQL::Package)
end
it "should execute package function and return correct value" do
expect(plsql.test_package.test_procedure("xxx")).to eq("XXX")
end
it "should report an existing procedure as existing" do
expect(plsql.test_package.procedure_defined?(:test_procedure)).to be_truthy
end
it "should report an inexistent procedure as not existing" do
expect(plsql.test_package.procedure_defined?(:inexistent_procedure)).to be_falsey
end
it "should search objects via []" do
package = PLSQL::Package.find(plsql, :test_package)
[:Test_Procedure, :test_procedure, "test_procedure", "TEST_PROCEDURE"].each do |name_variant|
expect(package[name_variant]).to be_a PLSQL::Procedure
end
[:Test_Variable, :test_variable, "test_variable", "TEST_VARIABLE"].each do |name_variant|
expect(package[name_variant]).to be_a PLSQL::Variable
end
end
context "with a user with execute privilege who is not the package owner" do
before(:all) do
plsql.execute("grant execute on TEST_PACKAGE to #{DATABASE_USERS_AND_PASSWORDS[1][0]}")
@original_connection = plsql.connection
@conn = get_connection(1)
end
before(:each) do
# resetting connection clears cached package objects and schema name
plsql.connection = @conn
end
after(:all) do
plsql.logoff
plsql.connection = @original_connection
end
it "should not find existing package" do
expect(PLSQL::Package.find(plsql, :test_package)).to be_nil
end
context "who sets current_schema to match the package owner" do
before(:all) do
plsql.execute "ALTER SESSION set current_schema=#{DATABASE_USERS_AND_PASSWORDS[0][0]}"
end
it "should find existing package" do
expect(PLSQL::Package.find(plsql, :test_package)).not_to be_nil
end
it "should report an existing procedure as existing" do
expect(plsql.test_package.procedure_defined?(:test_procedure)).to be_truthy
end
end
end
describe "variables" do
it "should set and get package variable value" do
plsql.test_package.test_variable = 1
expect(plsql.test_package.test_variable).to eq(1)
end
end
end
describe "Synonym to package" do
before(:all) do
plsql.connection = get_connection
plsql.execute <<-SQL
CREATE OR REPLACE PACKAGE hr.test_package IS
FUNCTION test_procedure ( p_string VARCHAR2 )
RETURN VARCHAR2;
END;
SQL
plsql.execute <<-SQL
CREATE OR REPLACE PACKAGE BODY hr.test_package IS
FUNCTION test_procedure ( p_string VARCHAR2 )
RETURN VARCHAR2
IS
BEGIN
RETURN UPPER(p_string);
END test_procedure;
END;
SQL
plsql.execute "CREATE SYNONYM test_pkg_synonym FOR hr.test_package"
end
after(:all) do
plsql.execute "DROP SYNONYM test_pkg_synonym" rescue nil
plsql.logoff
end
it "should find synonym to package" do
expect(PLSQL::Package.find(plsql, :test_pkg_synonym)).not_to be_nil
end
it "should execute package function using synonym and return correct value" do
expect(plsql.test_pkg_synonym.test_procedure("xxx")).to eq("XXX")
end
end
describe "Public synonym to package" do
before(:all) do
plsql.connection = get_connection
end
after(:all) do
plsql.logoff
end
it "should find public synonym to package" do
expect(PLSQL::Package.find(plsql, :utl_encode)).not_to be_nil
end
it "should execute package function using public synonym and return correct value" do
expect(plsql.utl_encode.base64_encode("abc")).to eq("4372773D")
end
end