コード例 #1
0
def test_repeated_composite_marshaled():
    class Foo(proto.Message):
        timestamps = proto.RepeatedField(
            proto.MESSAGE,
            message=timestamp_pb2.Timestamp,
            number=1,
        )

    foo = Foo(timestamps=[
        DatetimeWithNanoseconds(2012, 4, 21, 15, tzinfo=timezone.utc)
    ])
    foo.timestamps.append(timestamp_pb2.Timestamp(seconds=86400 * 365))
    foo.timestamps.append(
        DatetimeWithNanoseconds(2017, 10, 14, tzinfo=timezone.utc))
    assert all(
        [isinstance(i, DatetimeWithNanoseconds) for i in foo.timestamps])
    assert all([
        isinstance(i, timestamp_pb2.Timestamp) for i in Foo.pb(foo).timestamps
    ])
    assert foo.timestamps[0].year == 2012
    assert foo.timestamps[0].month == 4
    assert foo.timestamps[0].hour == 15
    assert foo.timestamps[1].year == 1971
    assert foo.timestamps[1].month == 1
    assert foo.timestamps[1].hour == 0
    assert foo.timestamps[2].year == 2017
    assert foo.timestamps[2].month == 10
    assert foo.timestamps[2].hour == 0
コード例 #2
0
def test_timestamp_to_python_idempotent():
    # This path can never run in the current configuration because proto
    # values are the only thing ever saved, and `to_python` is a read method.
    #
    # However, we test idempotency for consistency with `to_proto` and
    # general resiliency.
    marshal = BaseMarshal()
    py_value = DatetimeWithNanoseconds(2012, 4, 21, 15, tzinfo=timezone.utc)
    assert marshal.to_python(timestamp_pb2.Timestamp, py_value) is py_value
コード例 #3
0
def test_timestamp_del():
    class Foo(proto.Message):
        event_time = proto.Field(
            proto.MESSAGE, number=1, message=timestamp_pb2.Timestamp,
        )

    foo = Foo(event_time=DatetimeWithNanoseconds(2012, 4, 21, 15, tzinfo=timezone.utc))
    del foo.event_time
    assert foo.event_time is None
コード例 #4
0
 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))
コード例 #5
0
def test_timestamp_write_init():
    class Foo(proto.Message):
        event_time = proto.Field(
            proto.MESSAGE, number=1, message=timestamp_pb2.Timestamp,
        )

    foo = Foo(event_time=DatetimeWithNanoseconds(2012, 4, 21, 15, tzinfo=timezone.utc))
    assert isinstance(foo.event_time, DatetimeWithNanoseconds)
    assert isinstance(Foo.pb(foo).event_time, timestamp_pb2.Timestamp)
    assert foo.event_time.year == 2012
    assert foo.event_time.month == 4
    assert foo.event_time.hour == 15
    assert Foo.pb(foo).event_time.seconds == 1335020400
コード例 #6
0
def test_timestamp_rmw_nanos():
    class Foo(proto.Message):
        event_time = proto.Field(
            proto.MESSAGE, number=1, message=timestamp_pb2.Timestamp,
        )

    foo = Foo()
    foo.event_time = DatetimeWithNanoseconds(
        2012, 4, 21, 15, 0, 0, 1, tzinfo=timezone.utc
    )
    assert foo.event_time.microsecond == 1
    assert Foo.pb(foo).event_time.nanos == 1000
    foo.event_time = foo.event_time.replace(microsecond=2)
    assert foo.event_time.microsecond == 2
    assert Foo.pb(foo).event_time.nanos == 2000
コード例 #7
0
def test_documentsnapshot___hash__():
    import datetime
    from proto.datetime_helpers import DatetimeWithNanoseconds

    client = mock.MagicMock()
    client.__hash__.return_value = 234566789
    reference = _make_base_document_reference("hi", "bye", client=client)
    data = {"zoop": 83}
    update_time = DatetimeWithNanoseconds(2021,
                                          10,
                                          4,
                                          17,
                                          43,
                                          27,
                                          nanosecond=123456789,
                                          tzinfo=datetime.timezone.utc)
    snapshot = _make_document_snapshot(reference, data, True, None,
                                       mock.sentinel.create_time, update_time)
    assert hash(snapshot) == hash(reference) + hash(update_time)
コード例 #8
0
def test_watch_on_snapshot_target_no_change_no_target_ids_current():
    import datetime
    from proto.datetime_helpers import DatetimeWithNanoseconds

    inst = _make_watch()
    proto = _make_listen_response()
    read_time = DatetimeWithNanoseconds(1970,
                                        1,
                                        1,
                                        0,
                                        0,
                                        0,
                                        tzinfo=datetime.timezone.utc)
    proto.target_change.read_time = read_time
    inst.current = True

    def push(read_time, next_resume_token):
        inst._read_time = read_time
        inst._next_resume_token = next_resume_token

    inst.push = push
    inst.on_snapshot(proto)
    assert inst._read_time == read_time
    assert inst._next_resume_token == b""
コード例 #9
0
 def get_seconds_since_modification(self, resource: Any) -> bool:
     update_time = resource.update_time
     current_time = DatetimeWithNanoseconds.now(tz=update_time.tzinfo)
     return (current_time - update_time).total_seconds()
コード例 #10
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