Belle II Software development
softwareTriggerDBHandler.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <hlt/softwaretrigger/core/SoftwareTriggerDBHandler.h>
10#include <framework/database/Configuration.h>
11#include <framework/database/Database.h>
12
13#include <framework/utilities/TestHelpers.h>
14#include <gtest/gtest.h>
15
16using namespace std;
17
18namespace Belle2 {
23 namespace SoftwareTrigger {
24
26 class SoftwareTriggerDBHandlerTest : public ::testing::Test {
29
31 void SetUp()
32 {
34
37 evtPtr.registerInDataStore();
39 evtPtr.construct(1, 0, 0);
40
42 conf.setNewPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
43 conf.prependTestingPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
44 }
45
47 void TearDown()
48 {
52
53 delete m_tmpDir;
54 }
55 };
56
58 TEST_F(SoftwareTriggerDBHandlerTest, downloadAndChanged)
59 {
60 SoftwareTriggerObject preFilledObject;
61
62 IntervalOfValidity iov(0, 0, -1, -1);
63
64 // Create a new cut.
65 const auto& cutOne = SoftwareTriggerCut::compile("1 == 1", 1);
66
67 // Upload the first cut
68 SoftwareTriggerDBHandler::upload(cutOne, "test", "cutOne", iov);
69
70 // Try to download a missing cut.
71 SoftwareTriggerDBHandler::uploadTriggerMenu("test", {"cutOne", "cutTwo"}, true, iov);
72 EXPECT_B2FATAL(SoftwareTriggerDBHandler("test"));
73
74 // Download and test the single uploaded cut.
75 SoftwareTriggerDBHandler::uploadTriggerMenu("test", {"cutOne"}, true, iov);
76
77 SoftwareTriggerDBHandler dbHandler("test");
78 const auto& cutsWithNames = dbHandler.getCutsWithNames();
80 cutsWithNames.at("software_trigger_cut&test&cutOne")->checkPreScaled(preFilledObject));
81 EXPECT_THROW(cutsWithNames.at("software_trigger_cut&test&cutTwo"), std::out_of_range);
82
83 // Create the missing cut
84 const auto& cutTwo = SoftwareTriggerCut::compile("1 == 2", 1);
85
86 // Upload the second cut
87 SoftwareTriggerDBHandler::upload(cutOne, "test2", "cutOne", iov);
88 SoftwareTriggerDBHandler::upload(cutTwo, "test2", "cutTwo", iov);
89
90 // Initialize with both cuts uploaded (should not fail)
91 SoftwareTriggerDBHandler::uploadTriggerMenu("test2", {"cutOne", "cutTwo"}, true, iov);
92
93 SoftwareTriggerDBHandler otherDBHandler("test2");
94 const auto& otherCutsWithNames = otherDBHandler.getCutsWithNames();
95 // Try out both cuts
97 otherCutsWithNames.at("software_trigger_cut&test2&cutOne")->checkPreScaled(preFilledObject));
99 otherCutsWithNames.at("software_trigger_cut&test2&cutTwo")->checkPreScaled(preFilledObject));
100
101 }
102
104 TEST_F(SoftwareTriggerDBHandlerTest, pythonUpAndDownload)
105 {
106 IntervalOfValidity iov(0, 0, -1, -1);
107
108 // Create a new cut.
109 auto cutOne = SoftwareTriggerCut::compile("1 == 1", 1);
110
111 SoftwareTriggerDBHandler::upload(cutOne, "test", "cutOne1", iov);
112 auto downloadedCutOne = SoftwareTriggerDBHandler::download("test", "cutOne1");
113
114 ASSERT_NE(downloadedCutOne, nullptr);
115
116 EXPECT_EQ(cutOne->decompile(), downloadedCutOne->decompile());
117 EXPECT_EQ(cutOne->getPreScaleFactor(), downloadedCutOne->getPreScaleFactor());
118 EXPECT_EQ(cutOne->isRejectCut(), downloadedCutOne->isRejectCut());
119
120 // Create a second cut.
121 const auto& cutTwo = SoftwareTriggerCut::compile("1 == 2", 1, true);
122
123 SoftwareTriggerDBHandler::upload(cutTwo, "test", "cutTwo2", iov);
124 const auto& downloadedCutTwo = SoftwareTriggerDBHandler::download("test", "cutTwo2");
125
126 ASSERT_NE(downloadedCutTwo, nullptr);
127
128 EXPECT_EQ(cutTwo->decompile(), downloadedCutTwo->decompile());
129 EXPECT_EQ(cutTwo->getPreScaleFactor(), downloadedCutTwo->getPreScaleFactor());
130 EXPECT_EQ(cutTwo->isRejectCut(), downloadedCutTwo->isRejectCut());
131 }
132 }
134}
static Configuration & getInstance()
Get a reference to the instance which will be used when the Database is initialized.
static DataStore & Instance()
Instance of singleton Store.
Definition: DataStore.cc:54
void setInitializeActive(bool active)
Setter for m_initializeActive.
Definition: DataStore.cc:94
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
Definition: DataStore.cc:86
A class that describes the interval of experiments/runs for which an object in the database is valid.
static std::unique_ptr< SoftwareTriggerCut > compile(const std::string &cut_string, const unsigned int prescaleFactor, const bool rejectCut=false)
Compile a new SoftwareTriggerCut from a cut string (by using the GeneralCut::compile function) and an...
Class to test the down- and upload of trigger cuts to the DB.
void SetUp()
Setup the local DB and the datastore with the event meta data.
TestHelpers::TempDirCreator * m_tmpDir
Do everything in a temporary dir.
static void uploadTriggerMenu(const std::string &baseCutIdentifier, const std::vector< std::string > &cutIdentifiers, bool acceptMode, const IntervalOfValidity &iov)
Upload a new (or replace an old version) trigger menu with the given base and specific names.
static void upload(const std::unique_ptr< SoftwareTriggerCut > &cut, const std::string &baseCutIdentifier, const std::string &cutIdentifier, const IntervalOfValidity &iov)
Upload a new (or replace an old version) cut with the given base and specific name.
static std::unique_ptr< SoftwareTriggerCut > download(const std::string &baseCutIdentifier, const std::string &cutIdentifier)
Download a cut from the database.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
Definition: StoreObjPtr.h:119
changes working directory into a newly created directory, and removes it (and contents) on destructio...
Definition: TestHelpers.h:66
std::string getTempDir() const
Returns path of temporary directory.
Definition: TestHelpers.cc:67
void reset(bool keepEntries=false)
Invalidate all payloads.
Definition: DBStore.cc:177
static DBStore & Instance()
Instance of a singleton DBStore.
Definition: DBStore.cc:28
static void reset(bool keepConfig=false)
Reset the database instance.
Definition: Database.cc:50
@ c_accept
Accept this event.
@ c_noResult
There were not enough information to decide on what to do with the event.
Abstract base class for different kinds of events.
STL namespace.