Skip to content

Commit df9776b

Browse files
committed
fix: don't try to close closed cursors
1 parent d5735ea commit df9776b

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

google/cloud/bigquery/dbapi/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def close(self):
7676
self._bqstorage_client._transport.grpc_channel.close()
7777

7878
for cursor_ in self._cursors_created:
79-
cursor_.close()
79+
if not cursor_._closed:
80+
cursor_.close()
8081

8182
def commit(self):
8283
"""No-op, but for consistency raise an error if connection is closed."""

tests/unit/test_dbapi_connection.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def test_ctor_w_bqstorage_client(self):
6767
mock_client = self._mock_client()
6868
mock_bqstorage_client = self._mock_bqstorage_client()
6969
connection = self._make_one(
70-
client=mock_client, bqstorage_client=mock_bqstorage_client,
70+
client=mock_client,
71+
bqstorage_client=mock_bqstorage_client,
7172
)
7273
self.assertIsInstance(connection, Connection)
7374
self.assertIs(connection._client, mock_client)
@@ -109,7 +110,8 @@ def test_connect_w_both_clients(self):
109110
mock_client = self._mock_client()
110111
mock_bqstorage_client = self._mock_bqstorage_client()
111112
connection = connect(
112-
client=mock_client, bqstorage_client=mock_bqstorage_client,
113+
client=mock_client,
114+
bqstorage_client=mock_bqstorage_client,
113115
)
114116
self.assertIsInstance(connection, Connection)
115117
self.assertIs(connection._client, mock_client)
@@ -140,7 +142,9 @@ def test_close_closes_all_created_bigquery_clients(self):
140142
return_value=client,
141143
)
142144
bqstorage_client_patcher = mock.patch.object(
143-
client, "_create_bqstorage_client", return_value=bqstorage_client,
145+
client,
146+
"_create_bqstorage_client",
147+
return_value=bqstorage_client,
144148
)
145149

146150
with client_patcher, bqstorage_client_patcher:
@@ -156,7 +160,7 @@ def test_close_closes_all_created_bigquery_clients(self):
156160
)
157161
def test_close_does_not_close_bigquery_clients_passed_to_it(self):
158162
client = self._mock_client()
159-
bqstorage_client = self._mock_bqstorage_client()
163+
bqstorage_client = sesf._mock_bqstorage_client()
160164
connection = self._make_one(client=client, bqstorage_client=bqstorage_client)
161165

162166
connection.close()
@@ -176,6 +180,19 @@ def test_close_closes_all_created_cursors(self):
176180
self.assertTrue(cursor_1._closed)
177181
self.assertTrue(cursor_2._closed)
178182

183+
def test_close_closes_only_open_created_cursors(self):
184+
connection = self._make_one(client=self._mock_client())
185+
cursor_1 = connection.cursor()
186+
cursor_2 = connection.cursor()
187+
self.assertFalse(cursor_1._closed)
188+
self.assertFalse(cursor_2._closed)
189+
190+
cursor_1.close()
191+
connection.close()
192+
193+
self.assertTrue(cursor_1._closed)
194+
self.assertTrue(cursor_2._closed)
195+
179196
def test_does_not_keep_cursor_instances_alive(self):
180197
from google.cloud.bigquery.dbapi import Cursor
181198

0 commit comments

Comments
 (0)