Belle II Software  release-05-02-19
test_IPythonHandler.py
1 import os
2 from unittest import TestCase, main
3 
4 from hep_ipython_tools.calculation_process import CalculationProcess
5 from hep_ipython_tools.calculation import Calculation
6 from hep_ipython_tools.calculation_queue import CalculationQueue
7 from hep_ipython_tools.information import EnvironmentInformation
8 from hep_ipython_tools.ipython_handler import IPythonHandler
9 from hep_ipython_tools.tests.fixtures import MockQueue
10 
11 # @cond internal_test
12 
13 
14 class DeriviedCalculationProcess(CalculationProcess):
15 
16  def __init__(self, result_queue, log_file_name, parameters, some_variable, other_variable):
17  CalculationProcess.__init__(self, result_queue, log_file_name, parameters)
18 
19  self.some_variable = some_variable
20  self.other_variable = other_variable
21 
22  def start_process(self):
23  pass
24 
25 
26 class DerivedCalculation(Calculation):
27 
28  def __init__(self):
29  Calculation.__init__(self)
30  self._calculation_process_type = DeriviedCalculationProcess
31 
32 
33 class DerivedIPythonHandler(IPythonHandler):
34 
35  def __init__(self):
36  IPythonHandler.__init__(self)
37 
38  self._calculation_type = DerivedCalculation
39 
40 
41 class TestIPythonHandler(TestCase):
42 
43  def test_default(self):
44  handler = IPythonHandler()
45 
46  self.assertEqual(handler.log_files, [])
47  self.assertEqual(handler._calculation_type, Calculation)
48  self.assertIsInstance(handler.information, EnvironmentInformation)
49 
50  def test_create_queue(self):
51  queue = DerivedIPythonHandler.create_queue()
52 
53  self.assertIsInstance(queue, CalculationQueue)
54 
55  self.assertEqual(queue.get_keys(), [])
56  self.assertEqual(queue.results, {})
57 
58  def test_next_log_file(self):
59 
60  handler = DerivedIPythonHandler()
61 
62  self.assertEqual(handler.log_files, [])
63 
64  next_log_file = handler.next_log_file_name()
65 
66  try:
67  self.assertEqual(len(handler.log_files), 1)
68  self.assertEqual(handler.log_files[0][1], next_log_file)
69 
70  finally:
71  os.unlink(next_log_file)
72 
73  def test_many_next_log_files(self):
74  handler = DerivedIPythonHandler()
75 
76  self.assertEqual(handler.log_files, [])
77 
78  next_log_files = [handler.next_log_file_name() for _ in range(200)]
79 
80  try:
81  self.assertEqual(len(handler.log_files), 20)
82 
83  names = [log_file[1] for log_file in handler.log_files]
84  self.assertEqual(names, next_log_files[-20:])
85 
86  for closed_log_file in next_log_files[:-20]:
87  self.assertFalse(os.path.exists(closed_log_file))
88 
89  finally:
90  for log_file_name in next_log_files:
91  if os.path.exists(log_file_name):
92  os.unlink(log_file_name)
93 
94  def test_many_log_files_closed(self):
95  handler = DerivedIPythonHandler()
96 
97  self.assertEqual(handler.log_files, [])
98 
99  next_log_files = [handler.next_log_file_name() for _ in range(100)]
100 
101  for log_file_name in next_log_files:
102  if os.path.exists(log_file_name):
103  os.unlink(log_file_name)
104 
105  next_log_files = [handler.next_log_file_name() for _ in range(100)]
106 
107  try:
108  self.assertEqual(len(handler.log_files), 20)
109 
110  names = [log_file[1] for log_file in handler.log_files]
111  self.assertEqual(names, next_log_files[-20:])
112 
113  for closed_log_file in next_log_files[:-20]:
114  self.assertFalse(os.path.exists(closed_log_file))
115 
116  finally:
117  for log_file_name in next_log_files:
118  if os.path.exists(log_file_name):
119  os.unlink(log_file_name)
120 
121  def test_process_result_queue(self):
122  handler = DerivedIPythonHandler()
123 
124  queue = CalculationQueue()
125  queue.queue = MockQueue()
126 
127  queue.put("TestItem", "TestValue")
128 
129  calculation = handler.process(queue, some_variable=42, other_variable=21)
130 
131  self.assertEqual(len(calculation.process_list), 1)
132 
133  added_calculation = calculation.process_list[0]
134 
135  self.assertEqual(added_calculation.log_file_name, handler.log_files[0][1])
136  self.assertEqual(added_calculation.result_queue, queue)
137  self.assertEqual(added_calculation.result_queue.get_keys(), ["TestItem"])
138  self.assertEqual(added_calculation.result_queue.get("TestItem"), "TestValue")
139  self.assertEqual(added_calculation.parameters, None)
140  self.assertEqual(added_calculation.some_variable, 42)
141  self.assertEqual(added_calculation.other_variable, 21)
142 
143  self.assertEqual(calculation.get_keys(), ["TestItem"])
144  self.assertEqual(calculation.get("TestItem"), "TestValue")
145  self.assertEqual(calculation.get_parameters(), None)
146 
147  def test_process_no_result_queue(self):
148  handler = DerivedIPythonHandler()
149 
150  calculation = handler.process(some_variable=42, other_variable=21)
151 
152  self.assertEqual(len(calculation.process_list), 1)
153 
154  added_calculation = calculation.process_list[0]
155 
156  result_queue = added_calculation.result_queue
157 
158  self.assertIsInstance(result_queue, CalculationQueue)
159 
160  def test_parameter_space_no_queue(self):
161  handler = DerivedIPythonHandler()
162 
163  def creator_function(some_variable, other_variable):
164  return dict(some_variable=some_variable + 1, other_variable=other_variable + "c")
165 
166  calculations = handler.process_parameter_space(creator_function,
167  some_variable=[1, 2], other_variable=["a", "b"])
168 
169  self.assertEqual(len(calculations.process_list), 4)
170 
171  parameters = calculations.get_parameters()
172 
173  self.assertIn({"some_variable": 1, "other_variable": "a"}, parameters)
174  self.assertIn({"some_variable": 1, "other_variable": "b"}, parameters)
175  self.assertIn({"some_variable": 2, "other_variable": "a"}, parameters)
176  self.assertIn({"some_variable": 2, "other_variable": "b"}, parameters)
177 
178  def assert_is_in_processes(some_variable, other_variable):
179  for process in calculations.process_list:
180  if process.some_variable == some_variable and process.other_variable == other_variable:
181  return True
182 
183  self.fail()
184 
185  assert_is_in_processes(2, "ac")
186  assert_is_in_processes(3, "ac")
187  assert_is_in_processes(2, "bc")
188  assert_is_in_processes(3, "bc")
189 
190 if __name__ == "__main__":
191  main()
192 # @endcond
hep_ipython_tools.ipython_handler
Definition: ipython_handler.py:1
hep_ipython_tools.tests.fixtures
Definition: fixtures.py:1
hep_ipython_tools.calculation_process
Definition: calculation_process.py:1
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
hep_ipython_tools.calculation_queue
Definition: calculation_queue.py:1
TestCase
Definition: main.cc:1069
hep_ipython_tools.information
Definition: information.py:1
hep_ipython_tools.calculation
Definition: calculation.py:1