Skip to content

Commit ace27af

Browse files
committed
restructured tests a bit, added possibility to directly get subprogram with getattr
1 parent 0abde24 commit ace27af

6 files changed

Lines changed: 64 additions & 42 deletions

File tree

‎python_plsql/client.py‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,36 +254,48 @@ class Subprogram:
254254
def __init__(
255255
self, plsql: Database, resolved: ResolvedName, arguments: Iterator[Argument]
256256
):
257-
self.plsql, self.resolved = plsql, resolved
258-
259-
self.overloads = group_by_overload(arguments=arguments)
257+
self.plsql, self.resolved, self.overloads = (
258+
plsql,
259+
resolved,
260+
group_by_overload(arguments=arguments),
261+
)
260262

261263
def _call_function(self, *args, **kwargs):
264+
# match = self._matches(*args, **kwargs)[0]
265+
262266
with self.plsql._connection.cursor() as cursor:
263267
return cursor.callfunc(self.resolved.name, int, args, kwargs)
264268

265269
def _call_procedure(self, *args, **kwargs):
270+
# match = self._matches(*args, **kwargs)[0]
271+
266272
with self.plsql._connection.cursor() as cursor:
267273
cursor.callproc(self.resolved.name, *args, **kwargs)
268274

269275
def __call__(self, *args, **kwargs):
270-
if not self._match(*args, **kwargs):
276+
if (matches := len(self._matches(*args, **kwargs))) > 1:
277+
raise ValueError("Arguments matched with too many subprograms")
278+
elif matches == 0:
271279
raise ValueError("Arguments do not match subprogram")
272280

273-
if len(self._matches(*args, **kwargs)) > 1:
274-
raise ValueError("Matched with too many subprograms")
281+
if self.is_function and self.is_procedure:
282+
raise NotImplementedError(
283+
"Subprogram is overloaded as a function and procedure, use _call_(procedure/function) method"
284+
)
275285

276286
if self.is_function:
277287
return self._call_function(*args, **kwargs)
278288
else:
279289
self._call_procedure(*args, **kwargs)
280290

281291
def _matches(self, *args, **kwargs) -> List[Overload]:
292+
"""returns overloads that match with provided parameters"""
282293
return [
283294
overload for overload in self.overloads if overload.match(*args, **kwargs)
284295
]
285296

286297
def _match(self, *args, **kwargs) -> bool:
298+
"""returns true if function has"""
287299
return bool(self._matches(*args, **kwargs))
288300

289301
@property
@@ -294,7 +306,11 @@ def overloaded(self) -> bool:
294306

295307
@property
296308
def is_function(self):
297-
return all(overload.return_type for overload in self.overloads)
309+
return any(overload.return_type for overload in self.overloads)
310+
311+
@property
312+
def is_procedure(self):
313+
return any(not overload.return_type for overload in self.overloads)
298314

299315
@property
300316
def standalone(self) -> bool:
@@ -344,6 +360,12 @@ def __getattr__(self, item):
344360
plsql=self, resolved=name, arguments=self._describe_procedure(item),
345361
)
346362
elif name.object_type == ObjectTypes.package:
363+
if len(item.split(".")) > 1:
364+
return Subprogram(
365+
plsql=self,
366+
resolved=name,
367+
arguments=self._describe_procedure(item),
368+
)
347369
return Package(plsql=self, name=item)
348370

349371
raise AttributeError

‎tests/func/conftest.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.fixture(params=["simple_function", "test_package.simple_function"])
5+
def simple_function(plsql, request):
6+
return getattr(plsql, request.param)
7+
8+
9+
@pytest.fixture(params=["simple_procedure", "test_package.simple_procedure"])
10+
def simple_procedure(plsql, request):
11+
return getattr(plsql, request.param)

‎tests/func/test_errors.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
from python_plsql import DatabaseError, NotFound
3+
4+
5+
def test_error_when_object_does_not_exist(plsql):
6+
with pytest.raises(NotFound):
7+
plsql.aoeuidhtqjkxpyfg()
8+
9+
10+
def test_error_when_subprogram_does_not_exist(plsql):
11+
with pytest.raises(NotFound):
12+
plsql.test_package.aoeuidhtqjkxpyfg()
13+
14+
15+
@pytest.mark.parametrize(
16+
"args, kwargs", [([1], {}), ([], {"p_int": 5}), ([1], {"p_int": 5})],
17+
)
18+
def test_incorrect_call_raises_exception(simple_procedure, args, kwargs):
19+
with pytest.raises(ValueError, match="Arguments do not match subprogram"):
20+
simple_procedure(*args, **kwargs)

‎tests/func/test_function_call.py‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
from functools import reduce
33

44

5-
@pytest.fixture(params=["simple_function", "test_package.simple_function"])
6-
def simple_function(plsql, request):
7-
for access in request.param.split("."):
8-
plsql = getattr(plsql, access)
9-
return plsql
10-
11-
125
def test_function_returns_value(plsql, simple_function):
136
assert simple_function(21) == 42
147
assert simple_function(p_int=21) == 42

‎tests/func/test_package_call.py‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎tests/func/test_procedure_call.py‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,7 @@
22
import pytest
33

44

5-
def test_simple_procedure_gets_called(plsql):
6-
with pytest.raises(DatabaseError, match="You called me.+42"):
7-
plsql.simple_procedure()
8-
9-
10-
@pytest.mark.parametrize(
11-
"args, kwargs", [([1], {}), ([], {"p_int": 5}), ([1], {"p_int": 5})],
12-
)
13-
def test_incorrect_call_raises_exception(plsql, args, kwargs):
14-
with pytest.raises(ValueError, match="Arguments do not match subprogram"):
15-
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()
5+
def test_procedure_gets_called(simple_procedure):
6+
"""Checking if database executes the procedure, which is set to raise an application error"""
7+
with pytest.raises(DatabaseError, match="ORA-20001.+You called me.+42"):
8+
simple_procedure()

0 commit comments

Comments
 (0)