2023-01-11 15:21:14 +01:00
|
|
|
# Copyright 2020-2023 Ternaris.
|
2021-05-02 14:49:33 +02:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
"""Test full data roundtrip."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from rosbags.rosbag2 import Reader, Writer
|
|
|
|
|
from rosbags.serde import deserialize_cdr, serialize_cdr
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('mode', [*Writer.CompressionMode])
|
2021-11-25 14:26:17 +01:00
|
|
|
def test_roundtrip(mode: Writer.CompressionMode, tmp_path: Path) -> None:
|
2021-05-02 14:49:33 +02:00
|
|
|
"""Test full data roundtrip."""
|
|
|
|
|
|
|
|
|
|
class Foo: # pylint: disable=too-few-public-methods
|
|
|
|
|
"""Dummy class."""
|
|
|
|
|
|
|
|
|
|
data = 1.25
|
|
|
|
|
|
|
|
|
|
path = tmp_path / 'rosbag2'
|
|
|
|
|
wbag = Writer(path)
|
|
|
|
|
wbag.set_compression(mode, wbag.CompressionFormat.ZSTD)
|
|
|
|
|
with wbag:
|
|
|
|
|
msgtype = 'std_msgs/msg/Float64'
|
2021-08-01 18:22:36 +02:00
|
|
|
wconnection = wbag.add_connection('/test', msgtype)
|
|
|
|
|
wbag.write(wconnection, 42, serialize_cdr(Foo, msgtype))
|
2021-05-02 14:49:33 +02:00
|
|
|
|
|
|
|
|
rbag = Reader(path)
|
|
|
|
|
with rbag:
|
|
|
|
|
gen = rbag.messages()
|
2021-08-01 18:22:36 +02:00
|
|
|
rconnection, _, raw = next(gen)
|
2022-04-13 09:40:22 +02:00
|
|
|
assert rconnection.topic == wconnection.topic
|
|
|
|
|
assert rconnection.msgtype == wconnection.msgtype
|
|
|
|
|
assert rconnection.ext == wconnection.ext
|
2021-08-01 18:22:36 +02:00
|
|
|
msg = deserialize_cdr(raw, rconnection.msgtype)
|
2022-04-11 00:07:53 +02:00
|
|
|
assert getattr(msg, 'data', None) == Foo.data
|
2021-05-02 14:49:33 +02:00
|
|
|
with pytest.raises(StopIteration):
|
|
|
|
|
next(gen)
|