TestDirection.test_route_direct_route()   B
last analyzed

Complexity

Conditions 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 42
rs 8.8571
cc 2
1
from intelligine.core.exceptions import NoMolecule
2
from intelligine.simulation.molecule.Molecule import Molecule
3
from intelligine.simulation.molecule.MoleculeFlavour import MoleculeFlavour
4
from intelligine.tests.simulation.molecule.Base import Base
5
from intelligine.simulation.molecule.DirectionMolecule import DirectionMolecule
6
from intelligine.core.Context import Context
7
from intelligine.cst import MOLECULES_DIRECTION, PHEROMON_DIR_EXPLO, PHEROMON_DIR_NONE
8
from intelligine.synergy.event.move.direction import NORTH, NORTH_EST, EST, SOUTH_EST, SOUTH, SOUTH_WEST, WEST, \
9
    NORTH_WEST, CENTER
10
from intelligine.synergy.event.move.direction import get_position_with_direction_decal as _p
11
12
13
class TestDirection(Base):
14
15
    def __init__(self, *args, **kwargs):
16
        super().__init__(*args, **kwargs)
17
        self._context = Context()
18
19
    def setUp(self):
20
        self._context = Context()
21
22
    def _set_up_molecules(self, molecules, re_init=True):
23
        if re_init:
24
            self._context = Context()
25
        for position in molecules:
26
            self._context.molecules().set_flavour(position, MoleculeFlavour.new_from_raw_data(molecules[position]))
27
28
    def _test_direction_for_point(self, molecules, direction, molecule_type=PHEROMON_DIR_EXPLO,
29
                                  reference_point=_p(CENTER), re_init=True):
30
        """
31
32
        :param molecules:
33
        :param direction:
34
        :param molecule_type:
35
        :param reference_point:
36
        :return:
37
        """
38
        self._set_up_molecules(molecules, re_init=re_init)
39
        direction_tested = DirectionMolecule.get_direction_for_point(self._context, reference_point, molecule_type)
40
        self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
41
42
    def _test_direction_for_points(self, molecules, direction, molecule_type=PHEROMON_DIR_EXPLO,
43
                                   reference_point=_p(CENTER), re_init=True):
44
        """
45
46
        :param molecules:
47
        :param direction:
48
        :param molecule_type:
49
        :param reference_point:
50
        :return:
51
        """
52
        self._set_up_molecules(molecules, re_init=re_init)
53
        around_points = self._context.get_around_points_of_point(reference_point)
54
        direction_tested = DirectionMolecule.get_best_molecule_direction_in(self._context,
55
                                                                              reference_point,
56
                                                                              around_points,
57
                                                                              molecule_type)
58
        self.assertEqual(direction, direction_tested, "Direction must be %s" % direction)
59
60
    def test_route_direct_route(self):
61
        """
62
        Test easy direction with 1 best molecules just near actual position
63
        :return:
64
        """
65
        test_data = {
66
            NORTH_WEST: {
67
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
68
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
69
            },
70
            NORTH: {
71
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
72
                _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
73
            },
74
            NORTH_EST: {
75
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
76
                _p(NORTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
77
            },
78
            WEST: {
79
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
80
                _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
81
            },
82
            EST: {
83
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
84
                _p(EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
85
            },
86
            SOUTH_WEST: {
87
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
88
                _p(SOUTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
89
            },
90
            SOUTH: {
91
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
92
                _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
93
            },
94
            SOUTH_EST: {
95
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
96
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}}
97
            }
98
        }
99
100
        for direction_wanted in test_data:
101
            self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
102
103
    def test_route_with_multiple_same_intensity(self):
104
        """
105
        Test find route in middle of multiple molecules
106
        :return:
107
        """
108
        test_data = {
109
            NORTH_WEST: {
110
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
111
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
112
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}}
113
            },
114
            NORTH_WEST: {
115
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
116
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
117
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}},
118
                _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}}
119
            },
