From 5b37f6dab179368ada3e982d17c5897c8e683260 Mon Sep 17 00:00:00 2001 From: KonstantAnxiety Date: Wed, 15 Nov 2023 22:12:34 +0300 Subject: [PATCH] Add mysql DBA tests --- .../core/async_adapters_mysql.py | 8 +++++++- .../core/error_transformer.py | 14 ++++++++++++++ .../db/core/test_adapter.py | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/dl_connector_mysql/dl_connector_mysql_tests/db/core/test_adapter.py diff --git a/lib/dl_connector_mysql/dl_connector_mysql/core/async_adapters_mysql.py b/lib/dl_connector_mysql/dl_connector_mysql/core/async_adapters_mysql.py index 458436520..32eb1168d 100644 --- a/lib/dl_connector_mysql/dl_connector_mysql/core/async_adapters_mysql.py +++ b/lib/dl_connector_mysql/dl_connector_mysql/core/async_adapters_mysql.py @@ -146,11 +146,17 @@ async def _execute_by_steps(self, db_adapter_query: DBAdapterQuery) -> AsyncIter chunk_size = db_adapter_query.get_effective_chunk_size(self._default_chunk_size) query = db_adapter_query.query + debug_compiled_query = db_adapter_query.debug_compiled_query escape_percent = not db_adapter_query.is_dashsql_query # DON'T escape only for dashsql compiled_query, compiled_query_parameters = compile_mysql_query( query, dialect=self._dialect, escape_percent=escape_percent ) - debug_query = query if isinstance(query, str) else compile_query_for_debug(query, self._dialect) + debug_query = None + if self._target_dto.pass_db_query_to_user: + if debug_compiled_query is not None: + debug_query = debug_compiled_query + else: + debug_query = query if isinstance(query, str) else compile_query_for_debug(query, self._dialect) with self.handle_execution_error(debug_query): async with self._get_connection(db_adapter_query.db_name) as conn: diff --git a/lib/dl_connector_mysql/dl_connector_mysql/core/error_transformer.py b/lib/dl_connector_mysql/dl_connector_mysql/core/error_transformer.py index c51d45edd..d23cb333a 100644 --- a/lib/dl_connector_mysql/dl_connector_mysql/core/error_transformer.py +++ b/lib/dl_connector_mysql/dl_connector_mysql/core/error_transformer.py @@ -36,6 +36,16 @@ def _(exc: Exception) -> bool: return _ +def is_source_connect_async_error() -> ExcMatchCondition: + def _(exc: Exception) -> bool: + if isinstance(exc, pymysql.OperationalError): + if len(exc.args) >= 2 and exc.args[0] == 2003: + return True + return False + + return _ + + class AsyncMysqlChainedDbErrorTransformer(error_transformer.ChainedDbErrorTransformer): @staticmethod def _get_error_kw( @@ -53,6 +63,10 @@ def _get_error_kw( async_mysql_db_error_transformer: DbErrorTransformer = AsyncMysqlChainedDbErrorTransformer( ( + Rule( + when=is_source_connect_async_error(), + then_raise=exc.SourceConnectError, + ), Rule( when=is_table_does_not_exist_async_error(), then_raise=MysqlSourceDoesNotExistError, diff --git a/lib/dl_connector_mysql/dl_connector_mysql_tests/db/core/test_adapter.py b/lib/dl_connector_mysql/dl_connector_mysql_tests/db/core/test_adapter.py new file mode 100644 index 000000000..be0725e0d --- /dev/null +++ b/lib/dl_connector_mysql/dl_connector_mysql_tests/db/core/test_adapter.py @@ -0,0 +1,19 @@ +from dl_core_testing.testcases.adapter import BaseAsyncAdapterTestClass +from dl_testing.regulated_test import RegulatedTestParams + +from dl_connector_mysql.core.async_adapters_mysql import AsyncMySQLAdapter +from dl_connector_mysql.core.target_dto import MySQLConnTargetDTO +from dl_connector_mysql_tests.db.core.base import BaseMySQLTestClass + + +class TestAsyncMySQLAdapter( + BaseMySQLTestClass, + BaseAsyncAdapterTestClass[MySQLConnTargetDTO], +): + test_params = RegulatedTestParams( + mark_tests_skipped={ + BaseAsyncAdapterTestClass.test_default_pass_db_query_to_user: "Not relevant", + }, + ) + + ASYNC_ADAPTER_CLS = AsyncMySQLAdapter