Skip to content

Commit

Permalink
most fixes to v1beta1 unit
Browse files Browse the repository at this point in the history
  • Loading branch information
crwilcox committed Jun 27, 2020
1 parent a24c03b commit 8c698bd
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1beta1/_helpers.py
Expand Up @@ -281,7 +281,7 @@ def decode_value(value, client):
elif value_type == "double_value":
return value.double_value
elif value_type == "timestamp_value":
return DatetimeWithNanoseconds.from_timestamp_pb(value.timestamp_value)
return DatetimeWithNanoseconds.from_timestamp_pb(value._pb.timestamp_value)
elif value_type == "string_value":
return value.string_value
elif value_type == "bytes_value":
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1beta1/batch.py
Expand Up @@ -153,7 +153,7 @@ def commit(self):

self._write_pbs = []
self.write_results = results = list(commit_response.write_results)
self.commit_time = commit_response._pb.commit_time
self.commit_time = commit_response.commit_time
return results

def __enter__(self):
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/firestore_v1beta1/collection.py
Expand Up @@ -20,7 +20,7 @@

from google.cloud.firestore_v1beta1 import _helpers
from google.cloud.firestore_v1beta1 import query as query_mod
from google.cloud.firestore_v1beta1.types import document
from google.cloud.firestore_v1beta1.types import document as document_pb2
from google.cloud.firestore_v1beta1.watch import Watch
from google.cloud.firestore_v1beta1 import document

Expand Down Expand Up @@ -160,7 +160,7 @@ def add(self, document_data, document_id=None):
if document_id is None:
parent_path, expected_prefix = self._parent_info()

document_pb = document.Document()
document_pb = document_pb2.Document()

