Skip to content
Permalink
Browse files
feat: add to_api_repr method to Model (#326)
  • Loading branch information
HemangChothani committed Oct 14, 2020
1 parent 3be78b7 commit fb401bd94477323bba68cf252dd88166495daf54
Showing with 52 additions and 0 deletions.
  1. +8 −0 google/cloud/bigquery/model.py
  2. +44 −0 tests/unit/model/test_model.py
@@ -317,6 +317,14 @@ def _build_resource(self, filter_fields):
def __repr__(self):
return "Model(reference={})".format(repr(self.reference))

def to_api_repr(self):
"""Construct the API resource representation of this model.
Returns:
Dict[str, object]: Model reference represented as an API resource
"""
return json_format.MessageToDict(self._proto)


class ModelReference(object):
"""ModelReferences are pointers to models.
@@ -318,3 +318,47 @@ def test_repr(target_class):
"Model(reference=ModelReference("
"project_id='my-proj', dataset_id='my_dset', model_id='my_model'))"
)


def test_to_api_repr(target_class):
from google.protobuf import json_format

model = target_class("my-proj.my_dset.my_model")
resource = {
"etag": "abcdefg",
"modelReference": {
"projectId": "my-project",
"datasetId": "my_dataset",
"modelId": "my_model",
},
"creationTime": "1274284800000",
"lastModifiedTime": "1317484800000",
"modelType": "LOGISTIC_REGRESSION",
"trainingRuns": [
{
"trainingOptions": {"initialLearnRate": 1.0},
"startTime": "2010-05-19T16:00:00Z",
},
{
"trainingOptions": {"initialLearnRate": 0.5},
"startTime": "2011-10-01T16:00:00Z",
},
{
"trainingOptions": {"initialLearnRate": 0.25},
"startTime": "2012-12-21T16:00:00Z",
},
],
"description": "A friendly description.",
"location": "US",
"friendlyName": "A friendly name.",
"labels": {"greeting": "こんにちは"},
"expirationTime": "1356105600000",
"encryptionConfiguration": {
"kmsKeyName": "projects/1/locations/us/keyRings/1/cryptoKeys/1"
},
}
model._proto = json_format.ParseDict(
resource, types.Model()._pb, ignore_unknown_fields=True
)
got = model.to_api_repr()
assert got == resource

0 comments on commit fb401bd

Please sign in to comment.