8000 refactor: update and document Cursor.executemany() method, add unit t… · FirePing32/python-spanner-django@f68f426 · GitHub
[go: up one dir, main page]

Skip to content

Commit f68f426

Browse files
author
Ilya Gurov
authored
refactor: update and document Cursor.executemany() method, add unit t… (googleapis#517)
1 parent c477cc2 commit f68f426

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

google/cloud/spanner_dbapi/cursor.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ def __enter__(self):
208208
return self
209209

210210
def __exit__(self, etype, value, traceback):
211-
self.__clear()
212-
213-
def __clear(self):
214-
self._connection = None
211+
self.close()
215212

216213
@property
217214
def description(self):
@@ -264,12 +261,20 @@ def close(self):
264261
265262
The cursor will be unusable from this point forward.
266263
"""
267-
self.__clear()
268264
self._is_closed = True
269265

270266
def executemany(self, operation, seq_of_params):
271-
if not self._connection:
272-
raise ProgrammingError("Cursor is not connected to the database")
267+
"""
268+
Execute the given SQL with every parameters set
269+
from the given sequence of parameters.
270+
271+
:type operation: :class:`str`
272+
:param operation: SQL code to execute.
273+
274+
:type seq_of_params: :class:`list`
275+
:param seq_of_params: Sequence of params to run the query with.
276+
"""
277+
self._raise_if_closed()
273278

274279
for params in seq_of_params:
275280
self.execute(operation, params)

tests/spanner_dbapi/test_cursor.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,46 @@ def test_connection_closed(self):
5252
self.assertTrue(cursor.is_closed)
5353
with self.assertRaises(InterfaceError):
5454
cursor.execute("SELECT * FROM database")
55+
56+
def test_executemany_on_closed_cursor(self):
57+
with mock.patch(
58+
"google.cloud.spanner_v1.instance.Instance.exists",
59+
return_value=True,
60+
):
61+
with mock.patch(
62+
"google.cloud.spanner_v1.database.Database.exists",
63+
return_value=True,
64+
):
65+
connection = connect("test-instance", "test-database")
66+
67+
cursor = connection.cursor()
68+
cursor.close()
69+
70+
with self.assertRaises(InterfaceError):
71+
cursor.executemany(
72+
"""SELECT * FROM table1 WHERE "col1" = @a1""", ()
73+
)
74+
75+
def test_executemany(self):
76+
operation = """SELECT * FROM table1 WHERE "col1" = @a1"""
77+
params_seq = ((1,), (2,))
78+
79+
with mock.patch(
80+
"google.cloud.spanner_v1.instance.Instance.exists",
81+
return_value=True,
82+
):
83+
with mock.patch(
84+
"google.cloud.spanner_v1.database.Database.exists",
85+
return_value=True,
86+
):
87+
connection = connect("test-instance", "test-database")
88+
89+
cursor = connection.cursor()
90+
with mock.patch(
91+
"google.cloud.spanner_dbapi.cursor.Cursor.execute"
92+
) as execute_mock:
93+
cursor.executemany(operation, params_seq)
94+
95+
execute_mock.assert_has_calls(
96+
(mock.call(operation, (1,)), mock.call(operation, (2,)))
97+
)

0 commit comments

Comments
 (0)
0