Skip to content

Commit

Permalink
bigquery: Add tests for array query parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Dec 27, 2016
1 parent 96489a1 commit 1c9087d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
7 changes: 5 additions & 2 deletions bigquery/google/cloud/bigquery/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,11 @@ def from_api_repr(cls, resource):
:returns: instance
"""
name = resource.get('name')
array_type = resource['parameterType']['arrayType']
values = resource['parameterValue']['arrayValues']
array_type = resource['parameterType']['arrayType']['type']
values = [
value['value']
for value
in resource['parameterValue']['arrayValues']]
converted = [
_CELLDATA_FROM_JSON[array_type](value, None) for value in values]
return cls(name, array_type, converted)
Expand Down
63 changes: 53 additions & 10 deletions bigquery/unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,10 +1067,20 @@ def test_from_api_repr_w_name(self):
RESOURCE = {
'name': 'foo',
'parameterType': {
'arrayType': 'INT64',
'type': 'ARRAY',
'arrayType': {
'type': 'INT64',
},
},
'parameterValue': {
'arrayValues': ['1', '2'],
'arrayValues': [
{
'value': '1',
},
{
'value': '2'
},
],
},
}
klass = self._get_target_class()
Expand All @@ -1083,10 +1093,19 @@ def test_from_api_repr_wo_name(self):
RESOURCE = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
'arrayType': {
'type': 'INT64',
},
},
'parameterValue': {
'arrayValues': ['1', '2'],
'arrayValues': [
{
'value': '1',
},
{
'value': '2'
},
],
},
}
klass = self._get_target_class()
Expand All @@ -1100,10 +1119,19 @@ def test_to_api_repr_w_name(self):
'name': 'foo',
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
'arrayType': {
'type': 'INT64',
},
},
'parameterValue': {
'arrayValues': ['1', '2'],
'arrayValues': [
{
'value': '1',
},
{
'value': '2'
},
],
},
}
param = self._make_one(name='foo', array_type='INT64', values=[1, 2])
Expand All @@ -1113,10 +1141,19 @@ def test_to_api_repr_wo_name(self):
EXPECTED = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'INT64',
'arrayType': {
'type': 'INT64',
},
},
'parameterValue': {
'arrayValues': ['1', '2'],
'arrayValues': [
{
'value': '1',
},
{
'value': '2'
},
],
},
}
klass = self._get_target_class()
Expand All @@ -1127,10 +1164,16 @@ def test_to_api_repr_w_unknown_type(self):
EXPECTED = {
'parameterType': {
'type': 'ARRAY',
'arrayType': 'UNKNOWN',
'arrayType': {
'type': 'UNKNOWN',
},
},
'parameterValue': {
'arrayValues': ['unknown'],
'arrayValues': [
{
'value': 'unknown',
}
],
},
}
klass = self._get_target_class()
Expand Down
8 changes: 8 additions & 0 deletions system_tests/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ def _job_done(instance):
def test_sync_query_w_standard_sql_types(self):
import datetime
from google.cloud._helpers import UTC
from google.cloud.bigquery._helpers import ArrayQueryParameter
from google.cloud.bigquery._helpers import ScalarQueryParameter
from google.cloud.bigquery._helpers import StructQueryParameter
naive = datetime.datetime(2016, 12, 5, 12, 41, 9)
Expand All @@ -495,6 +496,8 @@ def test_sync_query_w_standard_sql_types(self):
answer = 42
answer_param = ScalarQueryParameter(
name='answer', type_='INT64', value=answer)
array_param = ArrayQueryParameter(
name='array_param', array_type='INT64', values=[1, 2])
struct_param = StructQueryParameter(
'hitchhiker', question_param, answer_param)
EXAMPLES = [
Expand Down Expand Up @@ -570,6 +573,11 @@ def test_sync_query_w_standard_sql_types(self):
'expected': zoned,
'query_parameters': [zoned_param],
},
{
'sql': 'SELECT @array_param',
'expected': [1, 2],
'query_parameters': [array_param],
},
{
'sql': 'SELECT (@hitchhiker.question, @hitchhiker.answer)',
'expected': ({'_field_1': question, '_field_2': answer}),
Expand Down

0 comments on commit 1c9087d

Please sign in to comment.