8 #include <framework/database/IntervalOfValidity.h>
9 #include <framework/database/Database.h>
10 #include <framework/database/Configuration.h>
11 #include <framework/database/DBObjPtr.h>
12 #include <framework/database/DBArray.h>
13 #include <framework/database/EventDependency.h>
14 #include <framework/database/PayloadFile.h>
15 #include <framework/database/DBPointer.h>
16 #include <framework/datastore/StoreObjPtr.h>
17 #include <framework/dataobjects/EventMetaData.h>
18 #include <framework/utilities/TestHelpers.h>
19 #include <framework/geometry/BFieldManager.h>
20 #include <framework/dbobjects/MagneticField.h>
21 #include <framework/dbobjects/MagneticFieldComponentConstant.h>
24 #include <TClonesArray.h>
26 #include <gtest/gtest.h>
38 class DataBaseTest :
public ::testing::Test {
42 enum EDatabaseType {c_local, c_central, c_chain, c_default};
45 EDatabaseType m_dbType = c_local;
53 DataStore::Instance().setInitializeActive(
true);
55 DataStore::Instance().setInitializeActive(
false);
58 auto& c = Conditions::Configuration::getInstance();
62 c.overrideGlobalTags();
63 c.setMetadataProviders({});
64 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
65 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
68 c.setGlobalTags({
"default"});
69 c.overrideGlobalTags();
72 c.setGlobalTags({
"default"});
73 c.overrideGlobalTags();
74 c.setMetadataProviders({});
75 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
76 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
82 if (m_dbType != c_central) {
83 list<Database::DBImportQuery> query;
84 TClonesArray array(
"TObject");
85 for (
int experiment = 1; experiment <= 5; experiment++) {
88 TString name =
"Experiment ";
92 new (array[experiment - 1]) TObject;
93 array[experiment - 1]->SetUniqueID(experiment);
94 Database::Instance().storeData(
"TObjects", &array, iov);
96 FILE* f = fopen(
"file.xml",
"w");
97 fprintf(f,
"Experiment %d\n", experiment);
99 Database::Instance().addPayload(
"file.xml",
"file.xml", iov);
101 if (m_dbType != c_chain) {
102 Database::Instance().storeData(query);
106 intraRunDep.add(10,
new TNamed(
"B",
"B"));
107 intraRunDep.add(50,
new TNamed(
"C",
"C"));
109 Database::Instance().storeData(
"IntraRun", &intraRunDep, iov1);
111 Database::Instance().storeData(
"IntraRun",
new TNamed(
"X",
"X"), iov2);
116 void TearDown()
override
118 if (m_dbType != c_central) std::filesystem::remove_all(
"testPayloads");
120 DataStore::Instance().reset();
149 TEST_F(DataBaseTest, IntervalOfValidityOperations)
183 EXPECT_TRUE(iov1.
empty());
202 EXPECT_TRUE(iov2.
empty());
220 TEST_F(DataBaseTest, getData)
222 EXPECT_TRUE(strcmp(Database::Instance().getData(
"TNamed", 1, 1)->GetName(),
"Experiment 1") == 0);
223 EXPECT_TRUE(strcmp(Database::Instance().getData(
"TNamed", 4, 1)->GetName(),
"Experiment 4") == 0);
224 EXPECT_TRUE(Database::Instance().getData(
"TNamed", 6, 1) == 0);
233 evtPtr->setExperiment(1);
234 DBStore::Instance().update();
236 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
237 evtPtr->setExperiment(4);
238 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
239 DBStore::Instance().update();
240 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 4") == 0);
241 evtPtr->setExperiment(7);
242 DBStore::Instance().update();
255 for (
const auto& o : missing) {
263 evtPtr->setExperiment(1);
264 DBStore::Instance().update();
265 EXPECT_TRUE(objects);
266 EXPECT_FALSE(missing);
267 EXPECT_EQ(objects.getEntries(), 1);
268 EXPECT_EQ(objects[0]->GetUniqueID(), 1);
269 evtPtr->setExperiment(4);
270 EXPECT_EQ(objects.getEntries(), 1);
271 DBStore::Instance().update();
272 EXPECT_EQ(objects.getEntries(), 4);
276 for (
const auto& o : objects) {
277 EXPECT_EQ(o.GetUniqueID(), ++i);
285 for (
const auto& o : missing) {
291 evtPtr->setExperiment(7);
292 DBStore::Instance().update();
293 EXPECT_FALSE(objects);
297 for (
const auto& o : objects) {
298 EXPECT_EQ(o.GetUniqueID(), ++i);
305 TEST_F(DataBaseTest, DBArrayRange)
310 evtPtr->setExperiment(3);
311 DBStore::Instance().update();
312 EXPECT_THROW(objects[-1], std::out_of_range);
313 EXPECT_THROW(objects[3], std::out_of_range);
317 TEST_F(DataBaseTest, TypeCheck)
327 TEST_F(DataBaseTest, IntraRun)
332 evtPtr->setExperiment(1);
335 DBStore::Instance().update();
336 DBStore::Instance().updateEvent();
337 EXPECT_TRUE(strcmp(intraRun->GetName(),
"A") == 0);
339 evtPtr->setEvent(10);
340 DBStore::Instance().updateEvent();
341 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
343 evtPtr->setEvent(49);
344 DBStore::Instance().updateEvent();
345 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
348 DBStore::Instance().update();
349 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
351 evtPtr->setEvent(50);
352 DBStore::Instance().updateEvent();
353 EXPECT_TRUE(strcmp(intraRun->GetName(),
"C") == 0);
356 DBStore::Instance().update();
357 EXPECT_TRUE(strcmp(intraRun->GetName(),
"X") == 0);
361 TEST_F(DataBaseTest, HasChanged)
364 evtPtr->setExperiment(0);
365 DBStore::Instance().update();
370 evtPtr->setExperiment(1);
373 DBStore::Instance().update();
377 DBStore::Instance().update();
380 evtPtr->setExperiment(5);
381 DBStore::Instance().update();
384 evtPtr->setExperiment(7);
385 DBStore::Instance().update();
388 evtPtr->setExperiment(1);
392 DBStore::Instance().update();
393 DBStore::Instance().updateEvent();
394 EXPECT_TRUE(intraRun.hasChanged());
397 DBStore::Instance().updateEvent();
398 EXPECT_FALSE(intraRun.hasChanged());
400 evtPtr->setEvent(10);
401 DBStore::Instance().updateEvent();
402 EXPECT_TRUE(intraRun.hasChanged());
404 evtPtr->setEvent(1000);
405 DBStore::Instance().updateEvent();
406 EXPECT_TRUE(intraRun.hasChanged());
409 DBStore::Instance().update();
410 DBStore::Instance().updateEvent();
411 EXPECT_TRUE(intraRun.hasChanged());
420 evtPtr->setExperiment(1);
421 DBStore::Instance().update();
422 EXPECT_EQ(payload->getContent(),
"Experiment 1\n") << payload->getFileName();
423 evtPtr->setExperiment(4);
424 DBStore::Instance().update();
425 EXPECT_EQ(payload->getContent(),
"Experiment 4\n") << payload->getFileName();
426 evtPtr->setExperiment(7);
427 DBStore::Instance().update();
428 EXPECT_FALSE(payload);
432 int callbackCounter = 0;
448 TEST_F(DataBaseTest, Callbacks)
457 EXPECT_EQ(callbackCounter, 0);
459 evtPtr->setExperiment(2);
460 DBStore::Instance().update();
461 EXPECT_EQ(callbackCounter, 1);
463 evtPtr->setExperiment(4);
464 DBStore::Instance().update();
465 EXPECT_EQ(callbackCounter, 2);
466 DBStore::Instance().update();
467 EXPECT_EQ(callbackCounter, 2);
469 evtPtr->setExperiment(6);
470 DBStore::Instance().update();
471 EXPECT_EQ(callbackCounter, 3);
473 evtPtr->setExperiment(7);
474 DBStore::Instance().update();
475 EXPECT_EQ(callbackCounter, 3);
478 objects.addCallback(&callback);
480 evtPtr->setExperiment(1);
481 DBStore::Instance().update();
483 EXPECT_EQ(callbackCounter, 5);
486 intraRun.addCallback(&callbackObject, &Callback::callback);
490 DBStore::Instance().updateEvent();
491 EXPECT_EQ(callbackCounter, 5);
494 DBStore::Instance().updateEvent();
495 EXPECT_EQ(callbackCounter, 5);
497 evtPtr->setEvent(10);
498 DBStore::Instance().updateEvent();
499 EXPECT_EQ(callbackCounter, 6);
502 DBStore::Instance().updateEvent();
503 EXPECT_EQ(callbackCounter, 7);
507 TEST_F(DataBaseTest, KeyAccess)
510 evtPtr->setExperiment(1);
511 DBStore::Instance().update();
514 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
515 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2),
nullptr);
516 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
false)->GetUniqueID(), 1);
517 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
true),
nullptr);
519 evtPtr->setExperiment(2);
520 DBStore::Instance().update();
522 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
523 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2)->GetUniqueID(), 2);
530 evtPtr->setExperiment(2);
531 DBStore::Instance().update();
534 EXPECT_EQ(ptr.key(), 1);
535 EXPECT_TRUE(ptr.isValid());
536 EXPECT_EQ(ptr->GetUniqueID(), 1);
538 EXPECT_EQ(ptr->GetUniqueID(), 2);
540 EXPECT_FALSE(ptr.isValid());
543 TEST_F(DataBaseTest, CleanupReattachDeathtest)
546 evtPtr->setExperiment(2);
547 DBStore::Instance().update();
551 double Bz = BFieldManager::getField(0, 0, 0).Z() / Unit::T;
553 DBStore::Instance().reset();
558 EXPECT_B2FATAL(BFieldManager::getFieldInTesla({0, 0, 0}));
564 Bz = BFieldManager::getField(0, 0, 0).Z() / Unit::T;
570 class DataBaseNoDataStoreTest :
public ::testing::Test {
574 enum EDatabaseType {c_local, c_central, c_chain, c_default};
577 EDatabaseType m_dbType = c_local;
583 DataBaseNoDataStoreTest() : m_event(0, 0, 1) {};
586 void SetUp()
override
588 auto& c = Conditions::Configuration::getInstance();
592 c.overrideGlobalTags();
593 c.setMetadataProviders({});
594 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
595 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
598 c.setGlobalTags({
"default"});
599 c.overrideGlobalTags();
602 c.setGlobalTags({
"default"});
603 c.overrideGlobalTags();
604 c.setMetadataProviders({});
605 c.setNewPayloadLocation(
"testPayloads/TestDatabase.txt");
606 c.appendTestingPayloadLocation(
"testPayloads/TestDatabase.txt");
612 if (m_dbType != c_central) {
613 list<Database::DBImportQuery> query;
614 TClonesArray array(
"TObject");
615 for (
int experiment = 1; experiment <= 5; experiment++) {
618 TString name =
"Experiment ";
622 new (array[experiment - 1]) TObject;
623 array[experiment - 1]->SetUniqueID(experiment);
624 Database::Instance().storeData(
"TObjects", &array, iov);
626 FILE* f = fopen(
"file.xml",
"w");
627 fprintf(f,
"Experiment %d\n", experiment);
629 Database::Instance().addPayload(
"file.xml",
"file.xml", iov);
631 if (m_dbType != c_chain) {
632 Database::Instance().storeData(query);
636 intraRunDep.add(10,
new TNamed(
"B",
"B"));
637 intraRunDep.add(50,
new TNamed(
"C",
"C"));
639 Database::Instance().storeData(
"IntraRun", &intraRunDep, iov1);
641 Database::Instance().storeData(
"IntraRun",
new TNamed(
"X",
"X"), iov2);
646 void TearDown()
override
648 if (m_dbType != c_central) std::filesystem::remove_all(
"testPayloads");
657 DBStore::Instance().update(m_event);
660 DBStore::Instance().update(m_event);
662 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
664 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 1") == 0);
665 DBStore::Instance().update(m_event);
666 EXPECT_TRUE(strcmp(named->GetName(),
"Experiment 4") == 0);
668 DBStore::Instance().update(m_event);
675 DBStore::Instance().update(m_event);
681 for (
const auto& o : missing) {
690 DBStore::Instance().update(m_event);
691 EXPECT_TRUE(objects);
692 EXPECT_FALSE(missing);
693 EXPECT_EQ(objects.getEntries(), 1);
694 EXPECT_EQ(objects[0]->GetUniqueID(), 1);
696 EXPECT_EQ(objects.getEntries(), 1);
697 DBStore::Instance().update(m_event);
698 EXPECT_EQ(objects.getEntries(), 4);
702 for (
const auto& o : objects) {
703 EXPECT_EQ(o.GetUniqueID(), ++i);
711 for (
const auto& o : missing) {
718 DBStore::Instance().update(m_event);
719 EXPECT_FALSE(objects);
723 for (
const auto& o : objects) {
724 EXPECT_EQ(o.GetUniqueID(), ++i);
731 TEST_F(DataBaseNoDataStoreTest, DBArrayRange)
736 DBStore::Instance().update(m_event);
737 EXPECT_THROW(objects[-1], std::out_of_range);
738 EXPECT_THROW(objects[3], std::out_of_range);
742 TEST_F(DataBaseNoDataStoreTest, TypeCheck)
752 TEST_F(DataBaseNoDataStoreTest, IntraRun)
759 DBStore::Instance().update(m_event);
760 DBStore::Instance().updateEvent(m_event.
getEvent());
761 EXPECT_TRUE(strcmp(intraRun->GetName(),
"A") == 0);
764 DBStore::Instance().updateEvent(m_event.
getEvent());
765 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
768 DBStore::Instance().updateEvent(m_event.
getEvent());
769 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
772 DBStore::Instance().update(m_event);
773 EXPECT_TRUE(strcmp(intraRun->GetName(),
"B") == 0);
776 DBStore::Instance().updateEvent(m_event.
getEvent());
777 EXPECT_TRUE(strcmp(intraRun->GetName(),
"C") == 0);
780 DBStore::Instance().update(m_event);
781 EXPECT_TRUE(strcmp(intraRun->GetName(),
"X") == 0);
785 TEST_F(DataBaseNoDataStoreTest, HasChanged)
788 DBStore::Instance().update(m_event);
796 DBStore::Instance().update(m_event);
800 DBStore::Instance().update(m_event);
804 DBStore::Instance().update(m_event);
808 DBStore::Instance().update(m_event);
815 DBStore::Instance().update(m_event);
816 DBStore::Instance().updateEvent(m_event.
getEvent());
817 EXPECT_TRUE(intraRun.hasChanged());
820 DBStore::Instance().updateEvent(m_event.
getEvent());
821 EXPECT_FALSE(intraRun.hasChanged());
824 DBStore::Instance().updateEvent(m_event.
getEvent());
825 EXPECT_TRUE(intraRun.hasChanged());
828 DBStore::Instance().updateEvent(m_event.
getEvent());
829 EXPECT_TRUE(intraRun.hasChanged());
832 DBStore::Instance().update(m_event);
833 DBStore::Instance().updateEvent(m_event.
getEvent());
834 EXPECT_TRUE(intraRun.hasChanged());
840 DBStore::Instance().update(m_event);
843 DBStore::Instance().update(m_event);
844 EXPECT_EQ(payload->getContent(),
"Experiment 1\n") << payload->getFileName();
846 DBStore::Instance().update(m_event);
847 EXPECT_EQ(payload->getContent(),
"Experiment 4\n") << payload->getFileName();
849 DBStore::Instance().update(m_event);
850 EXPECT_FALSE(payload);
855 TEST_F(DataBaseNoDataStoreTest, Callbacks)
863 EXPECT_EQ(callbackCounter, 0);
866 DBStore::Instance().update(m_event);
867 EXPECT_EQ(callbackCounter, 1);
870 DBStore::Instance().update(m_event);
871 EXPECT_EQ(callbackCounter, 2);
872 DBStore::Instance().update(m_event);
873 EXPECT_EQ(callbackCounter, 2);
876 DBStore::Instance().update(m_event);
877 EXPECT_EQ(callbackCounter, 3);
880 DBStore::Instance().update(m_event);
881 EXPECT_EQ(callbackCounter, 3);
884 objects.addCallback(&callback);
887 DBStore::Instance().update(m_event);
889 EXPECT_EQ(callbackCounter, 5);
892 intraRun.addCallback(&callbackObject, &Callback::callback);
896 DBStore::Instance().updateEvent(m_event.
getEvent());
897 EXPECT_EQ(callbackCounter, 5);
900 DBStore::Instance().updateEvent(m_event.
getEvent());
901 EXPECT_EQ(callbackCounter, 5);
904 DBStore::Instance().updateEvent(m_event.
getEvent());
905 EXPECT_EQ(callbackCounter, 6);
908 DBStore::Instance().updateEvent(m_event.
getEvent());
909 EXPECT_EQ(callbackCounter, 7);
913 TEST_F(DataBaseNoDataStoreTest, KeyAccess)
916 DBStore::Instance().update(m_event);
919 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
920 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2),
nullptr);
921 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
false)->GetUniqueID(), 1);
922 EXPECT_EQ(objects.getByKey(&TObject::IsFolder,
true),
nullptr);
925 DBStore::Instance().update(m_event);
927 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 1)->GetUniqueID(), 1);
928 EXPECT_EQ(objects.getByKey<
unsigned int>(&TObject::GetUniqueID, 2)->GetUniqueID(), 2);
934 DBStore::Instance().update(m_event);
937 EXPECT_EQ(ptr.key(), 1);
938 EXPECT_TRUE(ptr.isValid());
939 EXPECT_EQ(ptr->GetUniqueID(), 1);
941 EXPECT_EQ(ptr->GetUniqueID(), 2);
943 EXPECT_FALSE(ptr.isValid());
void addCallback(std::function< void(const std::string &)> callback, bool onDestruction=false)
Add a callback method.
bool isValid() const
Check whether a valid object was obtained from the database.
bool hasChanged()
Check whether the object has changed since the last call to hasChanged of the accessor).
Class for accessing arrays of objects in the database.
Specialization of DBObjPtr in case of PayloadFiles.
Class for accessing objects in the database.
Class for pointing to an element in an array stored in the database.
Class for handling changing conditions as a function of event number.
A class that describes the interval of experiments/runs for which an object in the database is valid.
bool trimOverlap(IntervalOfValidity &iov, bool trimOlder=true)
Remove the overlap between two intervals of validity by shortening one of them.
bool empty() const
Function that checks whether the validity interval is empty.
Describe one component of the Geometry.
A wrapper class for payload files used by the Database and DBStore classes.
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.
bool construct(Args &&... params)
Construct an object of type T in this StoreObjPtr, using the provided constructor arguments.
changes working directory into a newly created directory, and removes it (and contents) on destructio...
static const double T
[tesla]
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
static DBStore & Instance()
Instance of a singleton DBStore.
void addConstantOverride(const std::string &name, TObject *obj, bool oneRun=false)
Add constant override payload.
Abstract base class for different kinds of events.
Struct for bulk write queries.