-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cursor must detect if the parent connection is closed (#463)
- Loading branch information
Gurov Ilya
authored
Sep 1, 2020
1 parent
acd9209
commit 6028f88
Showing
4 changed files
with
158 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
"""Connection() class unit tests.""" | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
from spanner_dbapi import connect, InterfaceError | ||
|
||
|
||
class TestConnection(unittest.TestCase): | ||
def test_close(self): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.instance.Instance.exists", | ||
return_value=True, | ||
): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.database.Database.exists", | ||
return_value=True, | ||
): | ||
connection = connect("test-instance", "test-database") | ||
|
||
self.assertFalse(connection.is_closed) | ||
connection.close() | ||
self.assertTrue(connection.is_closed) | ||
|
||
with self.assertRaises(InterfaceError): | ||
connection.cursor() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Use of this source code is governed by a BSD-style | ||
# license that can be found in the LICENSE file or at | ||
# https://developers.google.com/open-source/licenses/bsd | ||
|
||
"""Cursor() class unit tests.""" | ||
|
||
import unittest | ||
from unittest import mock | ||
|
||
from spanner_dbapi import connect, InterfaceError | ||
|
||
|
||
class TestCursor(unittest.TestCase): | ||
def test_close(self): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.instance.Instance.exists", | ||
return_value=True, | ||
): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.database.Database.exists", | ||
return_value=True, | ||
): | ||
connection = connect("test-instance", "test-database") | ||
|
||
cursor = connection.cursor() | ||
self.assertFalse(cursor.is_closed) | ||
|
||
cursor.close() | ||
|
||
self.assertTrue(cursor.is_closed) | ||
with self.assertRaises(InterfaceError): | ||
cursor.execute("SELECT * FROM database") | ||
|
||
def test_connection_closed(self): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.instance.Instance.exists", | ||
return_value=True, | ||
): | ||
with mock.patch( | ||
"google.cloud.spanner_v1.database.Database.exists", | ||
return_value=True, | ||
): | ||
connection = connect("test-instance", "test-database") | ||
|
||
cursor = connection.cursor() | ||
self.assertFalse(cursor.is_closed) | ||
|
||
connection.close() | ||
|
||
self.assertTrue(cursor.is_closed) | ||
with self.assertRaises(InterfaceError): | ||
cursor.execute("SELECT * FROM database") |