Skip to content

Commit f9f9bf2

Browse files
committed
test: add test for SnapshotCheckout __exit__ checks
1 parent 82360fc commit f9f9bf2

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

tests/unit/test_database.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import mock
1919
from google.api_core import gapic_v1
20-
20+
from google.cloud.exceptions import NotFound
2121
from google.cloud.spanner_v1.param_types import INT64
2222
from google.api_core.retry import Retry
2323

@@ -1792,6 +1792,49 @@ class Testing(Exception):
17921792

17931793
self.assertIs(pool._session, session)
17941794

1795+
def test_context_mgr_session_not_found_error(self):
1796+
from google.cloud.spanner_v1.database import SnapshotCheckout
1797+
from google.cloud.exceptions import NotFound
1798+
1799+
database = _Database(self.DATABASE_NAME)
1800+
session = _Session(database, name="session-1")
1801+
session.exists = mock.MagicMock(return_value=False)
1802+
pool = database._pool = _Pool()
1803+
new_session = _Session(database, name="session-2")
1804+
new_session.create = mock.MagicMock(return_value=[])
1805+
pool._new_session = mock.MagicMock(return_value=new_session)
1806+
1807+
pool.put(session)
1808+
checkout = self._make_one(database)
1809+
1810+
self.assertEqual(pool._session, session)
1811+
with self.assertRaises(NotFound):
1812+
with checkout as snapshot:
1813+
raise NotFound(f"Session not found")
1814+
# Assert that session-1 was removed from pool and new session was added.
1815+
self.assertEqual(pool._session, new_session)
1816+
1817+
def test_context_mgr_unknown_error(self):
1818+
from google.cloud.spanner_v1.database import SnapshotCheckout
1819+
1820+
database = _Database(self.DATABASE_NAME)
1821+
session = _Session(database)
1822+
pool = database._pool = _Pool()
1823+
pool._new_session = mock.MagicMock(return_value=[])
1824+
pool.put(session)
1825+
checkout = self._make_one(database)
1826+
1827+
class Testing(Exception):
1828+
pass
1829+
1830+
self.assertEqual(pool._session, session)
1831+
with self.assertRaises(Testing):
1832+
with checkout as snapshot:
1833+
raise Testing(f"Session not found")
1834+
# Assert that session-1 was not removed from pool.
1835+
self.assertEqual(pool._session, session)
1836+
pool._new_session.assert_not_called()
1837+
17951838

17961839
class TestBatchSnapshot(_BaseTest):
17971840
TABLE = "table_name"

0 commit comments

Comments
 (0)