Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Begin using new microgenerator for v2 firestore #91

Merged
merged 15 commits into from Jul 14, 2020
Merged
4 changes: 2 additions & 2 deletions google/cloud/firestore_v1/__init__.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

# Copyright 2020 Google LLC
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -47,7 +47,7 @@
from google.cloud.firestore_v1.watch import Watch


# TODO(microgen): this is all on the generated surface. We require this to match
# TODO(https://github.com/googleapis/python-firestore/issues/93): this is all on the generated surface. We require this to match
# firestore.py. So comment out until needed on customer level for certain.
# from .services.firestore import FirestoreClient
# from .types.common import DocumentMask
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/firestore_v1/base_document.py
Expand Up @@ -243,7 +243,8 @@ def __eq__(self, other):
return self._reference == other._reference and self._data == other._data

def __hash__(self):
# TODO(microgen): maybe add datetime_with_nanos to protoplus, revisit
# TODO(microgen, https://github.com/googleapis/proto-plus-python/issues/38):
# maybe add datetime_with_nanos to protoplus, revisit
# seconds = self.update_time.seconds
# nanos = self.update_time.nanos
seconds = int(self.update_time.timestamp())
Expand Down
44 changes: 0 additions & 44 deletions google/cloud/firestore_v1/base_query.py
Expand Up @@ -778,50 +778,6 @@ def _comparator(self, doc1, doc2):
return orderBy.direction * comp

return 0
_orders = self._orders

# Add implicit sorting by name, using the last specified direction.
if len(_orders) == 0:
lastDirection = BaseQuery.ASCENDING
else:
if _orders[-1].direction == 1:
lastDirection = BaseQuery.ASCENDING
else:
lastDirection = BaseQuery.DESCENDING

orderBys = list(_orders)

order_pb = query.StructuredQuery.Order(
field=query.StructuredQuery.FieldReference(field_path="id"),
direction=_enum_from_direction(lastDirection),
)
orderBys.append(order_pb)

for orderBy in orderBys:
if orderBy.field.field_path == "id":
# If ordering by docuent id, compare resource paths.
comp = Order()._compare_to(doc1.reference._path, doc2.reference._path)
else:
if (
orderBy.field.field_path not in doc1._data
or orderBy.field.field_path not in doc2._data
):
raise ValueError(
"Can only compare fields that exist in the "
"DocumentSnapshot. Please include the fields you are "
"ordering on in your select() call."
)
v1 = doc1._data[orderBy.field.field_path]
v2 = doc2._data[orderBy.field.field_path]
encoded_v1 = _helpers.encode_value(v1)
encoded_v2 = _helpers.encode_value(v2)
comp = Order().compare(encoded_v1, encoded_v2)

if comp != 0:
# 1 == Ascending, -1 == Descending
return orderBy.direction * comp

return 0


def _enum_from_op_string(op_string):
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/firestore_v1/client.py
Expand Up @@ -226,7 +226,7 @@ def get_all(self, references, field_paths=None, transaction=None):
for get_doc_response in response_iterator:
yield _parse_batch_get(get_doc_response, reference_map, self)

def collections(self,):
def collections(self):
"""List top-level collections of the client's database.

Returns:
Expand Down
4 changes: 2 additions & 2 deletions google/cloud/firestore_v1/order.py
Expand Up @@ -50,7 +50,7 @@ def from_value(value):
}

if v not in lut:
raise ValueError("Could not detect value type for " + str(v))
raise ValueError(f"Could not detect value type for {v}")
rafilong marked this conversation as resolved.
Show resolved Hide resolved
return lut[v]


Expand Down Expand Up @@ -99,7 +99,7 @@ def compare(cls, left, right):
elif value_type == "map_value":
return cls.compare_objects(left, right)
else:
raise ValueError("Unknown ``value_type``", str(value_type))
raise ValueError(f"Unknown ``value_type`` {value_type}")
crwilcox marked this conversation as resolved.
Show resolved Hide resolved

@staticmethod
def compare_blobs(left, right):
Expand Down
3 changes: 2 additions & 1 deletion google/cloud/firestore_v1beta1/document.py
Expand Up @@ -570,7 +570,8 @@ def __eq__(self, other):
return self._reference == other._reference and self._data == other._data

def __hash__(self):
# TODO(microgen): maybe add datetime_with_nanos to protoplus, revisit
# TODO(microgen, https://github.com/googleapis/proto-plus-python/issues/38):
# maybe add datetime_with_nanos to protoplus, revisit
# seconds = self.update_time.seconds
# nanos = self.update_time.nanos
seconds = int(self.update_time.timestamp())
Expand Down
24 changes: 12 additions & 12 deletions tests/unit/v1/test_collection.py
Expand Up @@ -261,9 +261,9 @@ def test_get(self, query_class):
get_response = collection.get()

query_class.assert_called_once_with(collection)
query_inst = query_class.return_value
self.assertIs(get_response, query_inst.stream.return_value)
query_inst.stream.assert_called_once_with(transaction=None)
query_instance = query_class.return_value
self.assertIs(get_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=None)

# Verify the deprecation
self.assertEqual(len(warned), 1)
Expand All @@ -279,9 +279,9 @@ def test_get_with_transaction(self, query_class):
get_response = collection.get(transaction=transaction)

query_class.assert_called_once_with(collection)
query_inst = query_class.return_value
self.assertIs(get_response, query_inst.stream.return_value)
query_inst.stream.assert_called_once_with(transaction=transaction)
query_instance = query_class.return_value
self.assertIs(get_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=transaction)

# Verify the deprecation
self.assertEqual(len(warned), 1)
Expand All @@ -293,9 +293,9 @@ def test_stream(self, query_class):
stream_response = collection.stream()

query_class.assert_called_once_with(collection)
query_inst = query_class.return_value
self.assertIs(stream_response, query_inst.stream.return_value)
query_inst.stream.assert_called_once_with(transaction=None)
query_instance = query_class.return_value
self.assertIs(stream_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=None)

@mock.patch("google.cloud.firestore_v1.query.Query", autospec=True)
def test_stream_with_transaction(self, query_class):
Expand All @@ -304,9 +304,9 @@ def test_stream_with_transaction(self, query_class):
stream_response = collection.stream(transaction=transaction)

query_class.assert_called_once_with(collection)
query_inst = query_class.return_value
self.assertIs(stream_response, query_inst.stream.return_value)
query_inst.stream.assert_called_once_with(transaction=transaction)
query_instance = query_class.return_value
self.assertIs(stream_response, query_instance.stream.return_value)
query_instance.stream.assert_called_once_with(transaction=transaction)

@mock.patch("google.cloud.firestore_v1.collection.Watch", autospec=True)
def test_on_snapshot(self, watch):
Expand Down