Skip to content

Commit 76129db

Browse files
committed
Tests: Improve setup of fake cursor within DialectTest test cases
The test cases use a shared instance of a fake cursor, which is needed to satisfy CrateDB version inquiry of the driver early on, so it needs to be established _before_ invoking `sa.create_engine()` / `engine.connect()`. On the other hand, the _individual_ configuration of the fake cursor should happen per test case, to be able to define different values for `cursor.return_value` _and_ `cursor.description`. Beforehand, the corresponding test cases only succeeded because all used the same structure of a database response, i.e. all queried only a _single_ data column. Now, response structures are defined individually by setting `cursor.description` within the body of each test case.
1 parent 0909618 commit 76129db

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

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

+52-21
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,21 @@
3333
from sqlalchemy.orm import Session
3434
from sqlalchemy.testing import eq_, in_
3535

36-
fake_cursor = MagicMock(name='fake_cursor')
37-
fake_cursor.description = (
38-
('foo', None, None, None, None, None, None),
39-
)
4036
FakeCursor = MagicMock(name='FakeCursor', spec=Cursor)
41-
FakeCursor.return_value = fake_cursor
4237

4338

4439
@patch('crate.client.connection.Cursor', FakeCursor)
4540
class DialectTest(TestCase):
4641

4742
def execute_wrapper(self, query, *args, **kwargs):
4843
self.executed_statement = query
49-
return fake_cursor
44+
return self.fake_cursor
5045

5146
def setUp(self):
47+
48+
self.fake_cursor = MagicMock(name='fake_cursor')
49+
FakeCursor.return_value = self.fake_cursor
50+
5251
self.engine = sa.create_engine('crate://')
5352
self.executed_statement = None
5453

@@ -58,7 +57,7 @@ def setUp(self):
5857
self.engine.execute = self.execute_wrapper
5958
self.connection.execute = self.execute_wrapper
6059
else:
61-
fake_cursor.execute = self.execute_wrapper
60+
self.fake_cursor.execute = self.execute_wrapper
6261

6362
self.base = declarative_base(bind=self.engine)
6463

@@ -73,31 +72,56 @@ class Character(self.base):
7372
self.character = Character
7473
self.session = Session()
7574

76-
def test_pks_are_retrieved_depending_on_version_set(self):
75+
def test_pks_are_retrieved_depending_on_version_set_old(self):
76+
"""
77+
Test the old pk retrieval.
78+
"""
7779
meta = self.character.metadata
78-
79-
# test the old pk retrieval
8080
insp = inspect(meta.bind)
8181
self.engine.dialect.server_version_info = (0, 54, 0)
82-
fake_cursor.rowcount = 1
83-
fake_cursor.fetchone = MagicMock(return_value=[["id", "id2", "id3"]])
82+
83+
# Setup fake cursor.
84+
self.fake_cursor.rowcount = 1
85+
self.fake_cursor.description = (
86+
('foo', None, None, None, None, None, None),
87+
)
88+
self.fake_cursor.fetchone = MagicMock(return_value=[["id", "id2", "id3"]])
89+
90+
# Verify outcome.
8491
eq_(insp.get_pk_constraint("characters")['constrained_columns'], {"id", "id2", "id3"})
85-
fake_cursor.fetchone.assert_called_once_with()
92+
self.fake_cursor.fetchone.assert_called_once_with()
8693
in_("information_schema.table_constraints", self.executed_statement)
8794

88-
# test the new pk retrieval
95+
def test_pks_are_retrieved_depending_on_version_set_new(self):
96+
"""
97+
Test the new pk retrieval.
98+
"""
99+
meta = self.character.metadata
89100
insp = inspect(meta.bind)
90101
self.engine.dialect.server_version_info = (2, 3, 0)
91-
fake_cursor.rowcount = 3
92-
fake_cursor.fetchall = MagicMock(return_value=[["id"], ["id2"], ["id3"]])
102+
103+
# Setup fake cursor.
104+
self.fake_cursor.rowcount = 3
105+
self.fake_cursor.description = (
106+
('foo', None, None, None, None, None, None),
107+
)
108+
self.fake_cursor.fetchall = MagicMock(return_value=[["id"], ["id2"], ["id3"]])
109+
110+
# Verify outcome.
93111
eq_(insp.get_pk_constraint("characters")['constrained_columns'], {"id", "id2", "id3"})
94-
fake_cursor.fetchall.assert_called_once_with()
112+
self.fake_cursor.fetchall.assert_called_once_with()
95113
in_("information_schema.key_column_usage", self.executed_statement)
96114

97115
def test_get_table_names(self):
98-
fake_cursor.rowcount = 1
99-
fake_cursor.fetchall = MagicMock(return_value=[["t1"], ["t2"]])
100116

117+
# Setup fake cursor.
118+
self.fake_cursor.rowcount = 1
119+
self.fake_cursor.description = (
120+
('foo', None, None, None, None, None, None),
121+
)
122+
self.fake_cursor.fetchall = MagicMock(return_value=[["t1"], ["t2"]])
123+
124+
# Verify outcome.
101125
insp = inspect(self.character.metadata.bind)
102126
self.engine.dialect.server_version_info = (2, 0, 0)
103127
eq_(insp.get_table_names(schema="doc"),
@@ -117,11 +141,18 @@ def test_get_table_names(self):
117141
in_("WHERE schema_name = ? ORDER BY", self.executed_statement)
118142

119143
def test_get_view_names(self):
120-
fake_cursor.rowcount = 1
121-
fake_cursor.fetchall = MagicMock(return_value=[["v1"], ["v2"]])
122144

123145
insp = inspect(self.character.metadata.bind)
124146
self.engine.dialect.server_version_info = (2, 0, 0)
147+
148+
# Setup fake cursor.
149+
self.fake_cursor.rowcount = 1
150+
self.fake_cursor.description = (
151+
('foo', None, None, None, None, None, None),
152+
)
153+
self.fake_cursor.fetchall = MagicMock(return_value=[["v1"], ["v2"]])
154+
155+
# Verify outcome.
125156
eq_(insp.get_view_names(schema="doc"),
126157
['v1', 'v2'])
127158
eq_(self.executed_statement, "SELECT table_name FROM information_schema.views "

0 commit comments

Comments
 (0)