120
            NORTH_WEST: {
121
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 1, 0)}},
122
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 1, 0)}},
123
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (12, 1, 0)}},
124
                _p(SOUTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (8, 1, 0)}}
125
            },
126
        }
127
128
        for direction_wanted in test_data:
129
            self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
130
131 View Code Duplication
    def test_route_with_multiple_different_intensity(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
132
        """
133
        Test find route in middle of multiple molecules
134
        :return:
135
        """
136
        test_data = {
137
            NORTH_WEST: {
138
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2, 0)}},
139
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
140
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1, 0)}}
141
            },
142
            NORTH_WEST: {
143
                _p(CENTER): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (10, 2, 0)}},
144
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
145
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (8, 1, 0)}},
146
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (5, 10, 0)}}  # an other molecule type
147
            }
148
        }
149
150
        for direction_wanted in test_data:
151
            self._test_direction_for_point(test_data[direction_wanted], direction_wanted)
152
153
    def test_direction_direct(self):
154
        test_data = {
155
            NORTH: {
156
                _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}}
157
            },
158
            NORTH: {
159
                _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 2, 0)}},
160
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500, 0)}}  # An other molecule type
161
            }
162
        }
163
164
        for direction in test_data:
165
            self._test_direction_for_points(test_data[direction], direction)
166
167 View Code Duplication
    def test_direction_with_multiple_intensity(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
168
        test_data = {
169
            NORTH: {
170
                _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5, 0)}},
171
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}},
172
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}}
173
            },
174
            NORTH: {
175
                _p(NORTH): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 5, 0)}},
176
                _p(WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 500, 0)}},  # An other molecule_type
177
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}},
178
                _p(NORTH_WEST): {MOLECULES_DIRECTION: {PHEROMON_DIR_EXPLO: (9, 4, 0)}}
179
            }
180
        }
181
182
        for direction in test_data:
183
            self._test_direction_for_points(test_data[direction], direction)
184
185
    def test_no_molecules_around(self):
186
        # No molecule
187
        with self.assertRaises(NoMolecule):
188
            self._test_direction_for_points({}, -1)
189
190
        # Wrong molecule type
191
        with self.assertRaises(NoMolecule):
192
            self._test_direction_for_points({
193
                _p(SOUTH_EST): {MOLECULES_DIRECTION: {PHEROMON_DIR_NONE: (9, 5, 0)}}
194
            }, -1)
195
196
    def test_appose(self):
197
        self._test_point_raise_no_molecule()
198
        self._test_points_raise_no_molecule()
199
200
        # Une molecule au centre
201
        DirectionMolecule.appose(self._context,
202
                                  _p(CENTER),
203
                                  self._get_molecule(PHEROMON_DIR_EXPLO, 2))
204
        # Ne permet pas de trouver une route
205
        self._test_point_raise_no_molecule(re_init=False)
206
        self._test_points_raise_no_molecule(re_init=False)
207
208
        # Une molecule au nord
209
        DirectionMolecule.appose(self._context,
210
                                  _p(NORTH),
211
                                  self._get_molecule(PHEROMON_DIR_EXPLO, 1))
212
        # le permet
213
        self._test_direction_for_points({}, NORTH, re_init=False)
214
        self._test_direction_for_point({}, NORTH, re_init=False)
215
216
    def _test_point_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
217
                                 reference_point=_p(CENTER), re_init=True):
218
        with self.assertRaises(NoMolecule):
219
            self._test_direction_for_point(molecules, direction, re_init=re_init)
220
221
    def _test_points_raise_no_molecule(self, molecules={}, direction=-1, molecule_type=PHEROMON_DIR_EXPLO,
222
                                 reference_point=_p(CENTER), re_init=True):
223
        with self.assertRaises(NoMolecule):
224
            self._test_direction_for_points(molecules, direction, re_init=re_init)
225
226
    def _get_molecule(self, type, distance):
227
        return Molecule(MOLECULES_DIRECTION, type, distance=distance)
228