Completed
Push — master ( 45e830...996a6f )
by Wojtek
02:50
created

TestInteligentCredits   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 69
rs 10
c 1
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_credit_strategy_fail_at_last_step() 0 7 1
B test_credit_strategy_fail_on_group() 0 28 4
A test_credit_strategy_fail_between_groups() 0 23 3
A test_credit_strategy_success() 0 7 1
1
"""Test for intelligent credits."""
2
from unittest import TestCase
3
4
from grortir.main.model.core.abstract_process import AbstractProcess
5
from grortir.main.model.core.optimization_status import OptimizationStatus
6
from grortir.main.model.stages.calls_stage import CallsStage
7
from grortir.main.optimizers.grouping_strategy import GroupingStrategy
8
from grortir.main.pso.credit_calls_optimization_strategy import \
9
    CreditCallsOptimizationStrategy
10
from grortir.main.pso.pso_algorithm import PsoAlgorithm
11
12
13
class TestInteligentCredits(TestCase):
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...
14
    def test_credit_strategy_success(self):
0 ignored issues
show
Coding Style introduced by
This method 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...
15
        pso_algorithm, stages = prepare_data()
16
        stages[7].max_calls += 27
17
        pso_algorithm.run()
18
        is_success = pso_algorithm.process.optimization_status \
19
            == OptimizationStatus.success
20
        self.assertTrue(is_success)
21
22
    def test_credit_strategy_fail_at_last_step(self):
0 ignored issues
show
Coding Style Naming introduced by
The name test_credit_strategy_fail_at_last_step does not conform to the method naming conventions ([a-z_][a-z0-9_]{2,30}$).

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...
Coding Style introduced by
This method 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...
23
        pso_algorithm, stages = prepare_data()
24
        stages[7].max_calls += 26
25
        pso_algorithm.run()
26
        is_success = pso_algorithm.process.optimization_status \
27
            == OptimizationStatus.success
28
        self.assertFalse(is_success)
29
30
    def test_credit_strategy_fail_between_groups(self):
0 ignored issues
show
Coding Style Naming introduced by
The name test_credit_strategy_fail_between_groups does not conform to the method naming conventions ([a-z_][a-z0-9_]{2,30}$).

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...
Coding Style introduced by
This method 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...
31
        pso_algorithm, stages = prepare_data()
32
        stages[0].max_calls = 60
33
        stages[1].max_calls = 60
34
        stages[2].max_calls = 60
35
        stages[3].max_calls = 60
36
        stages[4].max_calls = 60
37
        stages[5].max_calls = 0
38
        stages[6].max_calls = 0
39
        stages[7].max_calls = 0
40
        pso_algorithm.run()
41
        is_success = pso_algorithm.process.optimization_status \
42
            == OptimizationStatus.success
43
        self.assertFalse(is_success)
44
        for i in range(3, 8):
45
            self.assertIsNone(stages[i].final_output)
46
            self.assertIsNone(stages[i].final_cost)
47
            self.assertIsNone(stages[i].final_quality)
48
            self.assertEqual(stages[i].optimization_status,
49
                             OptimizationStatus.failed)
50
        for i in range(0, 3):
51
            self.assertEqual(stages[i].optimization_status,
52
                             OptimizationStatus.success)
53
54
    def test_credit_strategy_fail_on_group(self):
0 ignored issues
show
Coding Style Naming introduced by
The name test_credit_strategy_fail_on_group does not conform to the method naming conventions ([a-z_][a-z0-9_]{2,30}$).

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...
Coding Style introduced by
This method 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...
55
        pso_algorithm, stages = prepare_data()
56
        stages[0].max_calls = 60
57
        stages[1].max_calls = 60
58
        stages[2].max_calls = 60
59
        stages[3].max_calls = 60
60
        stages[4].max_calls = 60
61
        stages[5].max_calls = 3
62
        stages[6].max_calls = 0
63
        stages[7].max_calls = 0
64
        pso_algorithm.run()
65
        is_success = pso_algorithm.process.optimization_status \
66
            == OptimizationStatus.success
67
        self.assertFalse(is_success)
68
        for i in range(3, 6):
69
            self.assertEqual(stages[i].final_cost, 2)
70
            self.assertEqual(stages[i].final_quality, 10000)
71
            self.assertEqual(stages[i].optimization_status,
72
                             OptimizationStatus.failed)
73
        for i in range(6, 8):
74
            self.assertIsNone(stages[i].final_output)
75
            self.assertIsNone(stages[i].final_cost)
76
            self.assertIsNone(stages[i].final_quality)
77
            self.assertEqual(stages[i].optimization_status,
78
                             OptimizationStatus.failed)
79
        for i in range(0, 3):
80
            self.assertEqual(stages[i].optimization_status,
81
                             OptimizationStatus.success)
82
83
84
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...
85
    pass
86
87
88
class FixedCallsStage(CallsStage):
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...
89
    def __init__(self, name, max_calls, input_vector, on_which_cost_success):
90
        super().__init__(name, max_calls, input_vector)
91
        self.on_which_cost_success = on_which_cost_success
92
93
    def is_enough_quality(self, value):
94
        return self.on_which_cost_success <= self.get_cost()
95
96
    def calculate_quality(self, input_vector, control_params):
0 ignored issues
show
Bug introduced by
Arguments number differs from overridden 'calculate_quality' method
Loading history...
97
        if self.is_enough_quality(1):
98
            return 0
99
        return 10000
100
101
    def get_output_of_stage(self, input_vector, control_params):
102
        return input_vector
103
104
105
def prepare_data():
0 ignored issues
show
Coding Style introduced by
This function 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...
106
    stages = {}
107
    for i in range(8):
108
        stages[i] = FixedCallsStage(str(i), 70, (0, 0, 0), (100 - i * 10))
109
    # Summary max_calls is equal to 560
110
    tested_process = ExampleProcess()
111
    # Our graph:
112
    #   0
113
    #   |
114
    #   1
115
    #   |\
116
    #   2 4
117
    #   | |\
118
    #   3 5 6
119
    #        \
120
    #         7
121
    # All edges directed to down
122
    # Order of nodes is the same as names
123
    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...
124
    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...
125
    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...
126
    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...
127
    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...
128
    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...
129
    tested_process.add_edge(stages[6], stages[7])
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...
130
    # Groups:
131
    #   (0)0
132
    #      |
133
    #   (0)1
134
    #      |\
135
    #   (0)2 4(1)
136
    #      | | \
137
    #   (1)3 5(1)6(2)
138
    #             \
139
    #              7(3)
140
    # Minimum required steps to success: 3*101+3*71+1*41+1*31= 588
141
    grouping_strategy = GroupingStrategy(list(stages.values()))
142
    grouping_strategy.define_group((stages[0], stages[1], stages[2]))
143
    grouping_strategy.define_group((stages[3], stages[4], stages[5]))
144
    grouping_strategy.define_group((stages[6],))
145
    grouping_strategy.define_group((stages[7],))
146
    optimization_strategy = CreditCallsOptimizationStrategy()
147
    pso_algorithm = PsoAlgorithm(tested_process, grouping_strategy,
148
                                 optimization_strategy, 2)
149
    return pso_algorithm, stages
150