Skip to content

Commit

Permalink
Merge pull request googleapis#2818 from tseaver/2815-bigquery-py3k-by…
Browse files Browse the repository at this point in the history
…tes-field

Fix 'BYTES' field handling on Py3k.
  • Loading branch information
tseaver authored Dec 6, 2016
2 parents 6814be4 + 54cd0a9 commit 91bd6dc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
from collections import OrderedDict
import datetime

import six

from google.cloud._helpers import _date_from_iso8601_date
from google.cloud._helpers import _datetime_from_microseconds
from google.cloud._helpers import _datetime_to_rfc3339
from google.cloud._helpers import _microseconds_from_datetime
from google.cloud._helpers import _RFC3339_NO_FRACTION
from google.cloud._helpers import _time_from_iso8601_time_naive
from google.cloud._helpers import _to_bytes


def _not_null(value, field):
Expand Down Expand Up @@ -57,7 +60,8 @@ def _string_from_json(value, _):
def _bytes_from_json(value, field):
"""Base64-decode value"""
if _not_null(value, field):
return base64.decodestring(value)
return base64.decodestring(
_to_bytes(value) if isinstance(value, six.text_type) else value)


def _timestamp_from_json(value, field):
Expand Down
9 changes: 8 additions & 1 deletion bigquery/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,20 @@ def test_w_none_required(self):
with self.assertRaises(TypeError):
self._call_fut(None, _Field('REQUIRED'))

def test_w_base64_encoded_value(self):
def test_w_base64_encoded_bytes(self):
import base64
expected = b'Wonderful!'
encoded = base64.encodestring(expected)
coerced = self._call_fut(encoded, object())
self.assertEqual(coerced, expected)

def test_w_base64_encoded_text(self):
import base64
expected = b'Wonderful!'
encoded = base64.encodestring(expected).decode('ascii')
coerced = self._call_fut(encoded, object())
self.assertEqual(coerced, expected)


class Test_timestamp_from_json(unittest.TestCase):

Expand Down

0 comments on commit 91bd6dc

Please sign in to comment.