def test___hash__(self):
     client = mock.MagicMock()
     client.__hash__.return_value = 234566789
     reference = self._make_reference("hi", "bye", client=client)
     data = {"zoop": 83}
     update_time = DatetimeWithNanoseconds.from_timestamp_pb(
         timestamp_pb2.Timestamp(seconds=123456, nanos=123456789))
     snapshot = self._make_one(reference, data, True, None,
                               mock.sentinel.create_time, update_time)
     self.assertEqual(hash(snapshot),
                      hash(reference) + hash(123456) + hash(123456789))
Esempio n. 2
0
def _get_value_from_value_pb(pb):
    """Given a protobuf for a Value, get the correct value.

    The Cloud Datastore Protobuf API returns a Property Protobuf which
    has one value set and the rest blank.  This function retrieves the
    the one value provided.

    Some work is done to coerce the return value into a more useful type
    (particularly in the case of a timestamp value, or a key value).

    :type pb: :class:`.entity_pb2.Value._pb`
    :param pb: The *raw* Value Protobuf.

    :rtype: object
    :returns: The value provided by the Protobuf.
    :raises: :class:`ValueError <exceptions.ValueError>` if no value type
             has been set.
    """
    value_type = pb.WhichOneof("value_type")

    if value_type == "timestamp_value":
        result = DatetimeWithNanoseconds.from_timestamp_pb(pb.timestamp_value)

    elif value_type == "key_value":
        result = key_from_protobuf(pb.key_value)

    elif value_type == "boolean_value":
        result = pb.boolean_value

    elif value_type == "double_value":
        result = pb.double_value

    elif value_type == "integer_value":
        result = pb.integer_value

    elif value_type == "string_value":
        result = pb.string_value

    elif value_type == "blob_value":
        result = pb.blob_value

    elif value_type == "entity_value":
        result = entity_from_protobuf(pb.entity_value)

    elif value_type == "array_value":
        result = [
            _get_value_from_value_pb(item_value)
            for item_value in pb.array_value.values
        ]

    elif value_type == "geo_point_value":
        result = GeoPoint(
            pb.geo_point_value.latitude,
            pb.geo_point_value.longitude,
        )

    elif value_type == "null_value":
        result = None

    else:
        raise ValueError("Value protobuf did not have any value set")

    return result