Skip to content

Commit 1b89131

Browse files
authored
Fix asynch error to comply with pep dbapi
1 parent 523559e commit 1b89131

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

clickhouse_sqlalchemy/drivers/asynch/base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import asynch
2-
31
from sqlalchemy.sql.elements import TextClause
42
from sqlalchemy.pool import AsyncAdaptedQueuePool
53

@@ -25,7 +23,7 @@ class ClickHouseDialect_asynch(ClickHouseDialect_native):
2523

2624
@classmethod
2725
def import_dbapi(cls):
28-
return AsyncAdapt_asynch_dbapi(asynch)
26+
return AsyncAdapt_asynch_dbapi()
2927

3028
@classmethod
3129
def get_pool_class(cls, url):

clickhouse_sqlalchemy/drivers/asynch/connector.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
import asynch
4+
import asynch.errors
35
from sqlalchemy.engine.interfaces import AdaptedConnection
46
from sqlalchemy.util.concurrency import await_only
57

@@ -109,15 +111,12 @@ def fetchall(self):
109111

110112

111113
class AsyncAdapt_asynch_dbapi:
112-
def __init__(self, asynch):
113-
self.asynch = asynch
114+
def __init__(self):
114115
self.paramstyle = 'pyformat'
115116
self._init_dbapi_attributes()
116117

117-
class Error(Exception):
118-
pass
119-
120118
def _init_dbapi_attributes(self):
119+
self.Error = asynch.errors.ClickHouseException
121120
for name in (
122121
'ServerException',
123122
'UnexpectedPacketFromServerError',
@@ -141,12 +140,12 @@ def _init_dbapi_attributes(self):
141140
'ProgrammingError',
142141
'NotSupportedError',
143142
):
144-
setattr(self, name, getattr(self.asynch.errors, name))
143+
setattr(self, name, getattr(asynch.errors, name))
145144

146145
def connect(self, *args, **kwargs) -> 'AsyncAdapt_asynch_connection':
147146
return AsyncAdapt_asynch_connection(
148147
self,
149-
await_only(self.asynch.connect(*args, **kwargs))
148+
await_only(asynch.connect(*args, **kwargs))
150149
)
151150

152151

tests/drivers/asynch/test_insert.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from sqlalchemy import Column, func, text
2-
3-
from clickhouse_sqlalchemy import engines, types, Table
1+
import sqlalchemy.exc
42
from asynch.errors import TypeMismatchError
3+
from sqlalchemy import Column, func, text
54

5+
from clickhouse_sqlalchemy import Table, engines, types
66
from tests.testcase import AsynchSessionTestCase
77

88

@@ -45,17 +45,19 @@ async def test_types_check(self):
4545
await self.run_sync(metadata.drop_all)
4646
await self.run_sync(metadata.create_all)
4747

48-
with self.assertRaises(TypeMismatchError) as ex:
48+
with self.assertRaises(sqlalchemy.exc.DBAPIError) as ex:
4949
await self.session.execute(
5050
table.insert(),
5151
[{'x': -1}],
5252
execution_options=dict(types_check=True),
5353
)
54+
self.assertTrue(isinstance(ex.exception.orig, TypeMismatchError))
5455
self.assertIn('-1 for column "x"', str(ex.exception))
5556

56-
with self.assertRaises(TypeMismatchError) as ex:
57+
with self.assertRaises(sqlalchemy.exc.DBAPIError) as ex:
5758
await self.session.execute(table.insert(), {'x': -1})
59+
self.assertTrue(isinstance(ex.exception.orig, TypeMismatchError))
5860
self.assertIn(
5961
'Repeat query with types_check=True for detailed info',
6062
str(ex.exception)
61-
)
63+
)

0 commit comments

Comments
 (0)