|
| 1 | +import datetime as dt |
1 | 2 | from typing import Any, Collection, Union |
2 | 3 |
|
3 | 4 | import numpy as np |
4 | 5 | import pytest |
5 | | -from google.protobuf import any_pb2, wrappers_pb2 |
| 6 | +from google.protobuf import any_pb2, timestamp_pb2, wrappers_pb2 |
6 | 7 | from google.protobuf.message import Message |
7 | 8 | from ni.panels.v1 import panel_types_pb2 |
8 | 9 | from ni.protobuf.types.scalar_pb2 import ScalarData |
|
53 | 54 | (tests.types.MixinIntEnum.VALUE11, "int"), |
54 | 55 | (tests.types.MyStrEnum.VALUE1, "str"), |
55 | 56 | (tests.types.MixinStrEnum.VALUE11, "str"), |
| 57 | + (dt.datetime.now(), "datetime"), |
56 | 58 | ([False, False], "Collection.bool"), |
57 | 59 | ([b"mystr", b"mystr"], "Collection.bytes"), |
58 | 60 | ([456.2, 1.0], "Collection.float"), |
@@ -123,6 +125,16 @@ def test___python_builtin_scalar___to_any___valid_wrapperpb2_value( |
123 | 125 | assert unpack_dest.value == expected_value |
124 | 126 |
|
125 | 127 |
|
| 128 | +def test___python_datetime_datetime___to_any___valid_timestamppb2_value() -> None: |
| 129 | + expected_value = dt.datetime.now() |
| 130 | + result = nipanel._convert.to_any(expected_value) |
| 131 | + unpack_dest = timestamp_pb2.Timestamp() |
| 132 | + _assert_any_and_unpack(result, unpack_dest) |
| 133 | + |
| 134 | + assert isinstance(unpack_dest, timestamp_pb2.Timestamp) |
| 135 | + assert unpack_dest.ToDatetime() == expected_value |
| 136 | + |
| 137 | + |
126 | 138 | @pytest.mark.parametrize( |
127 | 139 | "proto_type, default_value, expected_value", |
128 | 140 | [ |
@@ -175,6 +187,18 @@ def test___wrapperpb2_value___from_any___valid_python_value( |
175 | 187 | assert result == expected_value |
176 | 188 |
|
177 | 189 |
|
| 190 | +def test___timestamppb2_timestamp___from_any___valid_python_value() -> None: |
| 191 | + expected_value = dt.datetime.now() |
| 192 | + pb_value = timestamp_pb2.Timestamp() |
| 193 | + pb_value.FromDatetime(expected_value) |
| 194 | + packed_any = _pack_into_any(pb_value) |
| 195 | + |
| 196 | + result = nipanel._convert.from_any(packed_any) |
| 197 | + |
| 198 | + assert isinstance(result, dt.datetime) |
| 199 | + assert result == expected_value |
| 200 | + |
| 201 | + |
178 | 202 | @pytest.mark.parametrize( |
179 | 203 | "proto_type, expected_value", |
180 | 204 | [ |
|
0 commit comments