Skip to content

Commit f270aaa

Browse files
committed
refactored tests into separate dirs
implemented ability to call procedures in packages
1 parent 60ce4d0 commit f270aaa

12 files changed

Lines changed: 72 additions & 28 deletions

‎python_plsql/client.py‎

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,26 +61,31 @@ def _dbms_describe_describe_procedure(
6161
radix = number_table.newobject()
6262
spare = number_table.newobject()
6363

64-
cursor.callproc(
65-
"dbms_describe.describe_procedure",
66-
[
67-
name,
68-
None,
69-
None,
70-
overload,
71-
position,
72-
level,
73-
argument_name,
74-
datatype,
75-
default_value,
76-
in_out,
77-
length,
78-
precision,
79-
scale,
80-
radix,
81-
spare,
82-
],
83-
)
64+
try:
65+
cursor.callproc(
66+
"dbms_describe.describe_procedure",
67+
[
68+
name,
69+
None,
70+
None,
71+
overload,
72+
position,
73+
level,
74+
argument_name,
75+
datatype,
76+
default_value,
77+
in_out,
78+
length,
79+
precision,
80+
scale,
81+
radix,
82+
spare,
83+
],
84+
)
85+
except oracle.DatabaseError as err:
86+
if err.args[0].code == 20001:
87+
raise NotFound
88+
raise
8489

8590
return zip(
8691
overload.aslist(),
@@ -159,6 +164,20 @@ def __init__(self, plsql: Database, name: str):
159164
self._plsql, self._name = plsql, name
160165

161166

167+
class Package:
168+
def __init__(self, plsql: Database, name: str):
169+
self._plsql, self._name = plsql, name
170+
171+
def __getattr__(self, item):
172+
item = f"{self._name}.{item}"
173+
name = self._plsql._name_resolve(item)
174+
return Subprogram(
175+
plsql=self._plsql,
176+
resolved=name,
177+
arguments=self._plsql._describe_procedure(item),
178+
)
179+
180+
162181
class Query:
163182
def __init__(
164183
self, connection: oracle.Connection, sql: str, *positional_binds, **named_binds
@@ -284,16 +303,21 @@ def __init__(
284303
user=user, password=password, dsn=dsn, encoding=encoding, nencoding=encoding
285304
)
286305

306+
def is_schema(self, schema_name):
307+
return bool(
308+
self.query(
309+
"""SELECT COUNT(*) FROM all_users WHERE username = UPPER(:username)""",
310+
username=schema_name,
311+
).first
312+
)
313+
287314
def __getattr__(self, item):
288315
try:
289316
name = self._name_resolve(item)
290317
except (oracle.DatabaseError, NotFound):
291-
if self.query(
292-
"""SELECT COUNT(*) FROM all_users WHERE username = UPPER(:username)""",
293-
username=item,
294-
).first:
318+
if self.is_schema(schema_name=item):
295319
return Schema(plsql=self, name=item)
296-
raise AttributeError
320+
raise
297321
else:
298322
if name.object_type in {
299323
ObjectTypes.function,
@@ -302,6 +326,8 @@ def __getattr__(self, item):
302326
return Subprogram(
303327
plsql=self, resolved=name, arguments=self._describe_procedure(item),
304328
)
329+
elif name.object_type == ObjectTypes.package:
330+
return Package(plsql=self, name=item)
305331

306332
raise AttributeError
307333

‎tests/__init__.py‎

Whitespace-only changes.

‎tests/func/__init__.py‎

Whitespace-only changes.

‎tests/func/test_package_call.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
from python_plsql import DatabaseError, NotFound
3+
4+
5+
def test_simple_procedure_gets_called(plsql):
6+
with pytest.raises(DatabaseError, match="You called me.+42"):
7+
plsql.test_package.simple_procedure()
8+
9+
10+
def test_error_when_subprogram_does_not_exist(plsql):
11+
with pytest.raises(NotFound):
12+
plsql.test_package.aoeuidhtqjkxpyfg()
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from python_plsql import DatabaseError
1+
from python_plsql import DatabaseError, NotFound
22
import pytest
33

44

@@ -8,8 +8,13 @@ def test_simple_procedure_gets_called(plsql):
88

99

1010
@pytest.mark.parametrize(
11-
"args, kwargs", [([1], {}), ([], {"p_int": 5}), ([1], {"p_int": 5})]
11+
"args, kwargs", [([1], {}), ([], {"p_int": 5}), ([1], {"p_int": 5})],
1212
)
1313
def test_incorrect_call_raises_exception(plsql, args, kwargs):
1414
with pytest.raises(ValueError, match="Arguments do not match subprogram"):
1515
plsql.simple_procedure(*args, **kwargs)
16+
17+
18+
def test_error_when_subprogram_does_not_exist(plsql):
19+
with pytest.raises(NotFound):
20+
plsql.aoeuidhtqjkxpyfg()

‎tests/sql/package/test_package-b.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
CREATE OR REPLACE PACKAGE BODY test_package AS
22

33
PROCEDURE simple_procedure IS
4+
v_int INTEGER DEFAULT 42;
45
BEGIN
5-
NULL;
6+
raise_application_error(-20001, 'You called me :) ' || TO_CHAR(v_int));
67
END simple_procedure;
78

89
FUNCTION simple_function

‎tests/unit/__init__.py‎

Whitespace-only changes.

0 commit comments

Comments
 (0)