8from unittest
import TestCase, main
16class TestCalculation(TestCase):
18 def test_default(self):
19 calculation = Calculation()
21 self.assertEqual(calculation._calculation_process_type, CalculationProcess)
22 self.assertEqual(calculation.process_list, [])
24 calculations = Calculation([1, 2, 3])
25 self.assertEqual(calculations.process_list, [1, 2, 3])
28 calculations = Calculation([0, 1, 2])
30 for i, calc
in enumerate(calculations):
31 self.assertEqual(calc.process_list, [i])
33 self.assertEqual(len(calculations), 3)
35 def test_is_running(self):
36 calculations, process = self.create_calculations()
38 process.is_alive =
lambda:
True
39 self.assertEqual(calculations.is_running(), [
False,
True,
False])
41 def test_is_finished(self):
42 calculations, process = self.create_calculations()
44 process.already_run =
lambda:
True
45 self.assertEqual(calculations.is_finished(), [
False,
True,
False])
47 def test_map_on_process(self):
48 calculations, process = self.create_calculations()
50 process.is_alive =
lambda:
True
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)
57 def create_calculations(self):
58 process1 = CalculationProcess(CalculationQueue(),
None,
None)
59 process2 = CalculationProcess(CalculationQueue(),
None,
None)
60 process3 = CalculationProcess(CalculationQueue(),
None,
None)
62 calculations = Calculation([process1, process2, process3])
64 return calculations, process2
67if __name__ ==
"__main__":