10from unittest
import TestCase, main
14from tempfile
import NamedTemporaryFile
19class DerivedCalculationProcess(CalculationProcess):
21 def start_process(self, **kwargs):
22 self.result_queue.put(
"TestItem",
"TestValue")
23 self.result_queue.put(
"AnotherTestItem",
"AnotherTestValue")
26class OngoingCalculationProcess(CalculationProcess):
28 def start_process(self, **kwargs):
33class LoggingCalculationProcess(CalculationProcess):
35 def start_process(self, **kwargs):
36 with open(self.log_file_name,
"w")
as f:
37 f.write(
"Log File Content")
40class TestCalculationProcess(TestCase):
42 def get_terminated_process(self):
43 queue = CalculationQueue()
44 process = DerivedCalculationProcess(result_queue=queue, log_file_name=
None, parameters=
None)
49 def test_invalid_result_queue(self):
50 self.assertRaises(ValueError, DerivedCalculationProcess, result_queue=
None,
51 log_file_name=
None, parameters=
None)
53 def test_default_parameters(self):
54 queue = CalculationQueue()
55 process = DerivedCalculationProcess(result_queue=queue, log_file_name=
None, parameters=
None)
57 self.assertFalse(process.is_alive())
58 self.assertTrue(process.is_valid)
60 def test_get_keys(self):
61 process = self.get_terminated_process()
63 keys = process.get_keys()
65 self.assertIn(
"TestItem", keys)
66 self.assertIn(
"AnotherTestItem", keys)
69 process = self.get_terminated_process()
71 self.assertEqual(process.get(
"TestItem"),
"TestValue")
72 self.assertEqual(process.get(
"AnotherTestItem"),
"AnotherTestValue")
74 self.assertRaises(KeyError, process.get,
"UnknownItem")
76 def test_isAlive(self):
77 process = self.get_terminated_process()
79 self.assertFalse(process.is_alive())
82class TestLoggingCalculationProcess(TestCase):
85 queue = CalculationQueue()
87 self.tmp_file_name = NamedTemporaryFile(delete=
False).name
89 self.process = LoggingCalculationProcess(result_queue=queue, log_file_name=self.tmp_file_name,
96 self.process.terminate()
98 if os.path.exists(self.tmp_file_name):
99 os.unlink(self.tmp_file_name)
101 def test_get_log(self):
102 log_file_content = self.process.get_log()
103 self.assertEqual(log_file_content,
"Log File Content")
105 def test_save_log(self):
106 self.assertIsNone(self.process.log_file_content)
107 self.assertEqual(self.tmp_file_name, self.process.log_file_name)
109 self.process.save_log()
111 self.assertEqual(self.process.log_file_content,
"Log File Content")
112 self.assertIsNone(self.process.log_file_name)
114 self.assertFalse(os.path.exists(self.tmp_file_name))
116 def test_log_already_there(self):
117 self.process.log_file_content =
"Already there"
118 self.process.save_log()
120 self.assertEqual(self.process.log_file_content,
"Already there")
123class TestOngoingCalculationProcess(TestCase):
126 queue = CalculationQueue()
127 self.process = OngoingCalculationProcess(result_queue=queue, log_file_name=
None, parameters=
None)
130 self.process.terminate()
135 self.assertEqual(self.process.get_keys(),
None)
136 self.assertEqual(self.process.get(
"TestItem"),
None)
139class TestOngoingLoggingCalculationProcess(TestCase):
142 self.tmp_file_name = NamedTemporaryFile(delete=
False).name
144 queue = CalculationQueue()
145 self.process = OngoingCalculationProcess(result_queue=queue, log_file_name=self.tmp_file_name,
149 self.process.terminate()
151 if os.path.exists(self.tmp_file_name):
152 os.unlink(self.tmp_file_name)
154 def test_get_log(self):
157 self.assertEqual(self.process.get_log(),
"")
160if __name__ ==
"__main__":