Skip to content

Commit

Permalink
test: Add capella and messages tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Feb 21, 2024
1 parent 3617af1 commit 298bd38
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 10 deletions.
4 changes: 2 additions & 2 deletions capella_ros_tools/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __str__(self) -> str:
"""Return string representation of the field."""
out = f"{self.type} {self.name}"
if self.description:
out += f" # {_cleanhtml(self.description)}"
out += f" # {_cleanhtml(self.description)}"
return out


Expand All @@ -130,7 +130,7 @@ def __str__(self) -> str:
"""Return string representation of the constant."""
out = f"{self.type} {self.name} = {self.value}"
if self.description:
out += f" # {_cleanhtml(self.description)}"
out += f" # {_cleanhtml(self.description)}"
return out


Expand Down
204 changes: 204 additions & 0 deletions tests/test_capella.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

import pathlib

import pytest

from capella_ros_tools import capella, messages

EXAMPLE_CAPELLA_PATH = pathlib.Path(__file__).parent.joinpath(
"data/empty_model"
)


@pytest.fixture
def sample_capella_data_package():
capella_path = EXAMPLE_CAPELLA_PATH.as_posix()
return capella.CapellaDataPackage(capella_path, "la")


sample_message_person = messages.MessageDef(
"Person",
[
messages.FieldDef(
messages.TypeDef(
"string",
messages.Range("1", "1"),
),
"name",
"",
),
messages.FieldDef(
messages.TypeDef(
"uint8",
messages.Range("1", "1"),
messages.Range("0", "150"),
None,
),
"age",
"",
),
messages.FieldDef(
messages.TypeDef(
"Pet",
messages.Range("0", "*"),
),
"pet",
"",
),
],
[],
"",
)


sample_message_pet = messages.MessageDef(
"Pet",
[
messages.FieldDef(
messages.TypeDef(
"string",
messages.Range("1", "1"),
),
"name",
"",
),
messages.FieldDef(
messages.TypeDef(
"PetType",
messages.Range("1", "1"),
),
"type",
"",
),
],
[
messages.EnumDef(
"PetType",
[
messages.ConstantDef(
messages.TypeDef(
"uint8",
messages.Range("1", "1"),
),
"CAT",
"0",
"",
),
messages.ConstantDef(
messages.TypeDef("uint8", messages.Range("1", "1")),
"DOG",
"1",
"",
),
],
"",
),
],
"",
)


@pytest.mark.parametrize(
"sample_pkg_name", ["test_pkg_name", "test_pkg_name_2"]
)
def test_package(sample_capella_data_package, sample_pkg_name):
pkg_def = messages.MessagePkgDef(sample_pkg_name, [], [])
sample_capella_data_package.create_package(
pkg_def, sample_capella_data_package.data_package
)

pkg_obj = sample_capella_data_package.create_package(
pkg_def, sample_capella_data_package.data_package
)

assert pkg_obj in sample_capella_data_package.data_package.packages
assert pkg_obj.name == sample_pkg_name

sample_capella_data_package.remove_package(
pkg_obj, sample_capella_data_package.data_package
)

assert pkg_obj not in sample_capella_data_package.data_package.packages


def _create_and_return_class(sample_capella_data_package, sample_message_def):
sample_capella_data_package.create_class(
sample_message_def, sample_capella_data_package.data_package
)
cls_obj = sample_capella_data_package.create_class(
sample_message_def, sample_capella_data_package.data_package
)
return cls_obj


@pytest.mark.parametrize(
"sample_message_def", [sample_message_person, sample_message_pet]
)
def test_class(sample_capella_data_package, sample_message_def):
cls_obj = _create_and_return_class(
sample_capella_data_package, sample_message_def
)

assert cls_obj in sample_capella_data_package.data_package.classes
assert cls_obj.name == sample_message_def.name

sample_capella_data_package.remove_class(
cls_obj, sample_capella_data_package.data_package
)

assert cls_obj not in sample_capella_data_package.data_package.classes


def _create_and_return_enum(sample_capella_data_package, sample_enum_def):
sample_capella_data_package.create_enum(
sample_enum_def, sample_capella_data_package.data_package
)
enum_obj = sample_capella_data_package.create_enum(
sample_enum_def, sample_capella_data_package.data_package
)
return enum_obj


@pytest.mark.parametrize("sample_enum_def", [sample_message_pet.enums[0]])
def test_enum(sample_capella_data_package, sample_enum_def):
enum_obj = _create_and_return_enum(
sample_capella_data_package, sample_enum_def
)

