2021-11-07 22:00:21 +01:00
|
|
|
# pylint: disable=redefined-outer-name,missing-function-docstring,protected-access
|
|
|
|
""" Tests on opening hours helpers """
|
|
|
|
|
2023-01-06 19:36:14 +01:00
|
|
|
import cx_Oracle
|
2021-11-07 22:00:21 +01:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from mylib.oracle import OracleDB
|
|
|
|
|
|
|
|
|
|
|
|
class FakeCXOracleCursor:
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Fake cx_Oracle cursor"""
|
2021-11-07 22:00:21 +01:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def __init__(
|
|
|
|
self, expected_sql, expected_params, expected_return, expected_just_try, expected_exception
|
|
|
|
):
|
2021-11-07 22:00:21 +01:00
|
|
|
self.expected_sql = expected_sql
|
|
|
|
self.expected_params = expected_params
|
|
|
|
self.expected_return = expected_return
|
|
|
|
self.expected_just_try = expected_just_try
|
|
|
|
self.expected_exception = expected_exception
|
2022-12-09 12:20:01 +01:00
|
|
|
self.opened = True
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
def execute(self, sql, **params):
|
2022-12-09 12:20:01 +01:00
|
|
|
assert self.opened
|
2021-11-07 22:00:21 +01:00
|
|
|
if self.expected_exception:
|
2023-01-16 12:56:12 +01:00
|
|
|
raise cx_Oracle.Error(f"{self}.execute({sql}, {params}): expected exception")
|
|
|
|
if self.expected_just_try and not sql.lower().startswith("select "):
|
|
|
|
assert False, f"{self}.execute({sql}, {params}) may not be executed in just try mode"
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
|
|
|
sql == self.expected_sql
|
|
|
|
), "%s.execute(): Invalid SQL query:\n '%s'\nMay be:\n '%s'" % (
|
|
|
|
self,
|
|
|
|
sql,
|
|
|
|
self.expected_sql,
|
|
|
|
)
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
|
|
|
params == self.expected_params
|
|
|
|
), "%s.execute(): Invalid params:\n %s\nMay be:\n %s" % (
|
|
|
|
self,
|
|
|
|
params,
|
|
|
|
self.expected_params,
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
return self.expected_return
|
|
|
|
|
|
|
|
def fetchall(self):
|
2022-12-09 12:20:01 +01:00
|
|
|
assert self.opened
|
2021-11-07 22:00:21 +01:00
|
|
|
return self.expected_return
|
|
|
|
|
2022-12-09 12:20:01 +01:00
|
|
|
def __enter__(self):
|
|
|
|
self.opened = True
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.opened = False
|
|
|
|
|
2021-11-07 22:00:21 +01:00
|
|
|
def __repr__(self):
|
2023-01-06 22:13:28 +01:00
|
|
|
return (
|
2023-01-16 12:56:12 +01:00
|
|
|
f"FakeCXOracleCursor({self.expected_sql}, {self.expected_params}, "
|
|
|
|
f"{self.expected_return}, {self.expected_just_try})"
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FakeCXOracle:
|
2023-01-16 12:56:12 +01:00
|
|
|
"""Fake cx_Oracle connection"""
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
expected_sql = None
|
2023-01-06 19:36:14 +01:00
|
|
|
expected_params = {}
|
2021-11-07 22:00:21 +01:00
|
|
|
expected_return = True
|
|
|
|
expected_just_try = False
|
|
|
|
expected_exception = False
|
|
|
|
just_try = False
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
2023-10-27 13:35:59 +02:00
|
|
|
allowed_kwargs = {"dsn": str, "user": str, "password": (str, None)}
|
2021-11-07 22:00:21 +01:00
|
|
|
for arg, value in kwargs.items():
|
2023-01-06 22:13:28 +01:00
|
|
|
assert arg in allowed_kwargs, f"Invalid arg {arg}='{value}'"
|
2023-01-16 12:56:12 +01:00
|
|
|
assert isinstance(
|
|
|
|
value, allowed_kwargs[arg]
|
|
|
|
), f"Arg {arg} not a {allowed_kwargs[arg]} ({type(value)})"
|
2021-11-07 22:00:21 +01:00
|
|
|
setattr(self, arg, value)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
return self.expected_return
|
|
|
|
|
|
|
|
def cursor(self):
|
|
|
|
return FakeCXOracleCursor(
|
2023-01-16 12:56:12 +01:00
|
|
|
self.expected_sql,
|
|
|
|
self.expected_params,
|
|
|
|
self.expected_return,
|
|
|
|
self.expected_just_try or self.just_try,
|
|
|
|
self.expected_exception,
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
self._check_just_try()
|
|
|
|
return self.expected_return
|
|
|
|
|
|
|
|
def rollback(self):
|
|
|
|
self._check_just_try()
|
|
|
|
return self.expected_return
|
|
|
|
|
|
|
|
def _check_just_try(self):
|
|
|
|
if self.just_try:
|
|
|
|
assert False, "May not be executed in just try mode"
|
|
|
|
|
|
|
|
|
|
|
|
def fake_cxoracle_connect(**kwargs):
|
|
|
|
return FakeCXOracle(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def fake_cxoracle_connect_just_try(**kwargs):
|
|
|
|
con = FakeCXOracle(**kwargs)
|
|
|
|
con.just_try = True
|
|
|
|
return con
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def test_oracledb():
|
2023-01-16 12:56:12 +01:00
|
|
|
return OracleDB("127.0.0.1/dbname", "user", "password")
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_oracledb(mocker):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("cx_Oracle.connect", fake_cxoracle_connect)
|
|
|
|
return OracleDB("127.0.0.1/dbname", "user", "password")
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_just_try_oracledb(mocker):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("cx_Oracle.connect", fake_cxoracle_connect_just_try)
|
|
|
|
return OracleDB("127.0.0.1/dbname", "user", "password", just_try=True)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_connected_oracledb(fake_oracledb):
|
|
|
|
fake_oracledb.connect()
|
|
|
|
return fake_oracledb
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_connected_just_try_oracledb(fake_just_try_oracledb):
|
|
|
|
fake_just_try_oracledb.connect()
|
|
|
|
return fake_just_try_oracledb
|
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def generate_mock_args(
|
|
|
|
expected_args=(), expected_kwargs={}, expected_return=True
|
|
|
|
): # pylint: disable=dangerous-default-value
|
2021-11-07 22:00:21 +01:00
|
|
|
def mock_args(*args, **kwargs):
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert args == expected_args, "Invalid call args:\n %s\nMay be:\n %s" % (
|
|
|
|
args,
|
|
|
|
expected_args,
|
|
|
|
)
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert kwargs == expected_kwargs, "Invalid call kwargs:\n %s\nMay be:\n %s" % (
|
|
|
|
kwargs,
|
|
|
|
expected_kwargs,
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
return expected_return
|
2023-01-16 12:56:12 +01:00
|
|
|
|
2021-11-07 22:00:21 +01:00
|
|
|
return mock_args
|
|
|
|
|
|
|
|
|
|
|
|
def mock_doSQL_just_try(self, sql, params=None): # pylint: disable=unused-argument
|
|
|
|
assert False, "doSQL() may not be executed in just try mode"
|
|
|
|
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
def generate_mock_doSQL(
|
|
|
|
expected_sql, expected_params={}, expected_return=True
|
|
|
|
): # pylint: disable=dangerous-default-value
|
2021-11-07 22:00:21 +01:00
|
|
|
def mock_doSQL(self, sql, params=None): # pylint: disable=unused-argument
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert sql == expected_sql, "Invalid generated SQL query:\n '%s'\nMay be:\n '%s'" % (
|
|
|
|
sql,
|
|
|
|
expected_sql,
|
|
|
|
)
|
2023-01-06 22:13:28 +01:00
|
|
|
# pylint: disable=consider-using-f-string
|
2023-01-16 12:56:12 +01:00
|
|
|
assert params == expected_params, "Invalid generated params:\n %s\nMay be:\n %s" % (
|
|
|
|
params,
|
|
|
|
expected_params,
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
return expected_return
|
2023-01-16 12:56:12 +01:00
|
|
|
|
2021-11-07 22:00:21 +01:00
|
|
|
return mock_doSQL
|
|
|
|
|
|
|
|
|
|
|
|
# OracleDB.doSelect() have same expected parameters as OracleDB.doSQL()
|
|
|
|
generate_mock_doSelect = generate_mock_doSQL
|
|
|
|
mock_doSelect_just_try = mock_doSQL_just_try
|
|
|
|
|
|
|
|
#
|
|
|
|
# Test on OracleDB helper methods
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def test_combine_params_with_to_add_parameter():
|
2023-10-27 13:35:59 +02:00
|
|
|
assert OracleDB._combine_params({"test1": 1}, {"test2": 2}) == {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_combine_params_with_kargs():
|
2023-10-27 13:35:59 +02:00
|
|
|
assert OracleDB._combine_params({"test1": 1}, test2=2) == {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_combine_params_with_kargs_and_to_add_parameter():
|
2023-10-27 13:35:59 +02:00
|
|
|
assert OracleDB._combine_params({"test1": 1}, {"test2": 2}, test3=3) == {
|
|
|
|
"test1": 1,
|
|
|
|
"test2": 2,
|
|
|
|
"test3": 3,
|
|
|
|
}
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_params_are_preserved():
|
2023-10-27 13:35:59 +02:00
|
|
|
args = ("test = test", {"test1": 1})
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._format_where_clauses(*args) == args
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_raw():
|
2023-01-16 12:56:12 +01:00
|
|
|
assert OracleDB._format_where_clauses("test = test") == ("test = test", {})
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_tuple_clause_with_params():
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = ("test1 = :test1 AND test2 = :test2", {"test1": 1, "test2": 2})
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._format_where_clauses(where_clauses) == where_clauses
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_dict():
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._format_where_clauses(where_clauses) == (
|
|
|
|
'"test1" = :test1 AND "test2" = :test2',
|
2023-01-16 12:56:12 +01:00
|
|
|
where_clauses,
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_combined_types():
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = ("test1 = 1", ("test2 LIKE :test2", {"test2": 2}), {"test3": 3, "test4": 4})
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._format_where_clauses(where_clauses) == (
|
|
|
|
'test1 = 1 AND test2 LIKE :test2 AND "test3" = :test3 AND "test4" = :test4',
|
2023-10-27 13:35:59 +02:00
|
|
|
{"test2": 2, "test3": 3, "test4": 4},
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_format_where_clauses_with_where_op():
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test1": 1, "test2": 2}
|
2023-01-16 12:56:12 +01:00
|
|
|
assert OracleDB._format_where_clauses(where_clauses, where_op="OR") == (
|
2021-11-07 22:00:21 +01:00
|
|
|
'"test1" = :test1 OR "test2" = :test2',
|
2023-01-16 12:56:12 +01:00
|
|
|
where_clauses,
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_where_clauses():
|
|
|
|
sql = "SELECT * FROM table"
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._add_where_clauses(sql, None, where_clauses) == (
|
|
|
|
sql + ' WHERE "test1" = :test1 AND "test2" = :test2',
|
2023-01-16 12:56:12 +01:00
|
|
|
where_clauses,
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_where_clauses_preserved_params():
|
|
|
|
sql = "SELECT * FROM table"
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test1": 1, "test2": 2}
|
|
|
|
params = {"fake1": 1}
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._add_where_clauses(sql, params.copy(), where_clauses) == (
|
|
|
|
sql + ' WHERE "test1" = :test1 AND "test2" = :test2',
|
2023-10-27 13:35:59 +02:00
|
|
|
{**where_clauses, **params},
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_where_clauses_with_op():
|
|
|
|
sql = "SELECT * FROM table"
|
2023-01-16 12:56:12 +01:00
|
|
|
where_clauses = ("test1=1", "test2=2")
|
|
|
|
assert OracleDB._add_where_clauses(sql, None, where_clauses, where_op="OR") == (
|
|
|
|
sql + " WHERE test1=1 OR test2=2",
|
|
|
|
{},
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_where_clauses_with_duplicated_field():
|
|
|
|
sql = "UPDATE table SET test1=:test1"
|
2023-10-27 13:35:59 +02:00
|
|
|
params = {"test1": "new_value"}
|
|
|
|
where_clauses = {"test1": "where_value"}
|
2021-11-07 22:00:21 +01:00
|
|
|
assert OracleDB._add_where_clauses(sql, params, where_clauses) == (
|
|
|
|
sql + ' WHERE "test1" = :test1_1',
|
2023-10-27 13:35:59 +02:00
|
|
|
{"test1": "new_value", "test1_1": "where_value"},
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_quote_table_name():
|
|
|
|
assert OracleDB._quote_table_name("mytable") == '"mytable"'
|
|
|
|
assert OracleDB._quote_table_name("myschema.mytable") == '"myschema"."mytable"'
|
|
|
|
|
|
|
|
|
|
|
|
def test_insert(mocker, test_oracledb):
|
2023-10-27 13:35:59 +02:00
|
|
|
values = {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
mocker.patch(
|
2023-01-16 12:56:12 +01:00
|
|
|
"mylib.oracle.OracleDB.doSQL",
|
2021-11-07 22:00:21 +01:00
|
|
|
generate_mock_doSQL(
|
2023-01-16 12:56:12 +01:00
|
|
|
'INSERT INTO "mytable" ("test1", "test2") VALUES (:test1, :test2)', values
|
|
|
|
),
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert test_oracledb.insert("mytable", values)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_insert_just_try(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSQL", mock_doSQL_just_try)
|
2023-10-27 13:35:59 +02:00
|
|
|
assert test_oracledb.insert("mytable", {"test1": 1, "test2": 2}, just_try=True)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_update(mocker, test_oracledb):
|
2023-10-27 13:35:59 +02:00
|
|
|
values = {"test1": 1, "test2": 2}
|
|
|
|
where_clauses = {"test3": 3, "test4": 4}
|
2021-11-07 22:00:21 +01:00
|
|
|
mocker.patch(
|
2023-01-16 12:56:12 +01:00
|
|
|
"mylib.oracle.OracleDB.doSQL",
|
2021-11-07 22:00:21 +01:00
|
|
|
generate_mock_doSQL(
|
2023-01-16 12:56:12 +01:00
|
|
|
'UPDATE "mytable" SET "test1" = :test1, "test2" = :test2 WHERE "test3" = :test3 AND'
|
|
|
|
' "test4" = :test4',
|
2023-10-27 13:35:59 +02:00
|
|
|
{**values, **where_clauses},
|
2023-01-16 12:56:12 +01:00
|
|
|
),
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert test_oracledb.update("mytable", values, where_clauses)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_update_just_try(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSQL", mock_doSQL_just_try)
|
2023-10-27 13:35:59 +02:00
|
|
|
assert test_oracledb.update("mytable", {"test1": 1, "test2": 2}, None, just_try=True)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete(mocker, test_oracledb):
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test1": 1, "test2": 2}
|
2021-11-07 22:00:21 +01:00
|
|
|
mocker.patch(
|
2023-01-16 12:56:12 +01:00
|
|
|
"mylib.oracle.OracleDB.doSQL",
|
2021-11-07 22:00:21 +01:00
|
|
|
generate_mock_doSQL(
|
2023-01-16 12:56:12 +01:00
|
|
|
'DELETE FROM "mytable" WHERE "test1" = :test1 AND "test2" = :test2', where_clauses
|
|
|
|
),
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert test_oracledb.delete("mytable", where_clauses)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_delete_just_try(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSQL", mock_doSQL_just_try)
|
|
|
|
assert test_oracledb.delete("mytable", None, just_try=True)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_truncate(mocker, test_oracledb):
|
|
|
|
mocker.patch(
|
2023-01-16 12:56:12 +01:00
|
|
|
"mylib.oracle.OracleDB.doSQL", generate_mock_doSQL('TRUNCATE TABLE "mytable"', None)
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert test_oracledb.truncate("mytable")
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_truncate_just_try(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSQL", mock_doSelect_just_try)
|
|
|
|
assert test_oracledb.truncate("mytable", just_try=True)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_select(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fields = ("field1", "field2")
|
2023-10-27 13:35:59 +02:00
|
|
|
where_clauses = {"test3": 3, "test4": 4}
|
2021-11-07 22:00:21 +01:00
|
|
|
expected_return = [
|
2023-10-27 13:35:59 +02:00
|
|
|
{"field1": 1, "field2": 2},
|
|
|
|
{"field1": 2, "field2": 3},
|
2021-11-07 22:00:21 +01:00
|
|
|
]
|
|
|
|
order_by = "field1, DESC"
|
2023-12-15 11:35:43 +01:00
|
|
|
limit = 10
|
2021-11-07 22:00:21 +01:00
|
|
|
mocker.patch(
|
2023-01-16 12:56:12 +01:00
|
|
|
"mylib.oracle.OracleDB.doSelect",
|
2021-11-07 22:00:21 +01:00
|
|
|
generate_mock_doSQL(
|
2023-01-16 12:56:12 +01:00
|
|
|
'SELECT "field1", "field2" FROM "mytable" WHERE "test3" = :test3 AND "test4" = :test4'
|
2023-12-15 11:35:43 +01:00
|
|
|
" ORDER BY " + order_by + " LIMIT " + str(limit), # nosec: B608
|
2023-01-16 12:56:12 +01:00
|
|
|
where_clauses,
|
|
|
|
expected_return,
|
|
|
|
),
|
2021-11-07 22:00:21 +01:00
|
|
|
)
|
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
2023-12-15 11:35:43 +01:00
|
|
|
test_oracledb.select("mytable", where_clauses, fields, order_by=order_by, limit=limit)
|
|
|
|
== expected_return
|
2023-01-16 12:56:12 +01:00
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_select_without_field_and_order_by(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSelect", generate_mock_doSQL('SELECT * FROM "mytable"'))
|
2021-11-07 22:00:21 +01:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
assert test_oracledb.select("mytable")
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_select_just_try(mocker, test_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("mylib.oracle.OracleDB.doSQL", mock_doSelect_just_try)
|
|
|
|
assert test_oracledb.select("mytable", None, None, just_try=True)
|
|
|
|
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Tests on main methods
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
def test_connect(mocker, test_oracledb):
|
2023-10-27 13:35:59 +02:00
|
|
|
expected_kwargs = {
|
|
|
|
"dsn": test_oracledb._dsn,
|
|
|
|
"user": test_oracledb._user,
|
|
|
|
"password": test_oracledb._pwd,
|
|
|
|
}
|
2021-11-07 22:00:21 +01:00
|
|
|
|
2023-01-16 12:56:12 +01:00
|
|
|
mocker.patch("cx_Oracle.connect", generate_mock_args(expected_kwargs=expected_kwargs))
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
assert test_oracledb.connect()
|
|
|
|
|
|
|
|
|
|
|
|
def test_close(fake_oracledb):
|
|
|
|
assert fake_oracledb.close() is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_close_connected(fake_connected_oracledb):
|
|
|
|
assert fake_connected_oracledb.close() is None
|
|
|
|
|
|
|
|
|
|
|
|
def test_doSQL(fake_connected_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_oracledb._conn.expected_sql = "DELETE FROM table WHERE test1 = :test1"
|
2023-10-27 13:35:59 +02:00
|
|
|
fake_connected_oracledb._conn.expected_params = {"test1": 1}
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_oracledb.doSQL(
|
|
|
|
fake_connected_oracledb._conn.expected_sql, fake_connected_oracledb._conn.expected_params
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSQL_without_params(fake_connected_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_oracledb._conn.expected_sql = "DELETE FROM table"
|
2021-11-07 22:00:21 +01:00
|
|
|
fake_connected_oracledb.doSQL(fake_connected_oracledb._conn.expected_sql)
|
|
|
|
|
|
|
|
|
|
|
|
def test_doSQL_just_try(fake_connected_just_try_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
assert fake_connected_just_try_oracledb.doSQL("DELETE FROM table")
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSQL_on_exception(fake_connected_oracledb):
|
|
|
|
fake_connected_oracledb._conn.expected_exception = True
|
2023-01-16 12:56:12 +01:00
|
|
|
assert fake_connected_oracledb.doSQL("DELETE FROM table") is False
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSelect(fake_connected_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_oracledb._conn.expected_sql = "SELECT * FROM table WHERE test1 = :test1"
|
2023-10-27 13:35:59 +02:00
|
|
|
fake_connected_oracledb._conn.expected_params = {"test1": 1}
|
|
|
|
fake_connected_oracledb._conn.expected_return = [{"test1": 1}]
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
|
|
|
fake_connected_oracledb.doSelect(
|
|
|
|
fake_connected_oracledb._conn.expected_sql,
|
|
|
|
fake_connected_oracledb._conn.expected_params,
|
|
|
|
)
|
|
|
|
== fake_connected_oracledb._conn.expected_return
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSelect_without_params(fake_connected_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_oracledb._conn.expected_sql = "SELECT * FROM table"
|
2023-10-27 13:35:59 +02:00
|
|
|
fake_connected_oracledb._conn.expected_return = [{"test1": 1}]
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
|
|
|
fake_connected_oracledb.doSelect(fake_connected_oracledb._conn.expected_sql)
|
|
|
|
== fake_connected_oracledb._conn.expected_return
|
|
|
|
)
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSelect_on_exception(fake_connected_oracledb):
|
|
|
|
fake_connected_oracledb._conn.expected_exception = True
|
2023-01-16 12:56:12 +01:00
|
|
|
assert fake_connected_oracledb.doSelect("SELECT * FROM table") is False
|
2021-11-07 22:00:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_doSelect_just_try(fake_connected_just_try_oracledb):
|
2023-01-16 12:56:12 +01:00
|
|
|
fake_connected_just_try_oracledb._conn.expected_sql = "SELECT * FROM table WHERE test1 = :test1"
|
2023-10-27 13:35:59 +02:00
|
|
|
fake_connected_just_try_oracledb._conn.expected_params = {"test1": 1}
|
|
|
|
fake_connected_just_try_oracledb._conn.expected_return = [{"test1": 1}]
|
2023-01-16 12:56:12 +01:00
|
|
|
assert (
|
|
|
|
fake_connected_just_try_oracledb.doSelect(
|
|
|
|
fake_connected_just_try_oracledb._conn.expected_sql,
|
|
|
|
fake_connected_just_try_oracledb._conn.expected_params,
|
|
|
|
)
|
|
|
|
== fake_connected_just_try_oracledb._conn.expected_return
|
|
|
|
)
|