def test_snapshot_checkout(self): from google.cloud.spanner_dbapi import Connection connection = Connection(INSTANCE, DATABASE, read_only=True) connection.autocommit = False session_checkout = mock.MagicMock(autospec=True) connection._session_checkout = session_checkout snapshot = connection.snapshot_checkout() session_checkout.assert_called_once() self.assertEqual(snapshot, connection.snapshot_checkout()) connection.commit() self.assertIsNone(connection._snapshot) connection.snapshot_checkout() self.assertIsNotNone(connection._snapshot) connection.rollback() self.assertIsNone(connection._snapshot) connection.autocommit = True self.assertIsNone(connection.snapshot_checkout())
def test_commit(self, mock_warn): from google.cloud.spanner_dbapi import Connection from google.cloud.spanner_dbapi.connection import AUTOCOMMIT_MODE_WARNING connection = Connection(self.INSTANCE, self.DATABASE) with mock.patch( "google.cloud.spanner_dbapi.connection.Connection._release_session" ) as mock_release: connection.commit() mock_release.assert_not_called() connection._transaction = mock_transaction = mock.MagicMock( rolled_back=False, committed=False ) mock_transaction.commit = mock_commit = mock.MagicMock() with mock.patch( "google.cloud.spanner_dbapi.connection.Connection._release_session" ) as mock_release: connection.commit() mock_commit.assert_called_once_with() mock_release.assert_called_once_with() connection._autocommit = True connection.commit() mock_warn.assert_called_once_with( AUTOCOMMIT_MODE_WARNING, UserWarning, stacklevel=2 )