Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test ros2 type info #418

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
- name: "Build (Without Python node as it is build with maturin)"
run: cargo build --all --exclude dora-node-api-python
- name: "Test"
run: cargo test --all
run: cargo test --all --exclude dora-ros2-bridge-python

# Run examples as separate job because otherwise we will exhaust the disk
# space of the GitHub action runners.
Expand Down Expand Up @@ -164,6 +164,10 @@ jobs:
with:
required-ros-distributions: humble
- run: 'source /opt/ros/humble/setup.bash && echo AMENT_PREFIX_PATH=${AMENT_PREFIX_PATH} >> "$GITHUB_ENV"'
- name: "Install pyarrow for testing"
run: pip install numpy pyarrow
- name: "Test"
run: cargo test -p dora-ros2-bridge-python
- name: "Rust ROS2 Bridge example"
timeout-minutes: 30
env:
Expand Down
20 changes: 15 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions libraries/extensions/ros2-bridge/python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ eyre = "0.6"
serde = "1.0.166"
arrow = { workspace = true, features = ["pyarrow"] }
futures = "0.3.28"

[dev-dependencies]
serde_assert = "0.7.1"
96 changes: 96 additions & 0 deletions libraries/extensions/ros2-bridge/python/src/typed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,99 @@ pub struct TypeInfo<'a> {
/// the CDR format of ROS2 does not encode struct or field
/// names.
const DUMMY_STRUCT_NAME: &str = "struct";

#[cfg(test)]
mod tests {
use std::path::PathBuf;

use crate::typed::deserialize::StructDeserializer;
use crate::typed::serialize;
use crate::typed::TypeInfo;
use crate::Ros2Context;

use arrow::array::make_array;
use arrow::pyarrow::FromPyArrow;
use arrow::pyarrow::ToPyArrow;

use pyo3::types::IntoPyDict;
use pyo3::types::PyDict;
use pyo3::types::PyList;
use pyo3::types::PyModule;
use pyo3::types::PyTuple;
use pyo3::Python;
use serde::de::DeserializeSeed;
use serde::Serialize;

use serde_assert::Serializer;
use serialize::TypedValue;

use eyre::{Context, Result};
use serde_assert::Deserializer;
#[test]
fn test_python_array_code() -> Result<()> {
pyo3::prepare_freethreaded_python();
let context = Ros2Context::new(None).context("Could not create a context")?;
let messages = context.messages.clone();
let serializer = Serializer::builder().build();

Python::with_gil(|py| -> Result<()> {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); //.join("test_utils.py"); // Adjust this path as needed

// Add the Python module's directory to sys.path
py.run(
"import sys; sys.path.append(str(path))",
Some([("path", path)].into_py_dict(py)),
None,
)?;

let my_module = PyModule::import(py, "test_utils")?;

let arrays: &PyList = my_module.getattr("TEST_ARRAYS")?.extract()?;
for array_wrapper in arrays.iter() {
let arrays: &PyTuple = array_wrapper.extract()?;
let package_name: String = arrays.get_item(0)?.extract()?;
let message_name: String = arrays.get_item(1)?.extract()?;
println!("Checking {}::{}", package_name, message_name);
let in_pyarrow = arrays.get_item(2)?;

let array = arrow::array::ArrayData::from_pyarrow(in_pyarrow)?;
let type_info = TypeInfo {
package_name: package_name.into(),
message_name: message_name.clone().into(),
messages: messages.clone(),
};
let typed_value = TypedValue {
value: &make_array(array.clone()),
type_info: &type_info.clone(),
};

let typed_deserializer =
StructDeserializer::new(std::borrow::Cow::Owned(type_info));
let tokens = typed_value.serialize(&serializer)?;
let mut deserializer = Deserializer::builder(tokens).build();

let out_value = typed_deserializer
.deserialize(&mut deserializer)
.context("could not deserialize array")?;

let out_pyarrow = out_value.to_pyarrow(py)?;

let test_utils = PyModule::import(py, "test_utils")?;
let context = PyDict::new(py);

context.set_item("test_utils", test_utils)?;
context.set_item("in_pyarrow", in_pyarrow)?;
context.set_item("out_pyarrow", out_pyarrow)?;

let _ = py
.eval(
"test_utils.is_subset(in_pyarrow, out_pyarrow)",
Some(context),
None,
)
.context("could not check if it is a subset")?;
}
Ok(())
})
}
}
Loading
Loading