Belle II Software development
test_b2cal-extend-eventdependency-payload.py
1
8
9import glob
10import os
11import subprocess
12import basf2
13import b2test_utils
14
15'''
16Test the b2cal-extend-eventdependency-tool.
17'''
18
19
20if __name__ == '__main__':
21
23
24 import ROOT # noqa
25
26 # Location of the local database to be used during the whole test
27 database_location = 'testingdb/testingdb.txt'
28 basf2.conditions.expert_settings(save_payloads=database_location)
29
30 # Create a simple EventDependency payload with a BeamSpot object there
31 beamspot = ROOT.Belle2.BeamSpot()
32 event_object = ROOT.Belle2.EventDependency(beamspot)
33 database = ROOT.Belle2.Database.Instance()
34 iov = ROOT.Belle2.IntervalOfValidity.always()
35 created = database.storeData('BeamSpot', event_object, iov)
36 if not created:
37 basf2.B2FATAL('Something went wrong when creating the payload for the test')
38
39 # Locate the payload file
40 payload_file = glob.glob(f'{basf2.find_file(os.path.dirname(database_location))}/*root')[0]
41
42 # Test if the tool exits with a proper return code in case of incorrect calls
43 # First test: incorrect call, wrong payload file
44 result = subprocess.run(['b2cal-extend-eventdependency-payload', '-i', 'foo.bar',
45 '-n', 'BeamSpot', '--iov', '1', '2', '3', '4', '--output_db',
46 f'{database_location}'])
47 assert(not result.returncode == 0)
48 # Second test: incorrect call, wrong payload name
49 result = subprocess.run(['b2cal-extend-eventdependency-payload', '-i', f'{payload_file}',
50 '-n', 'FooBar', '--iov', '1', '2', '3', '4', '--output_db',
51 f'{database_location}'])
52 assert(not result.returncode == 0)
53 # Third test: incorrect call, incomplete IoV
54 result = subprocess.run(['b2cal-extend-eventdependency-payload', '-i', f'{payload_file}',
55 '-n', 'BeamSpot', '--iov', '1', '2', '3', '--output_db',
56 f'{database_location}'])
57 assert(not result.returncode == 0)
58 # Fourth test: incorrect call, wrong database file
59 result = subprocess.run(['b2cal-extend-eventdependency-payload', '-i', f'{payload_file}',
60 '-n', 'BeamSpot', '--iov', '1', '2', '3', '4', '--output_db',
61 'foo/bar'])
62 assert(not result.returncode == 0)
63
64 # Finally, test if the tool works fine if called correctly
65 result = subprocess.run(['b2cal-extend-eventdependency-payload', '-i', f'{payload_file}',
66 '-n', 'BeamSpot', '--iov', '1', '2', '3', '4', '--output_db',
67 f'{database_location}'])
68 assert(result.returncode == 0)
69
70 # Check if the tool appended correctly the new payload to the existing database
71 with open(database_location) as f:
72 line_number = 0
73 revision = 0
74 for line in f:
75 fields = line.split(' ')
76 assert(fields[0] == 'dbstore/BeamSpot')
77 # In the first line we have the first created payload: it must have an infinite IoV
78 if line_number == 0:
79 assert(fields[2].strip() == '0,0,-1,-1')
80 # While in the second line the IoV must be (1,2,3,4)
81 elif line_number == 1:
82 assert(fields[2].strip() == '1,2,3,4')
83 line_number += 1
def clean_working_directory()
Definition: __init__.py:189