assert enum_obj in sample_capella_data_package.data_package.datatypes
assert enum_obj.name == sample_enum_def.name
assert len(enum_obj.literals) == len(sample_enum_def.literals)
assert enum_obj.literals[0].name == sample_enum_def.literals[0].name

sample_capella_data_package.remove_enum(
enum_obj, sample_capella_data_package.data_package
)

assert enum_obj not in sample_capella_data_package.data_package.datatypes


@pytest.mark.parametrize(
"sample_message_def",
[sample_message_person, sample_message_pet],
)
def test_create_properties(sample_capella_data_package, sample_message_def):
cls_obj = _create_and_return_class(
sample_capella_data_package, sample_message_def
)
sample_capella_data_package.create_properties(
sample_message_def, sample_capella_data_package.data_package
)

assert len(cls_obj.owned_properties) == len(sample_message_def.fields)
for prop, field in zip(
cls_obj.owned_properties, sample_message_def.fields
):
assert prop.name == field.name

sample_capella_data_package.remove_class(
cls_obj, sample_capella_data_package.data_package
)

assert not cls_obj in sample_capella_data_package.data_package.classes
8 changes: 0 additions & 8 deletions tests/test_capella_ros_tools.py

This file was deleted.

135 changes: 135 additions & 0 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright DB InfraGO AG and contributors
# SPDX-License-Identifier: Apache-2.0

from pathlib import Path

import pytest

from capella_ros_tools.messages import (
CONSTANT_SEPARATOR,
UPPER_BOUND_TOKEN,
ConstantDef,
FieldDef,
MessageDef,
MessagePkgDef,
Range,
TypeDef,
)

PATH = Path(__file__).parent


@pytest.mark.parametrize(
"params, expected_output",
[
(
("test_name", Range("1", "1"), None, "test_package"),
"test_package/test_name",
),
(
("test_name", Range("0", "10"), None, "test_package"),
"test_package/test_name[10]",
),
(
("test_name", Range("1", "1"), Range("0", "10"), "test_package"),
f"test_package/test_name[{UPPER_BOUND_TOKEN}10]",
),
],
)
def test_TypeDef_str(params, expected_output):
name, card, range, package = params

type_def = TypeDef(name, card, range, package)

assert str(type_def) == expected_output


@pytest.mark.parametrize(
"type_str, expected_output",
[
(
"test_package/test_name",
("test_name", Range("1", "1"), None, "test_package"),
),
(
"test_package/test_name[10]",
("test_name", Range("0", "10"), None, "test_package"),
),
(
f"test_package/test_name[{UPPER_BOUND_TOKEN}10]",
("test_name", Range("1", "1"), Range("0", "10"), "test_package"),
),
],
)
def test_TypeDef_from_string(type_str, expected_output):
name, card, range, package = expected_output
type_def = TypeDef.from_string(type_str)

assert type_def.name == name
assert type_def.card == card
assert type_def.range == range
assert type_def.package == package


@pytest.mark.parametrize(
"params, expected_output",
[
(
("test_type", "test_name", "test_description"),
"test_type test_name # test_description",
),
],
)
def test_FieldDef_str(params, expected_output):
type_str, name, description = params
type_def = TypeDef.from_string(type_str)

field_def = FieldDef(type_def, name, description)

assert str(field_def) == expected_output


@pytest.mark.parametrize(
"params, expected_output",
[
(
("test_type", "test_name", 1, "test_description"),
f"test_type test_name {CONSTANT_SEPARATOR} 1 # test_description",
),
],
)
def test_ConstantDef_str(params, expected_output):
type_str, name, value, description = params
type_def = TypeDef.from_string(type_str)

constant_def = ConstantDef(type_def, name, value, description)

assert str(constant_def) == expected_output


@pytest.mark.parametrize(
"msg_file_path",
[
PATH.joinpath("data/example_msgs/msg/CameraInfo.msg"),
PATH.joinpath("data/example_msgs/msg/DiagnosticStatus.msg"),
PATH.joinpath("data/example_msgs/msg/PointCloud2.msg"),
],
)
def test_MessageDef(msg_file_path):
message_def = MessageDef.from_file(msg_file_path)

assert message_def.name == msg_file_path.stem
assert str(message_def) == msg_file_path.read_text()


@pytest.mark.parametrize(
"pkg_name, msg_path",
[
("example_msgs", PATH.joinpath("data/example_msgs")),
],
)
def test_MessagePkgDef_from_msg_folder(pkg_name, msg_path):
message_pkg_def = MessagePkgDef.from_msg_folder(pkg_name, msg_path)

assert message_pkg_def.name == pkg_name
assert len(message_pkg_def.messages) == len(list(msg_path.rglob("*.msg")))

0 comments on commit 298bd38

Please sign in to comment.