From 4c08769880fe94d0b115aa0eac28549a558f9d88 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Thu, 22 Feb 2024 16:41:52 +0100 Subject: [PATCH 1/4] added python bindings test for mutable object conversion --- tests/unittests/CMakeLists.txt | 4 ++++ tests/unittests/unittests.py | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/unittests/unittests.py diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index 5aa2db37e..d95441852 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -94,3 +94,7 @@ else() PODIO_SIOBLOCK_PATH=${CMAKE_CURRENT_BINARY_DIR} ) endif() + +add_test(NAME unittest.py COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/unittests.py) +PODIO_SET_TEST_ENV(unittest.py) +set_tests_properties(unittest.py PROPERTIES WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/tests) diff --git a/tests/unittests/unittests.py b/tests/unittests/unittests.py new file mode 100644 index 000000000..18416c475 --- /dev/null +++ b/tests/unittests/unittests.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +"""cppyy python binding tests""" + +import unittest +import ROOT + +res = ROOT.gSystem.Load("libTestDataModel.so") +if res < 0: + raise RuntimeError("Failed to load libTestDataModel.so") + +from ROOT import MutableExampleMC # pylint: disable=wrong-import-position # noqa: E402 +from ROOT import ExampleMCCollection # pylint: disable=wrong-import-position # noqa: E402 + + +class ObjectConversionsTest(unittest.TestCase): + """Object conversion binding tests""" + + def test_conversion_mutable_to_immutable(self): + ROOT.gInterpreter.Declare( + """ + void test_accepts_immutable(ExampleMC) {} + """ + ) + accepts_immutable = ROOT.test_accepts_immutable + mutable_obj = MutableExampleMC() + accepts_immutable(mutable_obj) + + +class OneToManyRelationsTest(unittest.TestCase): + """OneToManyRelations binding tests""" + + def test_add(self): + particles = ExampleMCCollection() + parent_particle = particles.create() + daughter_particle = particles.create() + + self.assertEqual(len(daughter_particle.parents()), 0) + daughter_particle.addparents(parent_particle) + self.assertEqual(len(daughter_particle.parents()), 1) + + +if __name__ == "__main__": + unittest.main() From 1c97d6bdd19e762001d05398cfa1f740b4e1d8b4 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Thu, 22 Feb 2024 16:43:40 +0100 Subject: [PATCH 2/4] replaced object conversion function with converting constructor --- python/templates/MutableObject.cc.jinja2 | 1 - python/templates/MutableObject.h.jinja2 | 3 --- python/templates/Object.cc.jinja2 | 2 ++ python/templates/Object.h.jinja2 | 2 ++ 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/templates/MutableObject.cc.jinja2 b/python/templates/MutableObject.cc.jinja2 index bb851a48c..eaec2f716 100644 --- a/python/templates/MutableObject.cc.jinja2 +++ b/python/templates/MutableObject.cc.jinja2 @@ -17,7 +17,6 @@ {{ utils.namespace_open(class.namespace) }} {{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }} -Mutable{{ class.bare_type }}::operator {{ class.bare_type }}() const { return {{ class.bare_type }}(m_obj); } {{ macros.member_getters(class, Members, use_get_syntax, prefix='Mutable') }} {{ macros.single_relation_getters(class, OneToOneRelations, use_get_syntax, prefix='Mutable') }} diff --git a/python/templates/MutableObject.h.jinja2 b/python/templates/MutableObject.h.jinja2 index 086f99b8b..19af5e3a3 100644 --- a/python/templates/MutableObject.h.jinja2 +++ b/python/templates/MutableObject.h.jinja2 @@ -39,9 +39,6 @@ public: {{ macros.constructors_destructors(class.bare_type, Members, prefix='Mutable') }} - /// conversion to const object - operator {{ class.bare_type }}() const; - public: {{ macros.member_getters(Members, use_get_syntax) }} diff --git a/python/templates/Object.cc.jinja2 b/python/templates/Object.cc.jinja2 index a526162ac..e95548fcc 100644 --- a/python/templates/Object.cc.jinja2 +++ b/python/templates/Object.cc.jinja2 @@ -18,6 +18,8 @@ {{ macros.constructors_destructors(class.bare_type, Members) }} +{{ class.bare_type }}::{{ class.bare_type }}(const Mutable{{ class.bare_type }}& other): {{ class.bare_type }}(other.m_obj) {} + {{ class.bare_type }}::{{ class.bare_type }}({{ class.bare_type }}Obj* obj) : m_obj(podio::utils::MaybeSharedPtr<{{ class.bare_type }}Obj>(obj)) {} {{ class.bare_type }} {{ class.bare_type }}::makeEmpty() { diff --git a/python/templates/Object.h.jinja2 b/python/templates/Object.h.jinja2 index 17afef9cd..acee909be 100644 --- a/python/templates/Object.h.jinja2 +++ b/python/templates/Object.h.jinja2 @@ -43,6 +43,8 @@ public: using collection_type = {{ class.bare_type }}Collection; {{ macros.constructors_destructors(class.bare_type, Members) }} + /// converting constructor from mutable object + {{ class.bare_type }}(const Mutable{{ class.bare_type }}& other); static {{ class.bare_type }} makeEmpty(); From 7afd7095e25522f8f975069f1fa770136b3efce7 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Thu, 22 Feb 2024 20:52:22 +0100 Subject: [PATCH 3/4] moved python bindings tests cleaned unnecessary import magic --- .../unittests.py => python/podio/test_CodeGen.py | 12 +----------- tests/unittests/CMakeLists.txt | 4 ---- 2 files changed, 1 insertion(+), 15 deletions(-) rename tests/unittests/unittests.py => python/podio/test_CodeGen.py (72%) diff --git a/tests/unittests/unittests.py b/python/podio/test_CodeGen.py similarity index 72% rename from tests/unittests/unittests.py rename to python/podio/test_CodeGen.py index 18416c475..3f1328c22 100644 --- a/tests/unittests/unittests.py +++ b/python/podio/test_CodeGen.py @@ -3,13 +3,7 @@ import unittest import ROOT - -res = ROOT.gSystem.Load("libTestDataModel.so") -if res < 0: - raise RuntimeError("Failed to load libTestDataModel.so") - -from ROOT import MutableExampleMC # pylint: disable=wrong-import-position # noqa: E402 -from ROOT import ExampleMCCollection # pylint: disable=wrong-import-position # noqa: E402 +from ROOT import ExampleMCCollection, MutableExampleMC class ObjectConversionsTest(unittest.TestCase): @@ -37,7 +31,3 @@ def test_add(self): self.assertEqual(len(daughter_particle.parents()), 0) daughter_particle.addparents(parent_particle) self.assertEqual(len(daughter_particle.parents()), 1) - - -if __name__ == "__main__": - unittest.main() diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index d95441852..5aa2db37e 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -94,7 +94,3 @@ else() PODIO_SIOBLOCK_PATH=${CMAKE_CURRENT_BINARY_DIR} ) endif() - -add_test(NAME unittest.py COMMAND python3 ${CMAKE_CURRENT_SOURCE_DIR}/unittests.py) -PODIO_SET_TEST_ENV(unittest.py) -set_tests_properties(unittest.py PROPERTIES WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/tests) From 8535dfdfe7f8961163c9a606b48cfa756fbfc6c2 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 23 Feb 2024 10:12:14 +0100 Subject: [PATCH 4/4] ignore emplace_back clang-tidy suggestion in test --- tests/unittests/unittest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittests/unittest.cpp b/tests/unittests/unittest.cpp index d26523b5d..74cbded14 100644 --- a/tests/unittests/unittest.cpp +++ b/tests/unittests/unittest.cpp @@ -156,7 +156,7 @@ TEST_CASE("Container lifetime", "[basics][memory-management]") { { MutableExampleHit hit; hit.energy(3.14f); - hits.push_back(hit); + hits.push_back(hit); // NOLINT(modernize-use-emplace) } auto hit = hits[0]; REQUIRE(hit.energy() == 3.14f);