Skip to content

Commit

Permalink
#178 improved MockQueryHandlerRunner (#201)
Browse files Browse the repository at this point in the history
Renamed
* testing.mock_query_handler_runner to query_handler.python_query_handler_runner
* method PythonQueryHandlerRunner.execute_query() to execute_queries()
* mock_query_result to python_query_result
* Fixed making methods private and made remaining methods private, too
* Included type of result in exception during method PythonQueryHandlerRunner.run()

Co-authored-by: Torsten Kilias <[email protected]>
  • Loading branch information
ckunki and tkilias authored Oct 21, 2024
1 parent 085bdc7 commit e299f38
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 68 deletions.
4 changes: 4 additions & 0 deletions doc/changes/changes_0.1.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Code name:
* #176: Updated usage of `exasol-bucketfs` to new API
* #185: Removed directory and script for building SLC AAF
* #191: Renamed UDF json element "parameters" to "parameter"
* #178: Fixed names of mock objects:
* Renamed `testing.mock_query_handler_runner.MockQueryHandlerRunner` to `query_handler.python_query_handler_runner.PythonQueryHandlerRunner`
* Renamed method `PythonQueryHandlerRunner.execute_query()` to `execute_queries()`
* Renamed `mock_query_result.MockQueryResult` to `python_query_result.PythonQueryResult`

### Documentation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from exasol_advanced_analytics_framework.query_handler.query.select_query import SelectQueryWithColumnDefinition
from exasol_advanced_analytics_framework.query_handler.query_handler import QueryHandler
from exasol_advanced_analytics_framework.query_handler.result import Continue, Finish
from exasol_advanced_analytics_framework.query_result.mock_query_result import MockQueryResult
from exasol_advanced_analytics_framework.query_result.python_query_result import PythonQueryResult
from exasol_advanced_analytics_framework.udf_framework.query_handler_runner_state import QueryHandlerRunnerState

LOGGER = logging.getLogger(__file__)
Expand All @@ -22,7 +22,7 @@
ParameterType = TypeVar("ParameterType")


class MockQueryHandlerRunner(Generic[ParameterType, ResultType]):
class PythonQueryHandlerRunner(Generic[ParameterType, ResultType]):

