1
|
|
|
from unittest import TestCase |
|
|
|
|
2
|
|
|
from unittest.mock import Mock |
3
|
|
|
|
4
|
|
|
import numpy as np |
|
|
|
|
5
|
|
|
|
6
|
|
|
from grortir.main.pso.particle import Particle |
7
|
|
|
from grortir.main.pso.position_updater import PositionUpdater |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TestParticle(TestCase): |
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
self.particle_mock = Mock() |
13
|
|
|
self.stages = [Mock(), Mock()] |
14
|
|
|
self.position_updater = Mock() |
15
|
|
|
self.velocity_calculator = Mock() |
16
|
|
|
for i in range(len(self.stages)): |
17
|
|
|
self.velocity_calculator. \ |
18
|
|
|
calculate_initial_velocity.return_value = 0.01 * (i + 1) |
19
|
|
|
self.particle_mock.stages = self.stages |
20
|
|
|
self.particle_mock.position_updater = self.position_updater |
21
|
|
|
self.particle_mock.velocity_calculator = self.velocity_calculator |
22
|
|
|
self.particle_mock.current_velocities = {} |
23
|
|
|
self.particle_mock.current_quality = {stage: 100 for stage in |
24
|
|
|
self.stages} |
25
|
|
|
self.particle_mock.current_control_params = {} |
26
|
|
|
self.particle_mock.current_input = {} |
27
|
|
|
self.process = Mock() |
28
|
|
|
self.particle_mock.process = self.process |
29
|
|
|
|
30
|
|
|
def test_get_the_overall_quality_po(self): |
|
|
|
|
31
|
|
|
particle_mock = Mock() |
32
|
|
|
particle_mock.current_quality = {'a': 2, 'b': 3, 'c': -3.1234} |
33
|
|
|
result = Particle.get_the_overall_quality(particle_mock) |
34
|
|
|
self.assertEqual(result, 3) |
35
|
|
|
|
36
|
|
|
def test___init__(self): |
|
|
|
|
37
|
|
|
particle = Particle(self.stages, self.process, 7) |
38
|
|
|
self.assertIsInstance(particle.position_updater, |
39
|
|
|
PositionUpdater) |
40
|
|
|
self.assertIsNotNone(particle) |
41
|
|
|
self.assertEqual(particle.best_quality, np.inf) |
42
|
|
|
|
43
|
|
|
def test_initialize(self): |
|
|
|
|
44
|
|
|
Particle.initialize(self.particle_mock) |
45
|
|
|
self.particle_mock._set_initial_positions.assart_any_call() |
|
|
|
|
46
|
|
|
self.particle_mock._set_initial_velocities.assart_any_call() |
|
|
|
|
47
|
|
|
|
48
|
|
|
def test_update_values(self): |
|
|
|
|
49
|
|
|
Particle.update_values(self.particle_mock) |
50
|
|
|
self.particle_mock.update_input_vectors.assert_any_call() |
51
|
|
|
self.particle_mock.calculate_current_quality.assert_any_call() |
52
|
|
|
self.particle_mock._update_best_position.assert_any_call() |
|
|
|
|
53
|
|
|
|
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.