|
1 | 1 | import datetime as dt |
2 | 2 | from typing import Any, Collection, Union |
3 | 3 |
|
| 4 | +import hightime as ht |
| 5 | +import nitypes.bintime as bt |
4 | 6 | import numpy as np |
5 | 7 | import pytest |
6 | 8 | from google.protobuf import any_pb2, duration_pb2, timestamp_pb2, wrappers_pb2 |
7 | 9 | from google.protobuf.message import Message |
8 | | -from ni.protobuf.types import array_pb2, attribute_value_pb2, scalar_pb2, vector_pb2, waveform_pb2 |
| 10 | +from ni.protobuf.types import ( |
| 11 | + array_pb2, |
| 12 | + attribute_value_pb2, |
| 13 | + precision_timestamp_pb2, |
| 14 | + scalar_pb2, |
| 15 | + vector_pb2, |
| 16 | + waveform_pb2, |
| 17 | +) |
9 | 18 | from nitypes.complex import ComplexInt32DType |
10 | 19 | from nitypes.scalar import Scalar |
| 20 | +from nitypes.time import convert_datetime |
11 | 21 | from nitypes.vector import Vector |
12 | 22 | from nitypes.waveform import AnalogWaveform, ComplexWaveform, DigitalWaveform, Spectrum |
13 | 23 | from typing_extensions import TypeAlias |
|
50 | 60 | (tests.types.MixinStrEnum.VALUE11, "builtins.str"), |
51 | 61 | (dt.datetime.now(), "datetime.datetime"), |
52 | 62 | (dt.timedelta(days=1), "datetime.timedelta"), |
| 63 | + (bt.DateTime.now(tz=dt.timezone.utc), "nitypes.bintime.DateTime"), |
| 64 | + (ht.datetime.now(), "hightime.datetime"), |
53 | 65 | ([False, False], "collections.abc.Collection[builtins.bool]"), |
54 | 66 | ([b"mystr", b"mystr"], "collections.abc.Collection[builtins.bytes]"), |
55 | 67 | ([456.2, 1.0], "collections.abc.Collection[builtins.float]"), |
@@ -369,6 +381,31 @@ def test___python_float64_spectrum___to_any___valid_double_spectrum_proto() -> N |
369 | 381 | assert unpack_dest.frequency_increment == 10.0 |
370 | 382 |
|
371 | 383 |
|
| 384 | +def test___python_bintime_datetime__to_any___valid_precision_timestamp_proto() -> None: |
| 385 | + python_value = bt.DateTime(year=2020, month=1, day=10, second=45, tzinfo=dt.timezone.utc) |
| 386 | + |
| 387 | + result = nipanel._convert.to_any(python_value) |
| 388 | + unpack_dest = precision_timestamp_pb2.PrecisionTimestamp() |
| 389 | + _assert_any_and_unpack(result, unpack_dest) |
| 390 | + |
| 391 | + expected_tuple = python_value.to_tuple() |
| 392 | + assert unpack_dest.seconds == expected_tuple.whole_seconds |
| 393 | + assert unpack_dest.fractional_seconds == expected_tuple.fractional_seconds |
| 394 | + |
| 395 | + |
| 396 | +def test___python_hightime_datetime__to_any___valid_precision_timestamp_proto() -> None: |
| 397 | + python_value = ht.datetime(year=2020, month=1, day=10, second=45, tzinfo=dt.timezone.utc) |
| 398 | + |
| 399 | + result = nipanel._convert.to_any(python_value) |
| 400 | + unpack_dest = precision_timestamp_pb2.PrecisionTimestamp() |
| 401 | + _assert_any_and_unpack(result, unpack_dest) |
| 402 | + |
| 403 | + expected_bt_datetime = convert_datetime(bt.DateTime, python_value) |
| 404 | + expected_tuple = expected_bt_datetime.to_tuple() |
| 405 | + assert unpack_dest.seconds == expected_tuple.whole_seconds |
| 406 | + assert unpack_dest.fractional_seconds == expected_tuple.fractional_seconds |
| 407 | + |
| 408 | + |
372 | 409 | @pytest.mark.parametrize( |
373 | 410 | "python_value", |
374 | 411 | [ |
@@ -564,6 +601,21 @@ def test___double_spectrum_proto___from_any___valid_python_spectrum() -> None: |
564 | 601 | assert result.frequency_increment == 10.0 |
565 | 602 |
|
566 | 603 |
|
| 604 | +def test___precision_timestamp_proto__from_any___valid_bintime_datetime() -> None: |
| 605 | + expected_bt_dt = bt.DateTime(year=2020, month=1, day=10, second=45, tzinfo=dt.timezone.utc) |
| 606 | + expected_tuple = expected_bt_dt.to_tuple() |
| 607 | + pb_value = precision_timestamp_pb2.PrecisionTimestamp( |
| 608 | + seconds=expected_tuple.whole_seconds, |
| 609 | + fractional_seconds=expected_tuple.fractional_seconds, |
| 610 | + ) |
| 611 | + packed_any = _pack_into_any(pb_value) |
| 612 | + |
| 613 | + result = nipanel._convert.from_any(packed_any) |
| 614 | + |
| 615 | + assert isinstance(result, bt.DateTime) |
| 616 | + assert result == expected_bt_dt |
| 617 | + |
| 618 | + |
567 | 619 | def test___double2darray___from_any___valid_python_2dcollection() -> None: |
568 | 620 | pb_value = array_pb2.Double2DArray(data=[1.0, 2.0, 3.0, 4.0], rows=2, columns=2) |
569 | 621 | packed_any = _pack_into_any(pb_value) |
|
0 commit comments