Skip to content

Commit 0ab01e7

Browse files
committed
initial commit
1 parent 13376e9 commit 0ab01e7

9 files changed

Lines changed: 92 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,5 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
./tests/database.ini

‎plsql/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .client import Database

‎plsql/client.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import cx_Oracle as oracle
2+
3+
4+
class Database:
5+
6+
def __init__(self, user, password, host, port, service_name, encoding):
7+
dsn = oracle.makedsn(host, port, service_name=service_name)
8+
self.connection = oracle.connect(user=user, password=password, dsn=dsn, encoding=encoding, nencoding=encoding)
9+
10+
def execute_immediate(self, dynamic_string):
11+
with self.connection.cursor() as cursor:
12+
cursor.execute(dynamic_string)
13+
14+
def __getattr__(self, item):
15+
def func(**kwargs):
16+
with self.connection.cursor() as cursor:
17+
return cursor.callfunc(item, int, keywordParameters=kwargs)
18+
19+
return func

‎requirements.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cx_Oracle
2+
pytest

‎tests/conftest.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
from plsql import Database
3+
from contextlib import contextmanager
4+
import configparser
5+
6+
7+
@pytest.fixture(scope='session')
8+
def plsql():
9+
config = configparser.ConfigParser()
10+
config.read('./database.ini')
11+
12+
print()
13+
14+
return Database(**dict(config['database'].items()))
15+
16+
17+
@contextmanager
18+
def managed_object(plsql, object_type, object_name):
19+
with open(f'./sql/{object_type}/{object_name}.sql', mode='r', encoding='utf8') as file:
20+
sql_text = file.read()
21+
22+
plsql.execute_immediate(sql_text)
23+
yield
24+
plsql.execute_immediate(f'DROP {object_type} {object_name}')
25+
26+
27+
@pytest.fixture(scope='module')
28+
def simple_function(plsql):
29+
with managed_object(plsql, 'function', 'simple_function'):
30+
yield
31+
32+
33+
@pytest.fixture(scope='module')
34+
def function_with_defaults(plsql):
35+
with managed_object(plsql, 'function', 'function_with_defaults'):
36+
yield

‎tests/sql/database_setup.sql‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE USER test_user IDENTIFIED BY test_password
2+
/
3+
4+
GRANT CONNECT, RESOURCE, DBA TO test_user
5+
/
6+
7+
GRANT CREATE SESSION TO test_user
8+
/
9+
10+
GRANT UNLIMITED TABLESPACE TO test_user
11+
/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE OR REPLACE FUNCTION function_with_defaults(p_int_a IN INTEGER DEFAULT 1,
2+
p_int_b IN INTEGER DEFAULT 4)
3+
RETURN INTEGER IS
4+
BEGIN
5+
RETURN p_int_a + p_int_b;
6+
END function_with_defaults;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE OR REPLACE FUNCTION simple_function(p_int IN INTEGER)
2+
RETURN INTEGER IS
3+
BEGIN
4+
RETURN p_int * 2;
5+
END simple_function;

‎tests/test_functions.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def test_simple_function(plsql, simple_function):
2+
assert plsql.simple_function(p_int=5) == 5 + 5
3+
assert plsql.simple_function(p_int=None) is None
4+
5+
6+
def test_function_with_defaults(plsql, function_with_defaults):
7+
assert plsql.function_with_defaults() == 5
8+
assert plsql.function_with_defaults(p_int_a=0) == 4
9+
assert plsql.function_with_defaults(p_int_b=0) == 1
10+
assert plsql.function_with_defaults(p_int_a=0, p_int_b=0) == 0

0 commit comments

Comments
 (0)