Skip to content

Commit

Permalink
Increase unit test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Jun 28, 2019
1 parent e169ec9 commit ab49dde
Show file tree
Hide file tree
Showing 9 changed files with 859 additions and 26 deletions.
26 changes: 26 additions & 0 deletions bigquery/google/cloud/bigquery/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import google.cloud._helpers
from google.cloud.bigquery import _helpers
from google.cloud.bigquery.model import ModelReference
from google.cloud.bigquery.routine import RoutineReference
from google.cloud.bigquery.table import TableReference


Expand Down Expand Up @@ -53,6 +54,25 @@ def _get_model_reference(self, model_id):
)


def _get_routine_reference(self, routine_id):
"""Constructs a RoutineReference.
Args:
routine_id (str): the ID of the routine.
Returns:
google.cloud.bigquery.routine.RoutineReference:
A RoutineReference for a routine in this dataset.
"""
return RoutineReference.from_api_repr(
{
"projectId": self.project,
"datasetId": self.dataset_id,
"routineId": routine_id,
}
)


class AccessEntry(object):
"""Represents grant of an access role to an entity.
Expand Down Expand Up @@ -224,6 +244,8 @@ def path(self):

model = _get_model_reference

routine = _get_routine_reference

@classmethod
def from_api_repr(cls, resource):
"""Factory: construct a dataset reference given its API representation
Expand Down Expand Up @@ -591,6 +613,8 @@ def _build_resource(self, filter_fields):

model = _get_model_reference

routine = _get_routine_reference

def __repr__(self):
return "Dataset({})".format(repr(self.reference))

Expand Down Expand Up @@ -672,3 +696,5 @@ def reference(self):
table = _get_table_reference

model = _get_model_reference

routine = _get_routine_reference
45 changes: 20 additions & 25 deletions bigquery/google/cloud/bigquery/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,19 @@ def return_type(self):
https://cloud.google.com/bigquery/docs/reference/rest/v2/routines#resource-routine
"""
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["return_type"])
if not resource:
return resource
output = google.cloud.bigquery_v2.types.StandardSqlDataType()
output = json_format.ParseDict(resource, output, ignore_unknown_fields=True)
return output

@return_type.setter
def return_type(self, value):
self._properties[
self._PROPERTY_TO_API_FIELD["return_type"]
] = json_format.MessageToDict(value)
if value:
resource = json_format.MessageToDict(value)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["return_type"]] = resource

@property
def imported_libraries(self):
Expand Down Expand Up @@ -257,6 +261,11 @@ def _build_resource(self, filter_fields):
"""Generate a resource for ``update``."""
return _helpers._build_resource_from_properties(self, filter_fields)

def __repr__(self):
return "Routine('{}.{}.{}')".format(
self.project, self.dataset_id, self.routine_id
)


class RoutineArgument(object):
"""Input/output argument of a function or a stored procedure.
Expand Down Expand Up @@ -328,15 +337,19 @@ def data_type(self):
https://cloud.google.com/bigquery/docs/reference/rest/v2/StandardSqlDataType
"""
resource = self._properties.get(self._PROPERTY_TO_API_FIELD["data_type"])
if not resource:
return resource
output = google.cloud.bigquery_v2.types.StandardSqlDataType()
output = json_format.ParseDict(resource, output, ignore_unknown_fields=True)
return output

@data_type.setter
def data_type(self, value):
self._properties[
self._PROPERTY_TO_API_FIELD["data_type"]
] = json_format.MessageToDict(value)
if value:
resource = json_format.MessageToDict(value)
else:
resource = None
self._properties[self._PROPERTY_TO_API_FIELD["data_type"]] = resource

@classmethod
def from_api_repr(cls, resource):
Expand All @@ -354,15 +367,6 @@ def from_api_repr(cls, resource):
ref._properties = resource
return ref

def to_api_repr(self):
"""Construct the API resource representation of this routine argument.
Returns:
Dict[str, object]:
Routine argument represented as an API resource.
"""
return self._properties

def __eq__(self, other):
if not isinstance(other, RoutineArgument):
return NotImplemented
Expand All @@ -374,7 +378,7 @@ def __ne__(self, other):
def __repr__(self):
all_properties = [
"{}={}".format(property_name, repr(getattr(self, property_name)))
for property_name in self._PROPERTY_TO_API_FIELD
for property_name in sorted(self._PROPERTY_TO_API_FIELD)
]
return "RoutineArgument({})".format(", ".join(all_properties))

Expand Down Expand Up @@ -458,15 +462,6 @@ def from_string(cls, routine_id, default_project=None):
{"projectId": proj, "datasetId": dset, "routineId": routine}
)

def to_api_repr(self):
"""Construct the API resource representation of this routine reference.
Returns:
Dict[str, object]:
Routine reference represented as an API resource.
"""
return self._properties

def __eq__(self, other):
"""Two RoutineReferences are equal if they point to the same routine."""
if not isinstance(other, RoutineReference):
Expand Down
1 change: 1 addition & 0 deletions bigquery/samples/update_routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def main(client, routine_id):
# Due to a limitation of the API, all fields are required, not just
# those that have been updated.
"arguments",
"language",
"type_",
"return_type",
],
Expand Down
Empty file.
Loading

0 comments on commit ab49dde

Please sign in to comment.