Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: The DB API Binary function accepts bytes data #630

Merged
merged 8 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions google/cloud/bigquery/dbapi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@
TimestampFromTicks = datetime.datetime.fromtimestamp


def Binary(string):
def Binary(data):
"""Contruct a DB-API binary value.

Args:
string (str): A string to encode as a binary value.
data (bytes-like): An object containing binary data and that
can be converted to bytes with the `bytes` builtin.

Returns:
bytes: The UTF-8 encoded bytes representing the string.
bytes: The binary data as a bytes object.
"""
return string.encode("utf-8")
if isinstance(data, int):
# This is not the conversion we're looking for, because it
# will simply create a bytes object of the given size.
raise TypeError("cannot convert 'decimal.Decimal' object to binary")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isinstance is checking for int, but the error says decimal.Decimal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

try:
return bytes(data)
except TypeError:
if isinstance(data, str):
return data.encode("utf-8")
else:
raise


def TimeFromTicks(ticks, tz=None):
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_dbapi_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import datetime
import pytest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We usually put pytest in the second grouping of imports rather than with the built-ins. We do try to follow PEP-8 advice.

Imports should be grouped in the following order:

  1. Standard library imports.
  2. Related third party imports.
  3. Local application/library specific imports.

You should put a blank line between each group of imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

import unittest

import google.cloud._helpers
Expand All @@ -29,6 +30,19 @@ def test_binary_type(self):
def test_binary_constructor(self):
self.assertEqual(types.Binary(u"hello"), b"hello")
self.assertEqual(types.Binary(u"\u1f60"), u"\u1f60".encode("utf-8"))
self.assertEqual(types.Binary(b"hello"), b"hello")
self.assertEqual(types.Binary(bytearray(b"hello")), b"hello")
self.assertEqual(types.Binary(memoryview(b"hello")), b"hello")

class C:
def __bytes__(self):
return b"Google"

self.assertEqual(types.Binary(C()), b"Google")

for bad in 42, 42.0, None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect the tests for "bad inputs" to be in a separate test function.

Ideally both of these (good inputs, bad inputs) would be parameterized in pytest-style tests. I think we can actually mix those in to the same file, so we can do that without refactoring everything, especially since these tests don't require any of the setup from the unit test class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

with pytest.raises(TypeError):
types.Binary(bad)

def test_timefromticks(self):
somedatetime = datetime.datetime(
Expand Down