Belle II Software light-2406-ragdoll
test_calculation.py
1
8from unittest import TestCase, main
9from hep_ipython_tools.calculation import Calculation
10from hep_ipython_tools.calculation_process import CalculationProcess
11from hep_ipython_tools.calculation_queue import CalculationQueue
12
13# @cond internal_test
14
15
16class TestCalculation(TestCase):
17
18 def test_default(self):
19 calculation = Calculation()
20
21 self.assertEqual(calculation._calculation_process_type, CalculationProcess)
22 self.assertEqual(calculation.process_list, [])
23
24 calculations = Calculation([1, 2, 3])
25 self.assertEqual(calculations.process_list, [1, 2, 3])
26
27 def test_iter(self):
28 calculations = Calculation([0, 1, 2])
29
30 for i, calc in enumerate(calculations):
31 self.assertEqual(calc.process_list, [i])
32
33 self.assertEqual(len(calculations), 3)
34
35 def test_is_running(self):
36 calculations, process = self.create_calculations()
37
38 process.is_alive = lambda: True
39 self.assertEqual(calculations.is_running(), [False, True, False])
40
41 def test_is_finished(self):
42 calculations, process = self.create_calculations()
43
44 process.already_run = lambda: True
45 self.assertEqual(calculations.is_finished(), [False, True, False])
46
47 def test_map_on_process(self):
48 calculations, process = self.create_calculations()
49
50 process.is_alive = lambda: True
51
52 self.assertEqual(calculations.map_on_processes(lambda p: p.is_alive(), index=None), [False, True, False])
53 self.assertEqual(calculations.map_on_processes(lambda p: p.is_alive(), index=1), True)
54 self.assertEqual(calculations.map_on_processes(lambda p: p.is_alive(), index=0), False)
55 self.assertEqual(calculations.map_on_processes(lambda p: p.is_alive(), index=process), True)
56
57 def create_calculations(self):
58 process1 = CalculationProcess(CalculationQueue(), None, None)
59 process2 = CalculationProcess(CalculationQueue(), None, None)
60 process3 = CalculationProcess(CalculationQueue(), None, None)
61
62 calculations = Calculation([process1, process2, process3])
63
64 return calculations, process2
65
66
67if __name__ == "__main__":
68 main()
69# @endcond
Definition: main.py:1