Skip to content

Commit

Permalink
Add first simple tests for "trivial" schema evolution
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Jun 15, 2023
1 parent 9e006fd commit 14c2010
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 1 deletion.
3 changes: 3 additions & 0 deletions tests/CTestCustom.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ if ((NOT "@FORCE_RUN_ALL_TESTS@" STREQUAL "ON") AND (NOT "@USE_SANITIZER@" STREQ
datamodel_def_store_roundtrip_root_extension
datamodel_def_store_roundtrip_sio
datamodel_def_store_roundtrip_sio_extension

write_old_data_root
read_new_data_root
)

foreach(version in @legacy_versions@)
Expand Down
20 changes: 20 additions & 0 deletions tests/schema_evolution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,23 @@ PODIO_ADD_ROOT_IO_DICT(TestDataModel_v1Dict TestDataModel_v1 "${headers}" ${CMAK
)

PODIO_ADD_SIO_IO_BLOCKS(TestDataModel_v1 "${headers}" "${sources}")

#--- Helper function to create a test in an environment that is suitable to
#--- write data in an old schema version
function(PODIO_CREATE_WRITE_OLD_DATA_TEST sourcefile additional_libs)
string( REPLACE ".cpp" "" name ${sourcefile} )
add_executable( ${name} ${sourcefile} )
add_test(NAME ${name} COMMAND ${name})

target_link_libraries(${name} PRIVATE TestDataModel_v1 ${additional_libs})
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/tests/schema_evolution)

set_property(TEST ${name}
PROPERTY ENVIRONMENT
LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/src:${CMAKE_CURRENT_BINARY_DIR}:$<TARGET_FILE_DIR:ROOT::Tree>:$<$<TARGET_EXISTS:SIO::sio>:$<TARGET_FILE_DIR:SIO::sio>>:$ENV{LD_LIBRARY_PATH}
PODIO_SIO_BLOCK=${CMAKE_CURRENT_BINARY_DIR}
ROOT_INCLUDE_PATH=${CMAKE_CURRENT_BINARY_DIR}/datamodel_old:${CMAKE_SOURCE_DIR}/include
)
endfunction()

add_subdirectory(root_io)
18 changes: 18 additions & 0 deletions tests/schema_evolution/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Schema Evolution tests
This folder contains tests for the schema evolution functionality in podio. The
functionality is tested by first writing data with an old schema version and
then reading in with the current schema version.
[`datalayout_old.yaml`](./datalayout_old.yaml) holds the definition of the old
version, using schema version 1, while the
[`datalayout.yaml`](../datalayout.yaml) that is also used for the other I/O
tests is used as the current version (schema version 2).

## Differences between the two schema versions
Since it is not immediately visible from the test code this list contains the
differences between the two schema versions, and also how this evolution is
tested (if it is supported)

| component / datatype | difference from v1 to v2 | purpose of test | tested with |
|--|--|--|--|
| `SimpleStruct` | no `int y` member in v1 | Addition of new members in components | As member of `ExampleWithArrayComponent` |
| `ExampleHit` | no `double energy` member in v1 | Addition of new members in datatypes | Directly via `ExampleHit` |
1 change: 0 additions & 1 deletion tests/schema_evolution/datalayout_old.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ datatypes :
- double x // x-coordinate
- double y // y-coordinate
- double z // z-coordinate
- double energy // measured energy deposit

ExampleMC :
Description : "Example MC-particle"
Expand Down
51 changes: 51 additions & 0 deletions tests/schema_evolution/read_new_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef PODIO_TESTS_SCHEMAEVOLUTION_READNEWDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
#define PODIO_TESTS_SCHEMAEVOLUTION_READNEWDATA_H // NOLINT(llvm-header-guard): folder structure not suitable

#include "datamodel/ExampleHitCollection.h"
#include "datamodel/ExampleWithArrayComponentCollection.h"

#include "podio/Frame.h"

#include <iostream>
#include <string>

int readSimpleStruct(const podio::Frame& event) {
const auto& coll = event.get<ExampleWithArrayComponentCollection>("simpleStructTest");
auto elem = coll[0];
const auto sstruct = elem.s();

if (sstruct.y != 0 || sstruct.x != 42 || sstruct.z != 123) {
return 1;
}
return 0;
}

int readExampleHit(const podio::Frame& event) {
const auto& coll = event.get<ExampleHitCollection>("datatypeMemberAdditionTest");
auto elem = coll[0];

if (elem.energy() != 0) {
return 1;
}
if (elem.x() != 1.23 || elem.y() != 1.23 || elem.z() != 1.23 || elem.cellID() != 0xcaffee) {
return 1;
}

return 0;
}

template <typename ReaderT>
int read_new_data(const std::string& filename) {
ReaderT reader{};
reader.openFile(filename);

const auto event = podio::Frame(reader.readEntry("events", 0));

int result = 0;
result += readSimpleStruct(event);
result += readExampleHit(event);

return result;
}

#endif // PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H
4 changes: 4 additions & 0 deletions tests/schema_evolution/root_io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PODIO_CREATE_WRITE_OLD_DATA_TEST(write_old_data_root.cpp "TestDataModel_v1Dict;podio::podioRootIO")

CREATE_PODIO_TEST(read_new_data_root.cpp "TestDataModelDict;podioRootIO")
set_property(TEST read_new_data_root PROPERTY DEPENDS write_old_data_root)
7 changes: 7 additions & 0 deletions tests/schema_evolution/root_io/read_new_data_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "schema_evolution/read_new_data.h"

#include "podio/ROOTFrameReader.h"

int main() {
return read_new_data<podio::ROOTFrameReader>("example_data_old_schema.root");
}
7 changes: 7 additions & 0 deletions tests/schema_evolution/root_io/write_old_data_root.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "write_old_data.h"

#include "podio/ROOTFrameWriter.h"

int main() {
return writeData<podio::ROOTFrameWriter>("example_data_old_schema.root");
}
55 changes: 55 additions & 0 deletions tests/schema_evolution/write_old_data.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H // NOLINT(llvm-header-guard): folder structure not suitable
#define PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H // NOLINT(llvm-header-guard): folder structure not suitable

#include "datamodel/ExampleHitCollection.h"
#include "datamodel/ExampleWithArrayComponentCollection.h"

#include "podio/Frame.h"

#include <string>

/// This is a datatype that holds a SimpleStruct component
auto writeSimpleStruct() {
ExampleWithArrayComponentCollection coll;
auto elem = coll.create();
auto sstruct = SimpleStruct();
sstruct.x = 42;
sstruct.z = 123;
elem.s(sstruct);

return coll;
}

auto writeExampleHit() {
ExampleHitCollection coll;
auto elem = coll.create();
elem.x(1.23);
elem.y(1.23);
elem.z(1.23);
elem.cellID(0xcaffee);

return coll;
}

podio::Frame createFrame() {
podio::Frame event;

event.put(writeSimpleStruct(), "simpleStructTest");
event.put(writeExampleHit(), "dataTypeMemberAdditionTest");

return event;
}

template <typename WriterT>
int writeData(const std::string& filename) {
WriterT writer(filename);

auto event = createFrame();
writer.writeFrame(event, "events");

writer.finish();

return 0;
}

#endif // PODIO_TESTS_SCHEMAEVOLUTION_WRITEOLDDATA_H

0 comments on commit 14c2010

Please sign in to comment.