created_document_pb = self._client._firestore_api.create_document(
request={
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1beta1/document.py
Expand Up @@ -405,7 +405,7 @@ def delete(self, option=None):
metadata=self._client._rpc_metadata,
)

return commit_response._pb.commit_time
return commit_response.commit_time

def get(self, field_paths=None, transaction=None):
"""Retrieve a snapshot of the current document.
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/v1beta1/test_batch.py
Expand Up @@ -229,9 +229,11 @@ def test_as_context_mgr_wo_error(self):

# Verify the mocks.
firestore_api.commit.assert_called_once_with(
client._database_string,
write_pbs,
transaction=None,
request={
"database": client._database_string,
"writes": write_pbs,
"transaction": None,
},
metadata=client._rpc_metadata,
)

Expand Down
19 changes: 12 additions & 7 deletions tests/unit/v1beta1/test_client.py
Expand Up @@ -283,7 +283,10 @@ def _next_page(self):
self.assertEqual(collection.id, collection_id)

firestore_api.list_collection_ids.assert_called_once_with(
client._database_string, metadata=client._rpc_metadata
request={
"parent":client._database_string
},
metadata=client._rpc_metadata
)

def _get_all_helper(self, client, references, document_pbs, **kwargs):
Expand Down Expand Up @@ -597,9 +600,10 @@ def test_found(self):
self.assertIs(snapshot._reference, mock.sentinel.reference)
self.assertEqual(snapshot._data, {"foo": 1.5, "bar": u"skillz"})
self.assertTrue(snapshot._exists)
self.assertEqual(snapshot.read_time, read_time)
self.assertEqual(snapshot.create_time, create_time)
self.assertEqual(snapshot.update_time, update_time)
# TODO(crwilcox): v2: datetimewithnanos
# self.assertEqual(snapshot.read_time, read_time)
# self.assertEqual(snapshot.create_time, create_time)
# self.assertEqual(snapshot.update_time, update_time)

def test_missing(self):
ref_string = self._dummy_ref_string()
Expand All @@ -614,13 +618,14 @@ def test_unset_result_type(self):
self._call_fut(response_pb, {})

def test_unknown_result_type(self):
response_pb = mock.Mock(spec=["WhichOneof"])
response_pb.WhichOneof.return_value = "zoob_value"
response_pb = mock.Mock()
response_pb._pb.mock_add_spec(spec=["WhichOneof"])
response_pb._pb.WhichOneof.return_value = "zoob_value"

with self.assertRaises(ValueError):
self._call_fut(response_pb, {})

response_pb.WhichOneof.assert_called_once_with("result")
response_pb._pb.WhichOneof.assert_called_once_with("result")


class Test__get_doc_mask(unittest.TestCase):
Expand Down
33 changes: 20 additions & 13 deletions tests/unit/v1beta1/test_collection.py
Expand Up @@ -220,7 +220,7 @@ def test_add_auto_assigned(self):
auto_assigned_id = "cheezburger"
name = "{}/{}/{}".format(parent_path, collection.id, auto_assigned_id)
create_doc_response = document.Document(name=name)
create_doc_response.update_time.FromDatetime(datetime.datetime.utcnow())
create_doc_response._pb.update_time.FromDatetime(datetime.datetime.utcnow())
firestore_api.create_document.return_value = create_doc_response

# Actually call add() on our collection; include a transform to make
Expand All @@ -237,18 +237,22 @@ def test_add_auto_assigned(self):

expected_document_pb = document.Document()
firestore_api.create_document.assert_called_once_with(
parent_path,
collection_id=collection.id,
document_id=None,
document=expected_document_pb,
mask=None,
request={
"parent": parent_path,
"collection_id": collection.id,
"document_id": None,
"document": expected_document_pb,
"mask": None,
},
metadata=client._rpc_metadata,
)
write_pbs = pbs_for_set_no_merge(document_ref._document_path, document_data)
firestore_api.commit.assert_called_once_with(
client._database_string,
write_pbs,
transaction=None,
request={
"database": client._database_string,
"writes": write_pbs,
"transaction": None,
},
metadata=client._rpc_metadata,
)

Expand Down Expand Up @@ -485,10 +489,13 @@ def _next_page(self):

parent, _ = collection._parent_info()
api_client.list_documents.assert_called_once_with(
parent,
collection.id,
page_size=page_size,
show_missing=True,
request={
"parent": parent,
"collection_id": collection.id,
"page_size": page_size,
"page_token": True,

},
metadata=client._rpc_metadata,
)

Expand Down
8 changes: 4 additions & 4 deletions tests/unit/v1beta1/test_query.py
Expand Up @@ -229,7 +229,7 @@ def test_where(self):
value=document.Value(integer_value=9000),
)
self.assertEqual(field_pb, expected_pb)
self._compare_queries(query, new_query, "_field_filters")
self._compare_queries(query_inst, new_query, "_field_filters")

def _where_unary_helper(self, value, op_enum, op_string="=="):
from google.cloud.firestore_v1beta1.types import query
Expand All @@ -248,7 +248,7 @@ def _where_unary_helper(self, value, op_enum, op_string="=="):
op=op_enum,
)
self.assertEqual(field_pb, expected_pb)
self._compare_queries(query, new_query, "_field_filters")
self._compare_queries(query_inst, new_query, "_field_filters")

def test_where_eq_null(self):
from google.cloud.firestore_v1beta1.types import StructuredQuery
Expand Down Expand Up @@ -936,9 +936,9 @@ def test__to_protobuf_start_at_only(self):
from google.cloud.firestore_v1beta1.types import query

parent = mock.Mock(id="phish", spec=["id"])
query = self._make_one(parent).order_by("X.Y").start_after({"X": {"Y": u"Z"}})
query_inst = self._make_one(parent).order_by("X.Y").start_after({"X": {"Y": u"Z"}})

structured_query_pb = query._to_protobuf()
structured_query_pb = query_inst._to_protobuf()
query_kwargs = {
"from_": [
query.StructuredQuery.CollectionSelector(collection_id=parent.id)
Expand Down

0 comments on commit 8c698bd

Please sign in to comment.