Skip to content

Commit

Permalink
fix(db_api): emit warning instead of an exception for rowcount prop…
Browse files Browse the repository at this point in the history
…erty (#628)

See for more context: googleapis/python-spanner-sqlalchemy#134
  • Loading branch information
Ilya Gurov authored Oct 26, 2021
1 parent 134a6e4 commit 62ff9ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 7 additions & 4 deletions google/cloud/spanner_dbapi/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

"""Database cursor for Google Cloud Spanner DB-API."""

import warnings
from collections import namedtuple

import sqlparse

from google.api_core.exceptions import Aborted
Expand All @@ -23,8 +26,6 @@
from google.api_core.exceptions import InvalidArgument
from google.api_core.exceptions import OutOfRange

from collections import namedtuple

from google.cloud import spanner_v1 as spanner
from google.cloud.spanner_dbapi.checksum import ResultsChecksum
from google.cloud.spanner_dbapi.exceptions import IntegrityError
Expand Down Expand Up @@ -120,10 +121,12 @@ def rowcount(self):
:raises: :class:`NotImplemented`.
"""
raise NotImplementedError(
warnings.warn(
"The `rowcount` property is non-operational. Request "
"resulting rows are streamed by the `fetch*()` methods "
"and can't be counted before they are all streamed."
"and can't be counted before they are all streamed.",
UserWarning,
stacklevel=2,
)

def _raise_if_closed(self):
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/spanner_dbapi/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,19 @@ def test_property_description(self):
self.assertIsNotNone(cursor.description)
self.assertIsInstance(cursor.description[0], ColumnInfo)

def test_property_rowcount(self):
@mock.patch("warnings.warn")
def test_property_rowcount(self, warn_mock):
connection = self._make_connection(self.INSTANCE, self.DATABASE)
cursor = self._make_one(connection)
with self.assertRaises(NotImplementedError):
cursor.rowcount

cursor.rowcount
warn_mock.assert_called_once_with(
"The `rowcount` property is non-operational. Request "
"resulting rows are streamed by the `fetch*()` methods "
"and can't be counted before they are all streamed.",
UserWarning,
stacklevel=2,
)

def test_callproc(self):
from google.cloud.spanner_dbapi.exceptions import InterfaceError
Expand Down

0 comments on commit 62ff9ae

Please sign in to comment.