Belle II Software  release-08-01-10
test_iov.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Test the classes to handle iovs
12 
13 import math
14 import random
15 import unittest
16 import doctest
17 from conditions_db.iov import IntervalOfValidity, IoVSet
18 import conditions_db.iov
19 
20 
21 class TestIntervalOfValidity(unittest.TestCase):
22  """Helper class to test an IoV"""
23 
24  def iovify(self, iov):
25  """Helper function to turn a tuple/None/list(tuple) into iov instances"""
26  if isinstance(iov, list):
27  return tuple(IntervalOfValidity(i) for i in iov)
28  elif iov is not None:
29  return IntervalOfValidity(iov)
30  else:
31  return None
32 
33  def test_create(self):
34  """Test that we get errors when creating invalid iovs"""
35  with self.assertRaises(ValueError):
36  iov = IntervalOfValidity(0)
37  with self.assertRaises(ValueError):
38  iov = IntervalOfValidity(-1, 0, 0, 0)
39  with self.assertRaises(ValueError):
40  iov = IntervalOfValidity(0, -1, 0, 0)
41  with self.assertRaises(ValueError):
42  iov = IntervalOfValidity(-1, -1, 0, 0)
43  with self.assertRaises(ValueError):
44  iov = IntervalOfValidity(1, 0, 0, 0)
45  with self.assertRaises(ValueError):
46  iov = IntervalOfValidity(0, 1, 0, 0)
47  with self.assertRaises(ValueError):
48  iov = IntervalOfValidity(10, 0, 7, 4)
49  with self.assertRaises(ValueError):
50  iov = IntervalOfValidity(0, 0, -1, 4)
51  iov = IntervalOfValidity(0, 0, -1, -1)
52  self.assertEqual(iov.final, (math.inf, math.inf))
53  iov = IntervalOfValidity(0, 0, 1, -1)
54  self.assertEqual(iov.final, (1, math.inf))
55  self.assertEqual(IntervalOfValidity(0, 1, 2, 3), IntervalOfValidity((0, 1, 2, 3)))
56 
57  def test_intersection(self):
58  """Test that we can intersect iovs"""
59  iov = IntervalOfValidity
60  for a, b, c in [
61  [(0, 0, 10, 0), (11, 10, 11, 12), None],
62  [(0, 0, 10, 0), (8, 0, 10, 12), (8, 0, 10, 0)],
63  [(0, 0, 0, 0), (0, 0, -1, -1), (0, 0, 0, 0)],
64  [(0, 0, -1, -1), (20, 12, 21, 234), (20, 12, 21, 234)],
65  [(0, 0, 2, -1), (1, 1, 2, 8), (1, 1, 2, 8)],
66  [(1, 2, 2, -1), (0, 0, 2, 8), (1, 2, 2, 8)],
67  [(1, 0, 3, -1), (0, 0, 2, -1), (1, 0, 2, -1)],
68  [(0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0)],
69  ]:
70  self.assertEqual(iov(a) & iov(b), self.iovifyiovify(c), f'{a} & {b} = {c}')
71  self.assertEqual(iov(b) & iov(a), self.iovifyiovify(c), f'{a} & {b} = {c}')
72 
73  def test_union(self):
74  """Test that we can calculate the union of iovs"""
75  iov = IntervalOfValidity
76  for a, b, c in [
77  [(0, 0, 10, 0), (11, 10, 11, 12), None],
78  [(0, 0, 10, 0), (8, 0, 10, 12), (0, 0, 10, 12)],
79  [(0, 0, 0, 0), (0, 0, -1, -1), (0, 0, -1, -1)],
80  [(0, 0, -1, -1), (20, 12, 21, 234), (0, 0, -1, -1)],
81  [(0, 0, 2, -1), (1, 1, 2, 8), (0, 0, 2, -1)],
82  [(1, 2, 2, -1), (0, 0, 2, 8), (0, 0, 2, -1)],
83  [(1, 0, 3, -1), (0, 0, 2, -1), (0, 0, 3, -1)],
84  [(0, 0, 0, 0), (0, 0, 0, 0), (0, 0, 0, 0)],
85  [(0, 0, 1, 1), (1, 2, 1, 3), (0, 0, 1, 3)],
86  [(0, 0, 1, 1), (1, 3, 1, 4), None],
87  [(0, 0, 1, -1), (2, 0, 2, 1), (0, 0, 2, 1)],
88  [(0, 0, 1, -1), (2, 0, 2, 1), (0, 0, 2, 1)],
89  [(0, 0, 1, -1), (2, 1, 2, 1), None],
90  [(0, 0, 1, -1), (1, 9999, 2, 1), (0, 0, 2, 1)],
91  ]:
92  self.assertEqual(iov(a) | iov(b), self.iovifyiovify(c), f'{a} | {b} = {c}')
93  self.assertEqual(iov(b) | iov(a), self.iovifyiovify(c), f'{a} | {b} = {c}')
94 
96  """Test that we can calculate the union starting at run 1 as well"""
97  iov = IntervalOfValidity
98  self.assertEqual(iov(0, 0, 1, -1).union(iov(2, 1, 2, -1)), None)
99  self.assertEqual(iov(0, 0, 1, -1).union(iov(2, 1, 2, -1), True), iov(0, 0, 2, -1))
100 
101  def test_subtract(self):
102  """Test subtracting iovs from another"""
103  iov = IntervalOfValidity
104  for a, b, c, d in [
105  [(0, 0, 10, 0), (11, 10, 11, 12), (0, 0, 10, 0), (11, 10, 11, 12)],
106  [(0, 0, 10, 0), (8, 0, 10, 12), (0, 0, 7, -1), (10, 1, 10, 12)],
107  [(0, 0, 0, 0), (0, 0, -1, -1), None, (0, 1, -1, -1)],
108  [(0, 0, -1, -1), (20, 12, 21, 234), [(0, 0, 20, 11), (21, 235, -1, -1)], None],
109  [(0, 0, 2, -1), (1, 1, 2, 8), [(0, 0, 1, 0), (2, 9, 2, -1)], None],
110  [(1, 2, 2, -1), (0, 0, 2, 8), (2, 9, 2, -1), (0, 0, 1, 1)],
111  [(1, 0, 3, -1), (0, 0, 2, -1), (3, 0, 3, -1), (0, 0, 0, -1)],
112  [(0, 0, -1, -1), (0, 0, -1, -1), None, None],
113  [(0, 0, 1, -1), (0, 0, 1, -1), None, None],
114  [(0, 0, 0, 0), (0, 0, 0, 0), None, None],
115  [(0, 0, 1, -1), (2, 0, 2, -1), (0, 0, 1, -1), (2, 0, 2, -1)],
116  [(11, 0, 12, 86), (0, 0, 11, -1), (12, 0, 12, 86), (0, 0, 10, -1)],
117  ]:
118  self.assertEqual(iov(a) - iov(b), self.iovifyiovify(c), f'{a} - {b} = {c}')
119  self.assertEqual(iov(b) - iov(a), self.iovifyiovify(d), f'{b} - {a} = {d}')
120 
121 
122 class TestIoVSet(unittest.TestCase):
123  """Helper class to test a set of IoVs"""
124 
125  def test_add(self):
126  """Test adding iovs to a set"""
127  inputs = [
128  [(0, 0, 0, -1), (1, 0, 1, -1), (2, 1, 2, -1), (3, 1, 3, 1), (3, 2, 3, 2), (3, 3, 3, 6), (3, 8, 3, 8)],
129  [(0, i, 0, i) for i in range(0, 100)],
130  [(0, i, 0, i) for i in range(0, 100, 2)],
131  ]
132  results = [
133  [(0, 0, 1, -1), (2, 1, 2, -1), (3, 1, 3, 6), (3, 8, 3, 8)],
134  [(0, 0, 0, 99)],
135  [(0, i, 0, i) for i in range(0, 100, 2)],
136  ]
137 
138  for _ in range(10):
139  for input, output in zip(inputs, results):
140  s = IoVSet()
141  for iov in random.sample(input, len(input)):
142  s.add(iov)
143  output = {IntervalOfValidity(e) for e in output}
144  self.assertEqual(s.iovs, output)
145 
146  def test_remove(self):
147  """Test removing iovs from a set"""
148  s = IoVSet()
149  s.add((0, 0, 0, 100))
150  to_remove = [(0, i, 0, i) for i in range(0, 100, 2)]
151  result = {IntervalOfValidity(0, i, 0, i) for i in range(1, 99, 2)}
152  result.add(IntervalOfValidity(0, 99, 0, 100))
153  for _ in range(10):
154  for iov in random.sample(to_remove, len(to_remove)):
155  s.remove(iov)
156  self.assertEqual(s.iovs, result)
157 
158  def test_intersection(self):
159  """Test intersecting two sets"""
160  iovs = []
161  for i in range(6):
162  iovs.append((i, 0, i + 5, -1))
163  result = {IntervalOfValidity(5, 0, 5, -1)}
164  for _ in range(10):
165  a = IoVSet([IntervalOfValidity.always()])
166  for iov in random.sample(iovs, len(iovs)):
167  a = a.intersect(iov)
168  self.assertEqual(a.iovs, result)
169  full = IoVSet(iovs, allow_overlaps=True)
170  self.assertEqual(full.iovs, {IntervalOfValidity(0, 0, 10, -1)})
171 
172 
173 def load_tests(loader, tests, ignore):
174  """Add the doctests to the list of tests"""
175  tests.addTests(doctest.DocTestSuite(conditions_db.iov))
176  return tests
177 
178 
179 if __name__ == "__main__":
180  # test everything
181  unittest.main()
def test_remove(self)
Definition: test_iov.py:146
def test_add(self)
Definition: test_iov.py:125
def test_intersection(self)
Definition: test_iov.py:158