1
|
|
|
"""Test class for module calls_stage.""" |
2
|
|
|
|
3
|
|
|
from unittest import TestCase |
4
|
|
|
from unittest.mock import Mock, sentinel |
5
|
|
|
|
6
|
|
|
from grortir.main.model.stages.calls_stage import CallsStage |
7
|
|
|
|
8
|
|
|
MAX_CALLS = 100 |
9
|
|
|
CONTRL_PARAMS = [1, 1, 1, 1, 1, 1.5] |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class TestCallsStage(TestCase): |
13
|
|
|
"""Test class for CallsStage.""" |
14
|
|
|
|
15
|
|
|
def test_get_quality(self): |
16
|
|
|
"""Test for get_quality method.""" |
17
|
|
|
tested_object = Mock() |
18
|
|
|
tested_object.cost = 7 |
19
|
|
|
tested_object.calculate_quality.return_value = sentinel.quality |
20
|
|
|
tested_object.control_params = CONTRL_PARAMS |
21
|
|
|
result = CallsStage.get_quality(tested_object, CONTRL_PARAMS) |
22
|
|
|
self.assertEqual(tested_object.cost, 8) |
23
|
|
|
tested_object.calculate_quality.assert_called_with(CONTRL_PARAMS, |
24
|
|
|
CONTRL_PARAMS) |
25
|
|
|
self.assertEqual(result, sentinel.quality) |
26
|
|
|
|
27
|
|
|
def test_get_quality_without_vector(self): |
28
|
|
|
"""Test for get_quality method when no arguments passed.""" |
29
|
|
|
tested_object = Mock() |
30
|
|
|
tested_object.cost = 9 |
31
|
|
|
tested_object.calculate_quality.return_value = sentinel.quality |
32
|
|
|
tested_object.control_params = CONTRL_PARAMS |
33
|
|
|
CallsStage.get_quality(tested_object) |
34
|
|
|
self.assertEqual(tested_object.cost, 10) |
35
|
|
|
tested_object.calculate_quality.assert_called_with(None, CONTRL_PARAMS) |
36
|
|
|
|
37
|
|
|
def test_get_cost(self): |
38
|
|
|
"""Test for get_cost method.""" |
39
|
|
|
tested_object = Mock() |
40
|
|
|
tested_object.cost = sentinel.cost |
41
|
|
|
result = CallsStage.get_cost(tested_object) |
42
|
|
|
self.assertEqual(result, sentinel.cost) |
43
|
|
|
|
44
|
|
|
def test_calculate_quality_ex(self): |
45
|
|
|
"""Test case when control are wrong.""" |
46
|
|
|
input_vector = (2, 3, 4, 5, 6) |
47
|
|
|
tested_object = CallsStage('name', MAX_CALLS, input_vector) |
48
|
|
|
tested_object.control_params = [2, 2] |
49
|
|
|
with self.assertRaises(AssertionError): |
50
|
|
|
tested_object.calculate_quality(input_vector, |
51
|
|
|
tested_object.control_params) |
52
|
|
|
|
53
|
|
|
def test_calculate_quality_ok(self): |
54
|
|
|
"""Test case when control params and input are okay.""" |
55
|
|
|
input_vector = (2, 3, 4, 5, 6, 1) |
56
|
|
|
tested_object = CallsStage('name', MAX_CALLS, input_vector) |
57
|
|
|
tested_object.control_params = CONTRL_PARAMS |
58
|
|
|
result = tested_object.calculate_quality(input_vector, |
59
|
|
|
tested_object.control_params) |
60
|
|
|
self.assertEqual(result, 55.25) |
61
|
|
|
|
62
|
|
|
def test_could_be_optimized_pos(self): |
63
|
|
|
"""Positive case for test could_be_optimized method.""" |
64
|
|
|
tested_object = Mock() |
65
|
|
|
tested_object.get_cost.return_value = MAX_CALLS - 1 |
66
|
|
|
tested_object.get_maximal_acceptable_cost.return_value = MAX_CALLS |
67
|
|
|
result = CallsStage.could_be_optimized(tested_object) |
68
|
|
|
self.assertTrue(result) |
69
|
|
|
|
70
|
|
|
def test_could_be_optimized_neg(self): |
71
|
|
|
"""Negative case for test could_be_optimized method.""" |
72
|
|
|
tested_object = Mock() |
73
|
|
|
tested_object.get_cost.return_value = MAX_CALLS + 1 |
74
|
|
|
tested_object.get_maximal_acceptable_cost.return_value = MAX_CALLS |
75
|
|
|
result = CallsStage.could_be_optimized(tested_object) |
76
|
|
|
self.assertFalse(result) |
77
|
|
|
|
78
|
|
|
def test_is_enough_quality(self): |
79
|
|
|
"""Test is_enough_quality method.""" |
80
|
|
|
tested_object = Mock() |
81
|
|
|
tested_object.maximum_acceptable_quality = 7 |
82
|
|
|
result = CallsStage.is_enough_quality(tested_object, 8) |
83
|
|
|
self.assertFalse(result) |
84
|
|
|
|
85
|
|
|
def test_get_output_of_stage(self): |
86
|
|
|
"""Test returning output.""" |
87
|
|
|
tested_object = CallsStage('name', MAX_CALLS) |
88
|
|
|
result = tested_object.get_output_of_stage([], []) |
89
|
|
|
self.assertEqual([], result) |
90
|
|
|
|
91
|
|
|
def test_get_maximal_acceptable_cost(self): |
|
|
|
|
92
|
|
|
tested_object = CallsStage('name', MAX_CALLS) |
93
|
|
|
result = tested_object.get_maximal_acceptable_cost() |
94
|
|
|
self.assertEqual(result, MAX_CALLS) |
95
|
|
|
|
96
|
|
|
def test___str__(self): |
|
|
|
|
97
|
|
|
tested_object = CallsStage('name', MAX_CALLS) |
98
|
|
|
res = str(tested_object) |
99
|
|
|
self.assertEqual(res, """{'control_params': [], |
100
|
|
|
'cost': 0, |
101
|
|
|
'final_cost': None, |
102
|
|
|
'final_output': None, |
103
|
|
|
'final_quality': None, |
104
|
|
|
'input_vector': (), |
105
|
|
|
'lower_bounds': [], |
106
|
|
|
'max_calls': 100, |
107
|
|
|
'maximum_acceptable_quality': 0.01, |
108
|
|
|
'name': 'name', |
109
|
|
|
'optimization_status': <OptimizationStatus.not_started: 'Not started'>, |
110
|
|
|
'upper_bounds': []}""") |
111
|
|
|
|
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.