Completed
Pull Request — master (#49)
by Wojtek
10:46
created

TestGroupedOptimizer.test_calling_functions()   B

Complexity

Conditions 1

Size

Total Lines 181

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 181
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
"""This test is checking that algortithm with grouping is working correctly."""
2
3
import unittest.mock as mock
4
from unittest import TestCase
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
from grortir.main.optimizers.grouping_strategy import GroupingStrategy
10
from grortir.main.pso.calls_optimization_strategy import \
11
    CallsOptimizationStrategy
12
from grortir.main.pso.pso_algorithm import PsoAlgorithm
13
14
15
class TestGroupedOptimizer(TestCase):
16
    """Class for testing Optimizer."""
17
18
    def setUp(self):
19
        """Set up environment."""
20
        self.some_process = AbstractProcess()
21
        self.first_stage = AbstractStage()
22
        self.second_stage = AbstractStage()
23
        self.third_stage_a = mock.Mock()
24
        self.third_stage_b = mock.Mock()
25
        self.some_process.add_path([self.first_stage, self.second_stage,
0 ignored issues
show
Bug introduced by
The Instance of AbstractProcess does not seem to have a member named add_path.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
26
                                    self.third_stage_a])
27
        self.some_process.add_edge(self.second_stage, self.third_stage_b)
0 ignored issues
show
Bug introduced by
The Instance of AbstractProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
28
29
    def test___init__(self):
30
        """Testing creating object."""
31
        optimizer = GroupedOptimizer(self.some_process, mock.Mock(),
32
                                     mock.Mock())
33
        self.assertIsNotNone(optimizer)
34
        self.assertEqual(optimizer.process, self.some_process)
35
36
    def test_calling_functions(self):
37
        """Test correct order of calling function."""
38
        optimizer = GroupedOptimizer(TESTED_PROCESS, GROUPING_STRATEGY,
39
                                     PSO_ALGORITHM)
40
        expected_calls = [mock.call.method_name('0', '__init__'),
41
                          mock.call.method_name('1', '__init__'),
42
                          mock.call.method_name('2', '__init__'),
43
                          mock.call.method_name('3', '__init__'),
44
                          mock.call.method_name('4', '__init__'),
45
                          mock.call.method_name('5', '__init__'),
46
                          mock.call.method_name('6', '__init__'),
47
                          mock.call.method_name('0', 'get_output_of_stage', 1),
48
                          mock.call.method_name('1', 'get_output_of_stage', 1),
49
                          mock.call.method_name('2', 'get_output_of_stage', 1),
50
                          mock.call.method_name('0', 'get_quality', 1),
51
                          mock.call.method_name('1', 'get_quality', 1),
52
                          mock.call.method_name('2', 'get_quality', 1),
53
                          mock.call.method_name('0', 'is_enough_quality', 1),
54
                          mock.call.method_name('0', 'could_be_optimized', 1),
55
                          mock.call.method_name('1', 'is_enough_quality', 1),
56
                          mock.call.method_name('1', 'could_be_optimized', 1),
57
                          mock.call.method_name('2', 'is_enough_quality', 1),
58
                          mock.call.method_name('2', 'could_be_optimized', 1),
59
                          mock.call.method_name('0', 'get_output_of_stage', 2),
60
                          mock.call.method_name('1', 'get_output_of_stage', 2),
61
                          mock.call.method_name('2', 'get_output_of_stage', 2),
62
                          mock.call.method_name('0', 'get_quality', 2),
63
                          mock.call.method_name('1', 'get_quality', 2),
64
                          mock.call.method_name('2', 'get_quality', 2),
65
                          mock.call.method_name('0', 'is_enough_quality', 2),
66
                          mock.call.method_name('0', 'could_be_optimized', 2),
67
                          mock.call.method_name('1', 'is_enough_quality', 2),
68
                          mock.call.method_name('1', 'could_be_optimized', 2),
69
                          mock.call.method_name('2', 'is_enough_quality', 2),
70
                          mock.call.method_name('2', 'could_be_optimized', 2),
71
                          mock.call.method_name('0', 'get_output_of_stage', 3),
72
                          mock.call.method_name('1', 'get_output_of_stage', 3),
73
                          mock.call.method_name('2', 'get_output_of_stage', 3),
74
                          mock.call.method_name('0', 'get_quality', 3),
75
                          mock.call.method_name('1', 'get_quality', 3),
76
                          mock.call.method_name('2', 'get_quality', 3),
77
                          mock.call.method_name('0', 'is_enough_quality', 3),
78
                          mock.call.method_name('0', 'could_be_optimized', 3),
79
                          mock.call.method_name('1', 'is_enough_quality', 3),
80
                          mock.call.method_name('1', 'could_be_optimized', 3),
81
                          mock.call.method_name('2', 'is_enough_quality', 3),
82
                          mock.call.method_name('2', 'could_be_optimized', 3),
83
                          mock.call.method_name('0', 'get_output_of_stage', 4),
84
                          mock.call.method_name('1', 'get_output_of_stage', 4),
85
                          mock.call.method_name('2', 'get_output_of_stage', 4),
86
                          mock.call.method_name('0', 'get_quality', 4),
87
                          mock.call.method_name('1', 'get_quality', 4),
88
                          mock.call.method_name('2', 'get_quality', 4),
89
                          mock.call.method_name('0', 'is_enough_quality', 4),
90
                          mock.call.method_name('0', 'could_be_optimized', 4),
91
                          mock.call.method_name('1', 'is_enough_quality', 4),
92
                          mock.call.method_name('1', 'could_be_optimized', 4),
93
                          mock.call.method_name('2', 'is_enough_quality', 4),
94
                          mock.call.method_name('2', 'could_be_optimized', 4),
95
                          mock.call.method_name('0', 'get_output_of_stage', 5),
96
                          mock.call.method_name('1', 'get_output_of_stage', 5),
97
                          mock.call.method_name('2', 'get_output_of_stage', 5),
98
                          mock.call.method_name('0', 'get_quality', 5),
99
                          mock.call.method_name('1', 'get_quality', 5),
100
                          mock.call.method_name('2', 'get_quality', 5),
101
                          mock.call.method_name('0', 'is_enough_quality', 5),
102
                          mock.call.method_name('0', 'could_be_optimized', 5),
103
                          mock.call.method_name('1', 'is_enough_quality', 5),
104
                          mock.call.method_name('1', 'could_be_optimized', 5),
105
                          mock.call.method_name('2', 'is_enough_quality', 5),
106
                          mock.call.method_name('2', 'could_be_optimized', 5),
107
                          mock.call.method_name('0', 'get_output_of_stage', 6),
108
                          mock.call.method_name('1', 'get_output_of_stage', 6),
109
                          mock.call.method_name('2', 'get_output_of_stage', 6),
110
                          mock.call.method_name('0', 'get_quality', 6),
111
                          mock.call.method_name('1', 'get_quality', 6),
112
                          mock.call.method_name('2', 'get_quality', 6),
113
                          mock.call.method_name('0', 'is_enough_quality', 6),
114
                          mock.call.method_name('0', 'could_be_optimized', 6),
115
                          mock.call.method_name('1', 'is_enough_quality', 6),
116
                          mock.call.method_name('1', 'could_be_optimized', 6),
117
                          mock.call.method_name('2', 'is_enough_quality', 6),
118
                          mock.call.method_name('2', 'could_be_optimized', 6),
119
                          mock.call.method_name('3', 'get_output_of_stage', 1),
120
                          mock.call.method_name('4', 'get_output_of_stage', 1),
121
                          mock.call.method_name('5', 'get_output_of_stage', 1),
122
                          mock.call.method_name('3', 'get_quality', 1),
123
                          mock.call.method_name('4', 'get_quality', 1),
124
                          mock.call.method_name('5', 'get_quality', 1),
125
                          mock.call.method_name('3', 'is_enough_quality', 1),
126
                          mock.call.method_name('3', 'could_be_optimized', 1),
127
                          mock.call.method_name('4', 'is_enough_quality', 1),
128
                          mock.call.method_name('4', 'could_be_optimized', 1),
129
                          mock.call.method_name('5', 'is_enough_quality', 1),
130
                          mock.call.method_name('5', 'could_be_optimized', 1),
131
                          mock.call.method_name('3', 'get_output_of_stage', 2),
132
                          mock.call.method_name('4', 'get_output_of_stage', 2),
133
                          mock.call.method_name('5', 'get_output_of_stage', 2),
134
                          mock.call.method_name('3', 'get_quality', 2),
135
                          mock.call.method_name('4', 'get_quality', 2),
136
                          mock.call.method_name('5', 'get_quality', 2),
137
                          mock.call.method_name('3', 'is_enough_quality', 2),
138
                          mock.call.method_name('3', 'could_be_optimized', 2),
139
                          mock.call.method_name('4', 'is_enough_quality', 2),
140
                          mock.call.method_name('4', 'could_be_optimized', 2),
141
                          mock.call.method_name('5', 'is_enough_quality', 2),
142
                          mock.call.method_name('5', 'could_be_optimized', 2),
143
                          mock.call.method_name('3', 'get_output_of_stage', 3),
144
                          mock.call.method_name('4', 'get_output_of_stage', 3),
145
                          mock.call.method_name('5', 'get_output_of_stage', 3),
146
                          mock.call.method_name('3', 'get_quality', 3),
147
                          mock.call.method_name('4', 'get_quality', 3),
148
                          mock.call.method_name('5', 'get_quality', 3),
149
                          mock.call.method_name('3', 'is_enough_quality', 3),
150
                          mock.call.method_name('3', 'could_be_optimized', 3),
151
                          mock.call.method_name('4', 'is_enough_quality', 3),
152
                          mock.call.method_name('4', 'could_be_optimized', 3),
153
                          mock.call.method_name('5', 'is_enough_quality', 3),
154
                          mock.call.method_name('5', 'could_be_optimized', 3),
155
                          mock.call.method_name('3', 'get_output_of_stage', 4),
156
                          mock.call.method_name('4', 'get_output_of_stage', 4),
157
                          mock.call.method_name('5', 'get_output_of_stage', 4),
158
                          mock.call.method_name('3', 'get_quality', 4),
159
                          mock.call.method_name('4', 'get_quality', 4),
160
                          mock.call.method_name('5', 'get_quality', 4),
161
                          mock.call.method_name('3', 'is_enough_quality', 4),
162
                          mock.call.method_name('3', 'could_be_optimized', 4),
163
                          mock.call.method_name('4', 'is_enough_quality', 4),
164
                          mock.call.method_name('4', 'could_be_optimized', 4),
165
                          mock.call.method_name('5', 'is_enough_quality', 4),
166
                          mock.call.method_name('5', 'could_be_optimized', 4),
167
                          mock.call.method_name('3', 'get_output_of_stage', 5),
168
                          mock.call.method_name('4', 'get_output_of_stage', 5),
169
                          mock.call.method_name('5', 'get_output_of_stage', 5),
170
                          mock.call.method_name('3', 'get_quality', 5),
171
                          mock.call.method_name('4', 'get_quality', 5),
172
                          mock.call.method_name('5', 'get_quality', 5),
173
                          mock.call.method_name('3', 'is_enough_quality', 5),
174
                          mock.call.method_name('3', 'could_be_optimized', 5),
175
                          mock.call.method_name('4', 'is_enough_quality', 5),
176
                          mock.call.method_name('4', 'could_be_optimized', 5),
177
                          mock.call.method_name('5', 'is_enough_quality', 5),
178
                          mock.call.method_name('5', 'could_be_optimized', 5),
179
                          mock.call.method_name('3', 'get_output_of_stage', 6),
180
                          mock.call.method_name('4', 'get_output_of_stage', 6),
181
                          mock.call.method_name('5', 'get_output_of_stage', 6),
182
                          mock.call.method_name('3', 'get_quality', 6),
183
                          mock.call.method_name('4', 'get_quality', 6),
184
                          mock.call.method_name('5', 'get_quality', 6),
185
                          mock.call.method_name('3', 'is_enough_quality', 6),
186
                          mock.call.method_name('3', 'could_be_optimized', 6),
187
                          mock.call.method_name('4', 'is_enough_quality', 6),
188
                          mock.call.method_name('4', 'could_be_optimized', 6),
189
                          mock.call.method_name('5', 'is_enough_quality', 6),
190
                          mock.call.method_name('5', 'could_be_optimized', 6),
191
                          mock.call.method_name('6', 'get_output_of_stage', 1),
192
                          mock.call.method_name('6', 'get_quality', 1),
193
                          mock.call.method_name('6', 'is_enough_quality', 1),
194
                          mock.call.method_name('6', 'could_be_optimized', 1),
195
                          mock.call.method_name('6', 'get_output_of_stage', 2),
196
                          mock.call.method_name('6', 'get_quality', 2),
197
                          mock.call.method_name('6', 'is_enough_quality', 2),
198
                          mock.call.method_name('6', 'could_be_optimized', 2),
199
                          mock.call.method_name('6', 'get_output_of_stage', 3),
200
                          mock.call.method_name('6', 'get_quality', 3),
201
                          mock.call.method_name('6', 'is_enough_quality', 3),
202
                          mock.call.method_name('6', 'could_be_optimized', 3),
203
                          mock.call.method_name('6', 'get_output_of_stage', 4),
204
                          mock.call.method_name('6', 'get_quality', 4),
205
                          mock.call.method_name('6', 'is_enough_quality', 4),
206
                          mock.call.method_name('6', 'could_be_optimized', 4),
207
                          mock.call.method_name('6', 'get_output_of_stage', 5),
208
                          mock.call.method_name('6', 'get_quality', 5),
209
                          mock.call.method_name('6', 'is_enough_quality', 5),
210
                          mock.call.method_name('6', 'could_be_optimized', 5),
211
                          mock.call.method_name('6', 'get_output_of_stage', 6),
212
                          mock.call.method_name('6', 'get_quality', 6),
213
                          mock.call.method_name('6', 'is_enough_quality', 6),
214
                          mock.call.method_name('6', 'could_be_optimized', 6)]
215
        optimizer.optimize_process()
216
        self.assertListEqual(CALLS_COUNTER.method_calls, expected_calls)
217
218
219
CALLS_COUNTER = mock.Mock(name="CallsCounter")
220
221
222
class DeterministicStage(AbstractStage):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Bug introduced by
The method get_maximal_acceptable_cost which was declared abstract in the super-class AbstractStage
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
best-practice introduced by
Too many instance attributes (11/7)
Loading history...
223
    def __init__(self, name, max_get_output_of_stage_count=10,
0 ignored issues
show
best-practice introduced by
Too many arguments (7/5)
Loading history...
224
                 max_is_enough_quality_count=10,
225
                 max_could_be_optimized_count=10, max_get_quality_count=10,
226
                 max_get_cost_count=10):
227
        super().__init__((0, 0, 0, 0))
228
        self.name = name
229
        CALLS_COUNTER.method_name(self.name, "__init__")
230
        self.get_cost_count = 0
231
        self.get_quality_count = 0
232
        self.could_be_optimized_count = 0
233
        self.is_enough_quality_count = 0
234
        self.get_output_of_stage_count = 0
235
        self.max_get_cost_count = max_get_cost_count
236
        self.max_get_quality_count = max_get_quality_count
237
        self.max_could_be_optimized_count = max_could_be_optimized_count
238
        self.max_is_enough_quality_count = max_is_enough_quality_count
239
        self.max_get_output_of_stage_count = max_get_output_of_stage_count
240
241
    def get_cost(self):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'get_cost' method
Loading history...
242
        self.get_cost_count += 1
243
        CALLS_COUNTER.method_name(self.name, "get_cost", self.get_cost_count)
244
        return self.get_cost_count
245
246
    def get_quality(self, control_params=None):
0 ignored issues
show
Bug Best Practice introduced by
Signature differs from overridden 'get_quality' method

It is generally a good practice to use signatures that are compatible with the Liskov substitution principle.

This allows to pass instances of the child class anywhere where the instances of the super-class/interface would be acceptable.

Loading history...
247
        self.get_quality_count += 1
248
        CALLS_COUNTER.method_name(self.name, "get_quality",
249
                                  self.get_quality_count)
250
        return self.get_quality_count
251
252
    def could_be_optimized(self):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'could_be_optimized' method
Loading history...
253
        self.could_be_optimized_count += 1
254
        CALLS_COUNTER.method_name(self.name, "could_be_optimized",
255
                                  self.could_be_optimized_count)
256
        return self.is_enough_quality_count < 8
257
258
    def is_enough_quality(self, value):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'is_enough_quality' method
Loading history...
259
        self.is_enough_quality_count += 1
260
        CALLS_COUNTER.method_name(self.name, "is_enough_quality",
261
                                  self.is_enough_quality_count)
262
        return self.is_enough_quality_count >= 5
263
264
    def get_output_of_stage(self):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'get_output_of_stage' method
Loading history...
265
        self.get_output_of_stage_count += 1
266
        output = [self.get_output_of_stage_count] * len(self.input_vector)
267
        output[0] = int(self.name)
268
        CALLS_COUNTER.method_name(self.name, "get_output_of_stage",
269
                                  self.get_output_of_stage_count)
270
        return output
271
272
273
class ExampleProcess(AbstractProcess):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
274
    pass
275
276
277
TESTED_PROCESS = ExampleProcess()
278
stages = {}
0 ignored issues
show
Coding Style Naming introduced by
The name stages does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
279
for i in range(7):
280
    stages[i] = DeterministicStage(str(i))
281
282
# Our graph:
283
#   0
284
#   |
285
#   1
286
#   |\
287
#   2 4
288
#   | |\
289
#   3 5 6
290
# All adges directed to down
291
# Order of nodes is the same as names
292
TESTED_PROCESS.add_edge(stages[0], stages[1])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
293
TESTED_PROCESS.add_edge(stages[1], stages[2])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
294
TESTED_PROCESS.add_edge(stages[2], stages[3])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
295
296
TESTED_PROCESS.add_edge(stages[1], stages[4])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
297
TESTED_PROCESS.add_edge(stages[4], stages[5])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
298
TESTED_PROCESS.add_edge(stages[4], stages[6])
0 ignored issues
show
Bug introduced by
The Instance of ExampleProcess does not seem to have a member named add_edge.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
299
300
# Groups:
301
#   (0)0
302
#      |
303
#   (0)1
304
#      |\
305
#   (0)2 4(1)
306
#      | | \
307
#   (1)3 5(1)6(2)
308
309
GROUPING_STRATEGY = GroupingStrategy(list(stages.values()))
310
GROUPING_STRATEGY.define_group((stages[0], stages[1], stages[2]))
311
GROUPING_STRATEGY.define_group((stages[3], stages[4], stages[5]))
312
GROUPING_STRATEGY.define_group((stages[6],))
313
314
OPTIMIZATION_STARTEGY = CallsOptimizationStrategy()
315
316
PSO_ALGORITHM = PsoAlgorithm(TESTED_PROCESS, GROUPING_STRATEGY,
317
                             OPTIMIZATION_STARTEGY, 2)
318