1
|
|
|
"""Module for testing grouped optimizer.""" |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import Mock |
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.optimizers.grouped_optimizer import GroupedOptimizer |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class TestGroupedOptimizer(TestCase): |
12
|
|
|
"""Class for testing Optimizer.""" |
13
|
|
|
|
14
|
|
|
def setUp(self): |
15
|
|
|
"""Set up environment.""" |
16
|
|
|
self.some_process = AbstractProcess() |
17
|
|
|
self.first_stage = AbstractStage() |
18
|
|
|
self.second_stage = AbstractStage() |
19
|
|
|
self.third_stage_a = Mock() |
20
|
|
|
self.third_stage_b = Mock() |
21
|
|
|
self.some_process.add_path([self.first_stage, self.second_stage, |
|
|
|
|
22
|
|
|
self.third_stage_a]) |
23
|
|
|
self.some_process.add_edge(self.second_stage, self.third_stage_b) |
|
|
|
|
24
|
|
|
|
25
|
|
|
def test___init__(self): |
26
|
|
|
"""Testing creating object.""" |
27
|
|
|
optimizer = GroupedOptimizer(self.some_process, Mock()) |
28
|
|
|
self.assertIsNotNone(optimizer) |
29
|
|
|
self.assertEqual(optimizer.process, self.some_process) |
30
|
|
|
|
31
|
|
|
def test_calling_functions(self): |
32
|
|
|
"""Test correct order of calling function.""" |
33
|
|
|
optimizer = GroupedOptimizer(self.some_process, Mock()) |
34
|
|
|
optimizer.optimize_process() |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class DeterministicStage(AbstractStage): |
|
|
|
|
38
|
|
|
def __init__(self, max_get_output_of_stage_count=10, |
|
|
|
|
39
|
|
|
max_is_enough_quality_count=10, |
40
|
|
|
max_could_be_optimized_count=10, max_get_quality_count=10, |
41
|
|
|
max_get_cost_count=10): |
42
|
|
|
super().__init__((0, 0, 0, 0)) |
43
|
|
|
self.get_cost_count = 0 |
44
|
|
|
self.get_quality_count = 0 |
45
|
|
|
self.could_be_optimized_count = 0 |
46
|
|
|
self.is_enough_quality_count = 0 |
47
|
|
|
self.get_output_of_stage_count = 0 |
48
|
|
|
self.max_get_cost_count = max_get_cost_count |
49
|
|
|
self.max_get_quality_count = max_get_quality_count |
50
|
|
|
self.max_could_be_optimized_count = max_could_be_optimized_count |
51
|
|
|
self.max_is_enough_quality_count = max_is_enough_quality_count |
52
|
|
|
self.max_get_output_of_stage_count = max_get_output_of_stage_count |
53
|
|
|
|
54
|
|
|
def get_cost(self): |
|
|
|
|
55
|
|
|
self.get_cost_count += 1 |
56
|
|
|
|
57
|
|
|
def get_quality(self, control_params=None): |
|
|
|
|
58
|
|
|
self.get_quality_count += 1 |
59
|
|
|
return |
60
|
|
|
|
61
|
|
|
def could_be_optimized(self): |
|
|
|
|
62
|
|
|
self.could_be_optimized_count += 1 |
63
|
|
|
return True |
64
|
|
|
|
65
|
|
|
def is_enough_quality(self, value): |
|
|
|
|
66
|
|
|
self.is_enough_quality_count += 1 |
67
|
|
|
return self.is_enough_quality_count == value |
68
|
|
|
|
69
|
|
|
def get_output_of_stage(self): |
|
|
|
|
70
|
|
|
self.get_output_of_stage_count += 1 |
71
|
|
|
return [self.get_output_of_stage_count] * len(self.input_vector) |
72
|
|
|
|
73
|
|
|
|
This check looks for calls to members that are non-existent. These calls will fail.
The member could have been renamed or removed.