Skip to content

Commit

Permalink
Add I/O tests for a type using interface relations
Browse files Browse the repository at this point in the history
  • Loading branch information
tmadlener committed Nov 22, 2023
1 parent 0abd056 commit 1b846b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tests/read_frame.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#ifndef PODIO_TESTS_READ_FRAME_H // NOLINT(llvm-header-guard): folder structure not suitable
#define PODIO_TESTS_READ_FRAME_H // NOLINT(llvm-header-guard): folder structure not suitable

#include "datamodel/ExampleWithVectorMemberCollection.h"
#include "read_test.h"

#include "datamodel/ExampleWithInterfaceRelationCollection.h"
#include "datamodel/ExampleWithVectorMemberCollection.h"

#include "extension_model/ContainedTypeCollection.h"
#include "extension_model/ExternalComponentTypeCollection.h"
#include "extension_model/ExternalRelationTypeCollection.h"
Expand Down Expand Up @@ -69,6 +71,26 @@ void checkVecMemSubsetColl(const podio::Frame& event) {
ASSERT(subsetColl[0] == origColl[0], "subset coll does not have the right contents");
}

void checkExampleWithInterfaceRelations(const podio::Frame& event) {
const auto& interfaceColl = event.get<ExampleWithInterfaceRelationCollection>("interfaceTypes");
const auto& hits = event.get<ExampleHitCollection>("hits");
const auto& clusters = event.get<ExampleClusterCollection>("clusters");
const auto& mcps = event.get<ExampleMCCollection>("mcparticles");

ASSERT(interfaceColl.isValid(), "interfaceTypes collection should be present");
ASSERT(interfaceColl.size() == 2, "interfaceTypes collection should have 2 elements");

auto element = interfaceColl[0];
ASSERT(element.aSingleEnergyType() == clusters[0], "OneToOneRelation with interface type not persisted correctly");

element = interfaceColl[1];
auto manyEnergies = element.manyEnergies();
ASSERT(manyEnergies.size() == 3, "OneToManyRelation with interface type does not have the expected size");
ASSERT(manyEnergies[0] == clusters[1], "OneToManyRelation 0 with interface type not persisted correctly");
ASSERT(manyEnergies[1] == mcps[0], "OneToManyRelation 1 with interface type not persisted correctly");
ASSERT(manyEnergies[2] == hits[1], "OneToManyRelation 2 with interface type not persisted correctly");
}

template <typename ReaderT>
int read_frames(const std::string& filename, bool assertBuildVersion = true) {
auto reader = ReaderT();
Expand Down Expand Up @@ -125,6 +147,9 @@ int read_frames(const std::string& filename, bool assertBuildVersion = true) {
if (reader.currentFileVersion() >= podio::version::Version{0, 16, 99}) {
checkVecMemSubsetColl(otherFrame);
}
if (reader.currentFileVersion() >= podio::version::Version{0, 17, 99}) {
checkExampleWithInterfaceRelations(otherFrame);
}
}

if (reader.readNextEntry(podio::Category::Event)) {
Expand Down
18 changes: 18 additions & 0 deletions tests/write_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "datamodel/ExampleWithARelationCollection.h"
#include "datamodel/ExampleWithArrayCollection.h"
#include "datamodel/ExampleWithFixedWidthIntegersCollection.h"
#include "datamodel/ExampleWithInterfaceRelationCollection.h"
#include "datamodel/ExampleWithNamespaceCollection.h"
#include "datamodel/ExampleWithOneRelationCollection.h"
#include "datamodel/ExampleWithVectorMemberCollection.h"
Expand Down Expand Up @@ -361,6 +362,21 @@ auto createExtensionExternalRelationCollection(int i, const ExampleHitCollection
return coll;
}

auto createExampleWithInterfaceRelationCollection(const ExampleHitCollection& hits, const ExampleMCCollection& mcps,
const ExampleClusterCollection& clusters) {
auto coll = ExampleWithInterfaceRelationCollection{};
// Create one element to test all of the four interface relations
auto element = coll.create();
element.aSingleEnergyType(clusters[0]);

element = coll.create();
element.addmanyEnergies(clusters[1]);
element.addmanyEnergies(mcps[0]);
element.addmanyEnergies(hits[1]);

return coll;
}

podio::Frame makeFrame(int iFrame) {
podio::Frame frame{};

Expand Down Expand Up @@ -420,6 +436,8 @@ podio::Frame makeFrame(int iFrame) {
frame.put(createExtensionExternalComponentCollection(iFrame), "extension_ExternalComponent");
frame.put(createExtensionExternalRelationCollection(iFrame, hits, clusters), "extension_ExternalRelation");

frame.put(createExampleWithInterfaceRelationCollection(hits, mcps, clusters), "interfaceTypes");

return frame;
}

Expand Down

0 comments on commit 1b846b0

Please sign in to comment.