Belle II Software development
python_datastore.py
1#!/usr/bin/env python3
2
3
10
11import basf2
12from ROOT import Belle2
13import unittest
14
15# @cond internal_test
16
17
18class PythonDataStoreTests(unittest.TestCase):
19
20 def test_PyStoreObj(self):
21 testObj = Belle2.PyStoreObj("TestObj")
22 persistentObj = Belle2.PyStoreObj("PersistentObj", Belle2.DataStore.c_Persistent)
23 objList = (testObj, persistentObj)
24
25 nEventObj = len(Belle2.PyStoreObj.list())
26 nPersistentObj = len(Belle2.PyStoreObj.list())
27
28 for obj in objList:
29 self.assertTrue(not obj)
30 self.assertTrue(not obj.create())
31
32 # class unknown
33 self.assertTrue(not obj.registerInDataStore())
34
35 # Object not yet created
36 self.assertTrue(not obj)
37
38 self.assertEqual(nEventObj, len(Belle2.PyStoreObj.list()))
39 self.assertEqual(nPersistentObj, len(Belle2.PyStoreObj.list(Belle2.DataStore.c_Persistent)))
40
41 # arrays with explicit class
42 def test_PyStoreObj_with_known_class(self):
43 nEventObj = len(Belle2.PyStoreObj.list())
44 nPersistentObj = len(Belle2.PyStoreObj.list())
45
46 eventMetaData = Belle2.PyStoreObj("EventMetaData")
47 self.assertTrue(eventMetaData.registerInDataStore())
48
49 self.assertFalse(eventMetaData.isValid())
50 eventMetaData.create()
51 self.assertTrue(eventMetaData.isValid())
52
53 # Test direct access to contained object
54 eventMetaData.setExperiment(17)
55
56 # Retrieve from other module
57 eventMetaData2 = Belle2.PyStoreObj(Belle2.EventMetaData.Class())
58 self.assertTrue(eventMetaData2.isValid())
59 self.assertEqual(17, eventMetaData.getExperiment())
60
61 # One persistent object was created in the process
62 self.assertEqual(nEventObj + 1, len(Belle2.PyStoreObj.list()))
63 self.assertEqual(nPersistentObj, len(Belle2.PyStoreObj.list(Belle2.DataStore.c_Persistent)))
64
65 def test_PyStoreArray(self):
66 # arrays
67 arrayList = (Belle2.PyStoreArray("TestArray"),
68 Belle2.PyStoreArray("PersistentArray", Belle2.DataStore.c_Persistent))
69
70 self.assertEqual(0, len(Belle2.PyStoreArray.list()))
71 self.assertEqual(0, len(Belle2.PyStoreArray.list(Belle2.DataStore.c_Persistent)))
72
73 for array in arrayList:
74 self.assertTrue(not array)
75 self.assertEqual(0, array.getEntries())
76 self.assertEqual(0, len(array))
77
78 self.assertTrue(not array[1234])
79 self.assertTrue(not array.appendNew())
80 self.assertEqual(0, len(array))
81
82 def test_EnumBackwardsCompatibility(self):
83 self.assertEqual(0, Belle2.DataStore.c_Event)
84 self.assertEqual(1, Belle2.DataStore.c_Persistent)
85
86 # arrays with explicit class
87 def test_PyStoreArray_with_known_class(self):
88 # First module creates and registers two store arrays and a relation
89 array = Belle2.PyStoreArray(Belle2.RelationsObject.Class())
90
91 self.assertTrue(array.registerInDataStore("RelationsObjects"))
92 self.assertEqual("RelationsObjects", array.getName())
93
94 otherArray = Belle2.PyStoreArray(Belle2.RelationsObject.Class(), "OtherRelationsObjects")
95
96 self.assertEqual("OtherRelationsObjects", otherArray.getName())
97 self.assertTrue(otherArray.registerInDataStore())
98
99 array.registerRelationTo(otherArray)
100
101 self.assertTrue(array.hasRelationTo(otherArray))
102 self.assertTrue(otherArray.hasRelationFrom(array))
103 self.assertFalse(array.hasRelationFrom(otherArray))
104 self.assertFalse(otherArray.hasRelationTo(array))
105
106 obj = array.appendNew()
107 self.assertTrue(obj)
108 self.assertEqual(1, len(array))
109 self.assertTrue(array[0])
110
111 for obj in array:
112 # Valid iterator
113 break
114 else:
115 # Invalid iterator
116 self.assertTrue(False)
117
118 otherObject = otherArray.appendNew()
119 obj.addRelationTo(otherObject)
120
121 # Second module requires and reads the store arrays
122
123 # Arrays can be retrieved like above or by their names only
124 array2 = Belle2.PyStoreArray("RelationsObjects")
125 array2.isRequired()
126 otherArray2 = Belle2.PyStoreArray("OtherRelationsObjects")
127 otherArray2.isRequired()
128
129 self.assertEqual(1, len(array2))
130 self.assertEqual(1, len(otherArray2))
131
132 array2.requireRelationTo(otherArray2)
133 obj2 = array2[0]
134 self.assertEqual(obj, obj2)
135
136 otherObject2 = obj2.getRelated("OtherRelationsObjects")
137 self.assertEqual(otherObject, otherObject2)
138
139
140if __name__ == "__main__":
141 basf2.B2INFO("Following error messages are expected, please ignore.")
142 unittest.main()
143 basf2.B2INFO("Previous errors are expected, please ignore.")
144
145# @endcond
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Definition: PyStoreArray.cc:28
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:67
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition: PyStoreObj.cc:28