Belle II Software  release-08-01-10
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 
16 using namespace std;
17 
18 namespace Belle2 {
23  namespace SoftwareTrigger {
24 
26  class SoftwareTriggerDBHandlerTest : public ::testing::Test {
28  TestHelpers::TempDirCreator* m_tmpDir = nullptr;
29 
31  void SetUp()
32  {
33  m_tmpDir = new TestHelpers::TempDirCreator;
34 
36  DataStore::Instance().setInitializeActive(true);
37  evtPtr.registerInDataStore();
38  DataStore::Instance().setInitializeActive(false);
39  evtPtr.construct(1, 0, 0);
40 
41  auto& conf = Conditions::Configuration::getInstance();
42  conf.setNewPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
43  conf.prependTestingPayloadLocation(m_tmpDir->getTempDir() + "/testPayloads/TestDatabase.txt");
44  }
45 
47  void TearDown()
48  {
49  Database::reset();
50  DBStore::Instance().reset();
51  DataStore::Instance().reset();
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();
79  EXPECT_EQ(SoftwareTriggerCutResult::c_accept,
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
96  EXPECT_EQ(SoftwareTriggerCutResult::c_accept,
97  otherCutsWithNames.at("software_trigger_cut&test2&cutOne")->checkPreScaled(preFilledObject));
98  EXPECT_EQ(SoftwareTriggerCutResult::c_noResult,
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 }
A class that describes the interval of experiments/runs for which an object in the database is valid.
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.
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
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:72
Abstract base class for different kinds of events.