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