9from unittest
import TestCase, main
21class DeriviedCalculationProcess(CalculationProcess):
23 def __init__(self, result_queue, log_file_name, parameters, some_variable, other_variable):
24 CalculationProcess.__init__(self, result_queue, log_file_name, parameters)
26 self.some_variable = some_variable
27 self.other_variable = other_variable
29 def start_process(self):
33class DerivedCalculation(Calculation):
36 Calculation.__init__(self)
37 self._calculation_process_type = DeriviedCalculationProcess
40class DerivedIPythonHandler(IPythonHandler):
43 IPythonHandler.__init__(self)
45 self._calculation_type = DerivedCalculation
48class TestIPythonHandler(TestCase):
50 def test_default(self):
51 handler = IPythonHandler()
53 self.assertEqual(handler.log_files, [])
54 self.assertEqual(handler._calculation_type, Calculation)
55 self.assertIsInstance(handler.information, EnvironmentInformation)
57 def test_create_queue(self):
58 queue = DerivedIPythonHandler.create_queue()
60 self.assertIsInstance(queue, CalculationQueue)
62 self.assertEqual(queue.get_keys(), [])
63 self.assertEqual(queue.results, {})
65 def test_next_log_file(self):
67 handler = DerivedIPythonHandler()
69 self.assertEqual(handler.log_files, [])
71 next_log_file = handler.next_log_file_name()
74 self.assertEqual(len(handler.log_files), 1)
75 self.assertEqual(handler.log_files[0][1], next_log_file)
78 os.unlink(next_log_file)
80 def test_many_next_log_files(self):
81 handler = DerivedIPythonHandler()
83 self.assertEqual(handler.log_files, [])
85 next_log_files = [handler.next_log_file_name()
for _
in range(200)]
88 self.assertEqual(len(handler.log_files), 20)
90 names = [log_file[1]
for log_file
in handler.log_files]
91 self.assertEqual(names, next_log_files[-20:])
93 for closed_log_file
in next_log_files[:-20]:
94 self.assertFalse(os.path.exists(closed_log_file))
97 for log_file_name
in next_log_files:
98 if os.path.exists(log_file_name):
99 os.unlink(log_file_name)
101 def test_many_log_files_closed(self):
102 handler = DerivedIPythonHandler()
104 self.assertEqual(handler.log_files, [])
106 next_log_files = [handler.next_log_file_name()
for _
in range(100)]
108 for log_file_name
in next_log_files:
109 if os.path.exists(log_file_name):
110 os.unlink(log_file_name)
112 next_log_files = [handler.next_log_file_name()
for _
in range(100)]
115 self.assertEqual(len(handler.log_files), 20)
117 names = [log_file[1]
for log_file
in handler.log_files]
118 self.assertEqual(names, next_log_files[-20:])
120 for closed_log_file
in next_log_files[:-20]:
121 self.assertFalse(os.path.exists(closed_log_file))
124 for log_file_name
in next_log_files:
125 if os.path.exists(log_file_name):
126 os.unlink(log_file_name)
128 def test_process_result_queue(self):
129 handler = DerivedIPythonHandler()
131 queue = CalculationQueue()
132 queue.queue = MockQueue()
134 queue.put(
"TestItem",
"TestValue")
136 calculation = handler.process(queue, some_variable=42, other_variable=21)
138 self.assertEqual(len(calculation.process_list), 1)
140 added_calculation = calculation.process_list[0]
142 self.assertEqual(added_calculation.log_file_name, handler.log_files[0][1])
143 self.assertEqual(added_calculation.result_queue, queue)
144 self.assertEqual(added_calculation.result_queue.get_keys(), [
"TestItem"])
145 self.assertEqual(added_calculation.result_queue.get(
"TestItem"),
"TestValue")
146 self.assertEqual(added_calculation.parameters,
None)
147 self.assertEqual(added_calculation.some_variable, 42)
148 self.assertEqual(added_calculation.other_variable, 21)
150 self.assertEqual(calculation.get_keys(), [
"TestItem"])
151 self.assertEqual(calculation.get(
"TestItem"),
"TestValue")
152 self.assertEqual(calculation.get_parameters(),
None)
154 def test_process_no_result_queue(self):
155 handler = DerivedIPythonHandler()
157 calculation = handler.process(some_variable=42, other_variable=21)
159 self.assertEqual(len(calculation.process_list), 1)
161 added_calculation = calculation.process_list[0]
163 result_queue = added_calculation.result_queue
165 self.assertIsInstance(result_queue, CalculationQueue)
167 def test_parameter_space_no_queue(self):
168 handler = DerivedIPythonHandler()
170 def creator_function(some_variable, other_variable):
171 return dict(some_variable=some_variable + 1, other_variable=other_variable +
"c")
173 calculations = handler.process_parameter_space(creator_function,
174 some_variable=[1, 2], other_variable=[
"a",
"b"])
176 self.assertEqual(len(calculations.process_list), 4)
178 parameters = calculations.get_parameters()
180 self.assertIn({
"some_variable": 1,
"other_variable":
"a"}, parameters)
181 self.assertIn({
"some_variable": 1,
"other_variable":
"b"}, parameters)
182 self.assertIn({
"some_variable": 2,
"other_variable":
"a"}, parameters)
183 self.assertIn({
"some_variable": 2,
"other_variable":
"b"}, parameters)
185 def assert_is_in_processes(some_variable, other_variable):
186 for process
in calculations.process_list:
187 if process.some_variable == some_variable
and process.other_variable == other_variable:
192 assert_is_in_processes(2,
"ac")
193 assert_is_in_processes(3,
"ac")
194 assert_is_in_processes(2,
"bc")
195 assert_is_in_processes(3,
"bc")
198if __name__ ==
"__main__":