Skip to content

Commit d2c1dad

Browse files
committed
SA14/Tests: Adjust DialectTest mechanics to mitigate weird NPE
Croaks otherwise:: File "/path/to/site-packages/sqlalchemy/testing/assertions.py", line 301, in in_ assert a in b, msg or "%r not in %r" % (a, b) TypeError: argument of type 'NoneType' is not iterable Now, this test case uses the same way to provide a fake Cursor instance like the others. However, there is a special requirement here that the fake Cursor should set `self.executed_statement`, when invoked. So, providing the fake cursor is done through a wrapper function, patching the corresponding `.execute()` methods, like before.
1 parent d0a5b56 commit d2c1dad

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/crate/client/sqlalchemy/tests/dialect_test.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,42 @@
2121

2222
from datetime import datetime
2323
from unittest import TestCase
24-
from unittest.mock import MagicMock
24+
from unittest.mock import MagicMock, patch
2525

2626
import sqlalchemy as sa
27+
28+
from crate.client.cursor import Cursor
29+
from crate.client.sqlalchemy.sa_version import SA_VERSION, SA_1_4
2730
from crate.client.sqlalchemy.types import Object
2831
from sqlalchemy import inspect
2932
from sqlalchemy.ext.declarative import declarative_base
3033
from sqlalchemy.orm import Session
3134
from sqlalchemy.testing import eq_, in_
3235

3336
fake_cursor = MagicMock(name='fake_cursor')
37+
FakeCursor = MagicMock(name='FakeCursor', spec=Cursor)
38+
FakeCursor.return_value = fake_cursor
3439

3540

41+
@patch('crate.client.connection.Cursor', FakeCursor)
3642
class DialectTest(TestCase):
3743

44+
def execute_wrapper(self, query, *args, **kwargs):
45+
self.executed_statement = query
46+
return fake_cursor
47+
3848
def setUp(self):
3949
self.engine = sa.create_engine('crate://')
4050
self.executed_statement = None
4151

42-
def execute_wrapper(query, *args, **kwargs):
43-
self.executed_statement = query
44-
return fake_cursor
45-
46-
self.engine.execute = execute_wrapper
4752
self.connection = self.engine.connect()
48-
self.connection.execute = execute_wrapper
53+
54+
if SA_VERSION < SA_1_4:
55+
self.engine.execute = self.execute_wrapper
56+
self.connection.execute = self.execute_wrapper
57+
else:
58+
fake_cursor.execute = self.execute_wrapper
59+
4960
self.base = declarative_base(bind=self.engine)
5061

5162
class Character(self.base):

0 commit comments

Comments
 (0)