def test_found(self): from google.cloud.firestore_v1.types import document from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1.document import DocumentSnapshot now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) ref_string = self._dummy_ref_string() document_pb = document.Document( name=ref_string, fields={ "foo": document.Value(double_value=1.5), "bar": document.Value(string_value=u"skillz"), }, create_time=create_time, update_time=update_time, ) response_pb = _make_batch_response(found=document_pb, read_time=read_time) reference_map = {ref_string: mock.sentinel.reference} snapshot = self._call_fut(response_pb, reference_map) self.assertIsInstance(snapshot, DocumentSnapshot) 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.timestamp_pb(), read_time) self.assertEqual(snapshot.create_time.timestamp_pb(), create_time) self.assertEqual(snapshot.update_time.timestamp_pb(), update_time)
def test__parse_batch_get_found(): from google.cloud.firestore_v1.types import document from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1.document import DocumentSnapshot from google.cloud.firestore_v1.base_client import _parse_batch_get now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) ref_string = _dummy_ref_string() document_pb = document.Document( name=ref_string, fields={ "foo": document.Value(double_value=1.5), "bar": document.Value(string_value=u"skillz"), }, create_time=create_time, update_time=update_time, ) response_pb = _make_batch_response(found=document_pb, read_time=read_time) reference_map = {ref_string: mock.sentinel.reference} snapshot = _parse_batch_get(response_pb, reference_map, mock.sentinel.client) assert isinstance(snapshot, DocumentSnapshot) assert snapshot._reference is mock.sentinel.reference assert snapshot._data == {"foo": 1.5, "bar": u"skillz"} assert snapshot._exists assert snapshot.read_time.timestamp_pb() == read_time assert snapshot.create_time.timestamp_pb() == create_time assert snapshot.update_time.timestamp_pb() == update_time
def _timestamp_value(seconds, nanos): from google.cloud.firestore_v1.types import document from google.protobuf import timestamp_pb2 return document.Value( timestamp_value=timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos) )
def _timestamp_value(seconds, nanos): return document.Value( timestamp_value=timestamp_pb2.Timestamp(seconds=seconds, nanos=nanos) )
def _reference_value(r): return document.Value(reference_value=r)
def encode_value(value) -> types.document.Value: """Converts a native Python value into a Firestore protobuf ``Value``. Args: value (Union[NoneType, bool, int, float, datetime.datetime, \ str, bytes, dict, ~google.cloud.Firestore.GeoPoint]): A native Python value to convert to a protobuf field. Returns: ~google.cloud.firestore_v1.types.Value: A value encoded as a Firestore protobuf. Raises: TypeError: If the ``value`` is not one of the accepted types. """ if value is None: return document.Value(null_value=struct_pb2.NULL_VALUE) # Must come before int since ``bool`` is an integer subtype. if isinstance(value, bool): return document.Value(boolean_value=value) if isinstance(value, int): return document.Value(integer_value=value) if isinstance(value, float): return document.Value(double_value=value) if isinstance(value, DatetimeWithNanoseconds): return document.Value(timestamp_value=value.timestamp_pb()) if isinstance(value, datetime.datetime): return document.Value(timestamp_value=_datetime_to_pb_timestamp(value)) if isinstance(value, str): return document.Value(string_value=value) if isinstance(value, bytes): return document.Value(bytes_value=value) # NOTE: We avoid doing an isinstance() check for a Document # here to avoid import cycles. document_path = getattr(value, "_document_path", None) if document_path is not None: return document.Value(reference_value=document_path) if isinstance(value, GeoPoint): return document.Value(geo_point_value=value.to_protobuf()) if isinstance(value, (list, tuple, set, frozenset)): value_list = tuple(encode_value(element) for element in value) value_pb = document.ArrayValue(values=value_list) return document.Value(array_value=value_pb) if isinstance(value, dict): value_dict = encode_dict(value) value_pb = document.MapValue(fields=value_dict) return document.Value(map_value=value_pb) raise TypeError( "Cannot convert to a Firestore Value", value, "Invalid type", type(value) )
def _reference_value(r): from google.cloud.firestore_v1.types import document return document.Value(reference_value=r)