Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 16 additions & 4 deletions google/cloud/bigquery/dbapi/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,28 @@
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 `int` object to binary")

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


def TimeFromTicks(ticks, tz=None):
Expand Down
32 changes: 28 additions & 4 deletions tests/unit/test_dbapi_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import datetime
import unittest

import pytest

import google.cloud._helpers
from google.cloud.bigquery.dbapi import types

Expand All @@ -26,10 +28,6 @@ def test_binary_type(self):
self.assertEqual("STRUCT", types.BINARY)
self.assertNotEqual("STRING", types.BINARY)

def test_binary_constructor(self):
self.assertEqual(types.Binary(u"hello"), b"hello")
self.assertEqual(types.Binary(u"\u1f60"), u"\u1f60".encode("utf-8"))

def test_timefromticks(self):
somedatetime = datetime.datetime(
2017, 2, 18, 12, 47, 26, tzinfo=google.cloud._helpers.UTC
Expand All @@ -40,3 +38,29 @@ def test_timefromticks(self):
types.TimeFromTicks(ticks, google.cloud._helpers.UTC),
datetime.time(12, 47, 26, tzinfo=google.cloud._helpers.UTC),
)


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


@pytest.mark.parametrize(
"raw,expected",
[
(u"hello", b"hello"),
(u"\u1f60", u"\u1f60".encode("utf-8")),
(b"hello", b"hello"),
(bytearray(b"hello"), b"hello"),
(memoryview(b"hello"), b"hello"),
(CustomBinary(), b"Google"),
],
)
def test_binary_constructor(raw, expected):
assert types.Binary(raw) == expected


@pytest.mark.parametrize("bad", (42, 42.0, None))
def test_invalid_binary_constructor(bad):
with pytest.raises(TypeError):
types.Binary(bad)