|
1
|
|
|
from unittest import TestCase |
|
|
|
|
|
|
2
|
|
|
from unittest.mock import Mock |
|
3
|
|
|
|
|
4
|
|
|
from grortir.main.pso.credit_calls_group_optimization_strategy import \ |
|
5
|
|
|
CreditCallsGroupOptimizationStrategy |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestCreditCallsGroupOptimizationStrategy(TestCase): |
|
|
|
|
|
|
9
|
|
|
def test_initialize(self): |
|
|
|
|
|
|
10
|
|
|
process, stages = self.get_basic_process() |
|
11
|
|
|
tested_object = CreditCallsGroupOptimizationStrategy(stages, process) |
|
12
|
|
|
|
|
13
|
|
|
tested_object.initialize() |
|
14
|
|
|
|
|
15
|
|
|
self.assertEqual(tested_object.max_calls_for_group, 60) |
|
16
|
|
|
self.assertEqual(tested_object.expected_quality, 1) |
|
17
|
|
|
|
|
18
|
|
|
def get_basic_process(self): |
|
|
|
|
|
|
19
|
|
|
stages = [] |
|
20
|
|
|
for i in range(1, 4): |
|
21
|
|
|
stages.append(Mock()) |
|
22
|
|
|
stages[i - 1].get_cost.return_value = 0 |
|
23
|
|
|
stages[i - 1].get_maximal_acceptable_cost.return_value = i * 10 |
|
24
|
|
|
stages[i - 1].maximum_acceptable_quality = i |
|
25
|
|
|
process = Mock() |
|
26
|
|
|
process.nodes.return_value = stages |
|
27
|
|
|
return process, stages |
|
28
|
|
|
|
|
29
|
|
|
def test_initialize_exception(self): |
|
|
|
|
|
|
30
|
|
|
stages = [] |
|
31
|
|
|
for i in range(1, 4): |
|
32
|
|
|
stages.append(Mock()) |
|
33
|
|
|
stages[i - 1].get_cost.return_value = 0 |
|
34
|
|
|
process = Mock() |
|
35
|
|
|
process.nodes.return_value = stages |
|
36
|
|
|
stages[0].get_cost.return_value = 1 |
|
37
|
|
|
tested_object = CreditCallsGroupOptimizationStrategy(stages, process) |
|
38
|
|
|
with self.assertRaises(ValueError): |
|
39
|
|
|
tested_object.initialize() |
|
40
|
|
|
|
|
41
|
|
View Code Duplication |
def test_should_continue_true(self): |
|
|
|
|
|
|
42
|
|
|
process, stages = self.get_basic_process() |
|
43
|
|
|
tested_object = CreditCallsGroupOptimizationStrategy(stages, process) |
|
44
|
|
|
tested_object.initialize() |
|
45
|
|
|
stages[0].get_cost.return_value = 20 |
|
46
|
|
|
stages[1].get_cost.return_value = 20 |
|
47
|
|
|
stages[2].get_cost.return_value = 20 |
|
48
|
|
|
best_particle = Mock() |
|
49
|
|
|
best_particle.best_quality = 2 |
|
50
|
|
|
result = tested_object.should_continue(best_particle) |
|
51
|
|
|
self.assertTrue(result) |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
def test_should_continue_false_enough_quality(self): |
|
|
|
|
|
|
54
|
|
|
process, stages = self.get_basic_process() |
|
55
|
|
|
tested_object = CreditCallsGroupOptimizationStrategy(stages, process) |
|
56
|
|
|
tested_object.initialize() |
|
57
|
|
|
best_particle = Mock() |
|
58
|
|
|
stages[0].get_cost.return_value = 20 |
|
59
|
|
|
stages[1].get_cost.return_value = 20 |
|
60
|
|
|
stages[2].get_cost.return_value = 20 |
|
61
|
|
|
best_particle.best_quality = 0.5 |
|
62
|
|
|
result = tested_object.should_continue(best_particle) |
|
63
|
|
|
self.assertFalse(result) |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
def test_should_continue_false_not_safe_cost(self): |
|
|
|
|
|
|
66
|
|
|
process, stages = self.get_basic_process() |
|
67
|
|
|
tested_object = CreditCallsGroupOptimizationStrategy(stages, process) |
|
68
|
|
|
tested_object.initialize() |
|
69
|
|
|
best_particle = Mock() |
|
70
|
|
|
best_particle.best_quality = 2 |
|
71
|
|
|
stages[0].get_cost.return_value = 20 |
|
72
|
|
|
stages[1].get_cost.return_value = 20 |
|
73
|
|
|
stages[2].get_cost.return_value = 21 |
|
74
|
|
|
result = tested_object.should_continue(best_particle) |
|
75
|
|
|
self.assertFalse(result) |
|
76
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.