def __init__(self,
sql_executor: SQLExecutor,
Expand All @@ -43,55 +43,55 @@ def run(self) -> ResultType:
try:
result = self._state.query_handler.start()
while isinstance(result, Continue):
result = self.handle_continue(result)
result = self._handle_continue(result)
if isinstance(result, Finish):
self.handle_finish()
self._handle_finish()
return result.result
else:
raise RuntimeError("Unknown Result")
raise RuntimeError(f"Unsupported result type {type(result)}")
except Exception as e:
try:
self.handle_finish()
self._handle_finish()
except Exception as e1:
LOGGER.exception("Catched exeception during cleanup after an exception.")
raise RuntimeError(f"Execution of query handler {self._state.query_handler} failed.") from e

def handle_continue(self, result: Continue) -> Union[Continue, Finish[ResultType]]:
self.release_and_create_query_handler_context_of_input_query()
self.cleanup_query_handler_context()
self.execute_query(result.query_list)
input_query_result = self.run_input_query(result)
def _handle_continue(self, result: Continue) -> Union[Continue, Finish[ResultType]]:
self._release_and_create_query_handler_context_of_input_query()
self._cleanup_query_handler_context()
self._execute_queries(result.query_list)
input_query_result = self._run_input_query(result)
result = self._state.query_handler.handle_query_result(input_query_result)
return result

def run_input_query(self, result: Continue) -> MockQueryResult:
def _run_input_query(self, result: Continue) -> PythonQueryResult:
input_query_view, input_query = self._wrap_return_query(result.input_query)
self._sql_executor.execute(input_query_view)
input_query_result_set = self._sql_executor.execute(input_query)
if input_query_result_set.columns() != result.input_query.output_columns:
raise RuntimeError(f"Specified columns {result.input_query.output_columns} of the input query "
f"are not equal to the actual received columns {input_query_result_set.columns()}")
input_query_result_table = input_query_result_set.fetchall()
input_query_result = MockQueryResult(data=input_query_result_table,
input_query_result = PythonQueryResult(data=input_query_result_table,
columns=result.input_query.output_columns)
return input_query_result

def handle_finish(self):
def _handle_finish(self):
if self._state.input_query_query_handler_context is not None:
self._state.input_query_query_handler_context.release()
self._state.top_level_query_handler_context.release()
self.cleanup_query_handler_context()
self._cleanup_query_handler_context()

def cleanup_query_handler_context(self):
def _cleanup_query_handler_context(self):
cleanup_query_list = \
self._state.top_level_query_handler_context.cleanup_released_object_proxies()
self.execute_query(cleanup_query_list)
self._execute_queries(cleanup_query_list)

def execute_query(self, queries: List[Query]):
def _execute_queries(self, queries: List[Query]):
for query in queries:
self._sql_executor.execute(query.query_string)

def release_and_create_query_handler_context_of_input_query(self):
def _release_and_create_query_handler_context_of_input_query(self):
if self._state.input_query_query_handler_context is not None:
self._state.input_query_query_handler_context.release()
self._state.input_query_query_handler_context = \
Expand All @@ -101,15 +101,15 @@ def _wrap_return_query(self, input_query: SelectQueryWithColumnDefinition) -> Tu
temporary_view_name = self._state.input_query_query_handler_context.get_temporary_view_name()
input_query_create_view_string = cleandoc(
f"""
CREATE OR REPLACE VIEW {temporary_view_name.fully_qualified} AS
CREATE OR REPLACE VIEW {temporary_view_name.fully_qualified} AS
{input_query.query_string};
""")
full_qualified_columns = [col.name.fully_qualified
for col in input_query.output_columns]
columns_str = ",\n".join(full_qualified_columns)
input_query_string = cleandoc(
f"""
SELECT
SELECT
{textwrap.indent(columns_str, " " * 4)}
FROM {temporary_view_name.fully_qualified};
""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from exasol_advanced_analytics_framework.query_result.query_result import QueryResult, Row


class MockQueryResult(QueryResult):
class PythonQueryResult(QueryResult):
def __getattr__(self, name: str) -> Any:
return self[name]

Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from exasol_advanced_analytics_framework.query_handler.query_handler import QueryHandler
from exasol_advanced_analytics_framework.query_handler.result import Continue, \
Finish
from exasol_advanced_analytics_framework.query_result.mock_query_result import MockQueryResult
from exasol_advanced_analytics_framework.query_result.python_query_result import PythonQueryResult
from exasol_advanced_analytics_framework.query_result.query_result import QueryResult


Expand All @@ -36,7 +36,7 @@ def test():
handler = TestQueryHandler({"a": 2}, query_handler_context)
result = handler.start()
if isinstance(result, Continue):
query_result = MockQueryResult([(2,)], result.input_query.output_columns)
query_result = PythonQueryResult([(2,)], result.input_query.output_columns)
actual_result = handler.handle_query_result(query_result).result
assert actual_result == 2
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from exasol_advanced_analytics_framework.query_handler.query_handler import QueryHandler
from exasol_advanced_analytics_framework.query_handler.result import Continue, Finish
from exasol_advanced_analytics_framework.query_result.query_result import QueryResult
from exasol_advanced_analytics_framework.testing.mock_query_handler_runner import MockQueryHandlerRunner
from exasol_advanced_analytics_framework.query_handler.python_query_handler_runner import PythonQueryHandlerRunner

EXPECTED_EXCEPTION = "ExpectedException"

Expand Down Expand Up @@ -71,7 +71,7 @@ def test_start_finish(top_level_query_handler_context):
"""
sql_executor = MockSQLExecutor(result_sets=[])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand Down Expand Up @@ -103,7 +103,7 @@ def test_start_finish_cleanup_queries(temporary_schema_name, top_level_query_han
"""
sql_executor = MockSQLExecutor(result_sets=[MockResultSet()])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_start_error_cleanup_queries(temporary_schema_name, top_level_query_hand
"""
sql_executor = MockSQLExecutor(result_sets=[MockResultSet()])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand Down Expand Up @@ -188,7 +188,7 @@ def test_continue_finish(temporary_schema_name, top_level_query_handler_context)
drop_input_query_view_result_set
])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand All @@ -199,13 +199,13 @@ def test_continue_finish(temporary_schema_name, top_level_query_handler_context)
sql_executor.queries == [
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS
SELECT 1 as "a";
"""
),
cleandoc(
f"""
SELECT\x20
SELECT
"a"
FROM "{temporary_schema_name}"."temp_db_object_2_1";
"""),
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_continue_wrong_columns(temporary_schema_name, top_level_query_handler_c
drop_input_query_view_result_set
])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand Down Expand Up @@ -302,7 +302,7 @@ def test_continue_query_list(temporary_schema_name, top_level_query_handler_cont
drop_input_query_view_result_set
])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand All @@ -314,12 +314,12 @@ def test_continue_query_list(temporary_schema_name, top_level_query_handler_cont
f"""SELECT 1""",
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS
SELECT 1 as "a";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"a"
FROM "{temporary_schema_name}"."temp_db_object_2_1";
"""),
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_continue_error_cleanup_queries(temporary_schema_name, top_level_query_h
drop_input_query_view_result_set
])
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand All @@ -378,12 +378,12 @@ def test_continue_error_cleanup_queries(temporary_schema_name, top_level_query_h
assert sql_executor.queries == [
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS
SELECT 1 as "a";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"a"
FROM "{temporary_schema_name}"."temp_db_object_2_1";
"""),
Expand Down Expand Up @@ -453,7 +453,7 @@ def test_continue_continue_finish(temporary_schema_name, top_level_query_handler
])
temporary_schema_name = "temp_schema_name"
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand All @@ -464,24 +464,24 @@ def test_continue_continue_finish(temporary_schema_name, top_level_query_handler
sql_executor.queries == [
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_2_1" AS
SELECT 1 as "a";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"a"
FROM "{temporary_schema_name}"."temp_db_object_2_1";
"""),
f"""DROP VIEW IF EXISTS "{temporary_schema_name}"."temp_db_object_2_1";""",
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_4_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_4_1" AS
SELECT 1 as "b";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"b"
FROM "{temporary_schema_name}"."temp_db_object_4_1";
"""),
Expand Down Expand Up @@ -552,7 +552,7 @@ def test_continue_cleanup_continue_finish(temporary_schema_name, top_level_query
])
temporary_schema_name = "temp_schema_name"
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand All @@ -563,25 +563,25 @@ def test_continue_cleanup_continue_finish(temporary_schema_name, top_level_query
sql_executor.queries == [
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_4_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_4_1" AS
SELECT 1 as "a";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"a"
FROM "{temporary_schema_name}"."temp_db_object_4_1";
"""),
f"""DROP VIEW IF EXISTS "{temporary_schema_name}"."temp_db_object_4_1";""",
f"""DROP TABLE IF EXISTS "{temporary_schema_name}"."temp_db_object_2_1";""",
cleandoc(
f"""
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_6_1" AS\x20
CREATE OR REPLACE VIEW "{temporary_schema_name}"."temp_db_object_6_1" AS
SELECT 1 as "b";
"""),
cleandoc(
f"""
SELECT\x20
SELECT
"b"
FROM "{temporary_schema_name}"."temp_db_object_6_1";
"""),
Expand All @@ -607,7 +607,7 @@ def test_fail_in_cleanup(temporary_schema_name, top_level_query_handler_context)
sql_executor = MockSQLExecutor()
temporary_schema_name = "temp_schema_name"
test_input = TestInput()
query_handler_runner = MockQueryHandlerRunner[TestInput, TestOutput](
query_handler_runner = PythonQueryHandlerRunner[TestInput, TestOutput](
sql_executor=sql_executor,
top_level_query_handler_context=top_level_query_handler_context,
parameter=test_input,
Expand Down
Loading

0 comments on commit e299f38

Please sign in to comment.