|
1
|
|
|
"""Module for testing optimizer.""" |
|
2
|
|
|
|
|
3
|
|
|
from unittest import TestCase |
|
4
|
|
|
from unittest.mock import patch, Mock, sentinel |
|
5
|
|
|
|
|
6
|
|
|
from grortir.main.model.core.abstract_process import AbstractProcess |
|
7
|
|
|
from grortir.main.model.core.abstract_stage import AbstractStage |
|
8
|
|
|
from grortir.main.model.core.optimization_status import OptimizationStatus |
|
9
|
|
|
from grortir.main.optimizers.optimizer import Optimizer |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestOptimizer(TestCase): |
|
13
|
|
|
"""Class for testing Optimizer.""" |
|
14
|
|
|
|
|
15
|
|
|
def setUp(self): |
|
16
|
|
|
"""Set up environment.""" |
|
17
|
|
|
self.some_process = AbstractProcess() |
|
18
|
|
|
self.first_stage = AbstractStage() |
|
19
|
|
|
self.second_stage = AbstractStage() |
|
20
|
|
|
self.some_process.add_path([self.first_stage, self.second_stage]) |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
def test___init__(self): |
|
23
|
|
|
"""Testing creating object.""" |
|
24
|
|
|
optimizer = Optimizer(self.some_process) |
|
25
|
|
|
self.assertIsNotNone(optimizer) |
|
26
|
|
|
self.assertEqual(optimizer.process, self.some_process) |
|
27
|
|
|
|
|
28
|
|
|
def test_set_order(self): |
|
29
|
|
|
"""Test changing order of optimizing.""" |
|
30
|
|
|
optimizer = Optimizer(self.some_process) |
|
31
|
|
|
optimizer.set_custom_optimizing_order( |
|
32
|
|
|
[self.second_stage, self.first_stage]) |
|
33
|
|
|
self.assertEqual(optimizer.ordered_stages[0], self.second_stage) |
|
34
|
|
|
|
|
35
|
|
|
def test_set_order_fail(self): |
|
36
|
|
|
"""Test changing order of optimizing with incorrect list.""" |
|
37
|
|
|
optimizer = Optimizer(self.some_process) |
|
38
|
|
|
with self.assertRaises(ValueError): |
|
39
|
|
|
optimizer.set_custom_optimizing_order( |
|
40
|
|
|
[self.second_stage, self.first_stage, self.first_stage]) |
|
41
|
|
|
|
|
42
|
|
|
@patch('grortir.main.optimizers.optimizer.pso') |
|
43
|
|
|
def test_optimize_process(self, pso_mock): |
|
44
|
|
|
"""Test optimization of initial stage in process.""" |
|
45
|
|
|
optimizer = Optimizer(self.some_process) |
|
46
|
|
|
result = optimizer.optimize_process() |
|
47
|
|
|
pso_mock.assert_called_once_with(self.first_stage) |
|
48
|
|
|
self.assertFalse(result) |
|
49
|
|
|
|
|
50
|
|
|
@patch('grortir.main.optimizers.optimizer.pso') |
|
51
|
|
|
def test_optimize_whole_process(self, pso_mock): |
|
52
|
|
|
"""Test whole optimization.""" |
|
53
|
|
|
optimizer = Optimizer(self.some_process) |
|
54
|
|
|
self.first_stage.optimization_status = OptimizationStatus.success |
|
55
|
|
|
self.first_stage.get_output_of_stage = Mock(return_value=sentinel) |
|
56
|
|
|
self.second_stage.optimization_status = OptimizationStatus.success |
|
57
|
|
|
result = optimizer.optimize_process() |
|
58
|
|
|
pso_mock.assert_called_with(self.second_stage) |
|
59
|
|
|
self.assertTrue(result) |
|
60
|
|
|
|
|
61
|
|
|
@patch('grortir.main.optimizers.optimizer.pso') |
|
62
|
|
|
def test_optimize_incorrect_process(self, pso_mock): |
|
63
|
|
|
"""Test incorrect process structure.""" |
|
64
|
|
|
optimizer = Optimizer(self.some_process) |
|
65
|
|
|
third_stage = AbstractStage() |
|
66
|
|
|
self.some_process.add_edge(third_stage, self.second_stage) |
|
|
|
|
|
|
67
|
|
|
self.first_stage.optimization_status = OptimizationStatus.success |
|
68
|
|
|
self.first_stage.get_output_of_stage = Mock(return_value=sentinel) |
|
69
|
|
|
pso_mock.assert_not_called() |
|
70
|
|
|
with self.assertRaises(AttributeError): |
|
71
|
|
|
optimizer.optimize_process() |
|
72
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.