Belle II Software development
test_calculationList.py
1
8from unittest import TestCase, main
9from hep_ipython_tools.calculation_list import create_every_parameter_combination, create_all_calculations
10
11# @cond internal_test
12
13
14class TestCalculationList(TestCase):
15
16 def test_create_every_parameter_combination(self):
17
18 parameter_names = ["A", "B", "C"]
19 parameter_values = [
20 [1, 2],
21 ["a", "b"],
22 ["(", ")"]
23 ]
24
25 every_parameter_combination = create_every_parameter_combination(parameter_names,
26 parameter_values)
27
28 self.assertEqual(len(every_parameter_combination), 8)
29
30 self.assertIn({"A": 1, "B": "a", "C": "("}, every_parameter_combination)
31 self.assertIn({"A": 1, "B": "a", "C": ")"}, every_parameter_combination)
32 self.assertIn({"A": 1, "B": "b", "C": "("}, every_parameter_combination)
33 self.assertIn({"A": 1, "B": "b", "C": ")"}, every_parameter_combination)
34 self.assertIn({"A": 2, "B": "a", "C": "("}, every_parameter_combination)
35 self.assertIn({"A": 2, "B": "a", "C": ")"}, every_parameter_combination)
36 self.assertIn({"A": 2, "B": "b", "C": "("}, every_parameter_combination)
37 self.assertIn({"A": 2, "B": "b", "C": ")"}, every_parameter_combination)
38
39 def test_create_all_calculations_no_queue(self):
40 def creator_function(x, y):
41 return {"a": 2 * x, "b": y + 1}
42
43 all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(creator_function,
44 x=[1, 2], y=[3, 4])
45
46 all_calculation_kwargs = list(all_calculation_kwargs)
47 all_queues = list(all_queues)
48 every_parameter_combination = list(every_parameter_combination)
49
50 self.assertEqual(len(all_calculation_kwargs), 4)
51 self.assertEqual(len(all_queues), 4)
52 self.assertEqual(len(every_parameter_combination), 4)
53
54 self.assertIn({"x": 1, "y": 3}, every_parameter_combination)
55 self.assertIn({"x": 1, "y": 4}, every_parameter_combination)
56 self.assertIn({"x": 2, "y": 3}, every_parameter_combination)
57 self.assertIn({"x": 2, "y": 4}, every_parameter_combination)
58
59 for combination in [{"x": 1, "y": 3}, {"x": 1, "y": 4}, {"x": 2, "y": 3}, {"x": 2, "y": 4}]:
60 index = every_parameter_combination.index(combination)
61 x = combination["x"]
62 y = combination["y"]
63 self.assertEqual(all_calculation_kwargs[index], {"a": 2 * x, "b": y + 1})
64
65 def test_create_all_calculations_queue(self):
66 def creator_function(x, y, queue):
67 return {"a": 2 * x, "b": y + 1, "queue": queue}
68
69 all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(creator_function,
70 x=[1, 2], y=[3, 4])
71
72 all_calculation_kwargs = list(all_calculation_kwargs)
73 all_queues = list(all_queues)
74 every_parameter_combination = list(every_parameter_combination)
75
76 self.assertEqual(len(all_calculation_kwargs), 4)
77 self.assertEqual(len(all_queues), 4)
78 self.assertEqual(len(every_parameter_combination), 4)
79
80 self.assertIn({"x": 1, "y": 3}, every_parameter_combination)
81 self.assertIn({"x": 1, "y": 4}, every_parameter_combination)
82 self.assertIn({"x": 2, "y": 3}, every_parameter_combination)
83 self.assertIn({"x": 2, "y": 4}, every_parameter_combination)
84
85 for combination in [{"x": 1, "y": 3}, {"x": 1, "y": 4}, {"x": 2, "y": 3}, {"x": 2, "y": 4}]:
86 index = every_parameter_combination.index(combination)
87 x = combination["x"]
88 y = combination["y"]
89 self.assertEqual(all_calculation_kwargs[index], {"a": 2 * x, "b": y + 1, "queue": all_queues[index]})
90
91 def test_create_all_calculations_none(self):
92 def creator_function(x, y):
93 if x > 1:
94 return None
95 return {"a": 2 * x, "b": y + 1}
96
97 all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(creator_function,
98 x=[1, 2], y=[3, 4])
99 all_calculation_kwargs = list(all_calculation_kwargs)
100 all_queues = list(all_queues)
101 every_parameter_combination = list(every_parameter_combination)
102
103 self.assertEqual(len(all_calculation_kwargs), 4)
104 self.assertEqual(len(all_queues), 4)
105 self.assertEqual(len(every_parameter_combination), 4)
106
107 self.assertIn({"a": 2, "b": 4}, all_calculation_kwargs)
108 self.assertIn({"a": 2, "b": 5}, all_calculation_kwargs)
109 self.assertNotIn({"a": 4, "b": 4}, all_calculation_kwargs)
110 self.assertNotIn({"a": 4, "b": 5}, all_calculation_kwargs)
111
112 for combination in [{"x": 1, "y": 3}, {"x": 1, "y": 4}, {"x": 2, "y": 3}, {"x": 2, "y": 4}]:
113 index = every_parameter_combination.index(combination)
114 x = combination["x"]
115 y = combination["y"]
116
117 if x == 1:
118 self.assertEqual(all_calculation_kwargs[index], {"a": 2 * x, "b": y + 1})
119 else:
120 self.assertEqual(all_calculation_kwargs[index], None)
121
122
123if __name__ == "__main__":
124 main()
125# @endcond
Definition: main.py:1