| 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 |  |  | from grortir.main.pso.velocity_calculator import VelocityCalculator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class TestParticle(TestCase): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         self.particle_mock = Mock() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         self.stages = [Mock(), Mock()] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         self.position_updaters = {stage: Mock() for stage in self.stages} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         self.velocity_calculators = {stage: Mock() for stage in self.stages} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         for i in range(len(self.stages)): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             stage = self.stages[i] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             self.velocity_calculators[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |                 stage].calculate_initial_velocity.return_value = 0.01 * (i + 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self.particle_mock.stages = self.stages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self.particle_mock.position_updaters = self.position_updaters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         self.particle_mock.velocity_calculators = self.velocity_calculators | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         self.particle_mock.current_velocities = {} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         self.particle_mock.current_quality = {stage: 100 for stage in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                                               self.stages} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         self.assertIsNotNone(particle) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         self.assertEqual(particle.best_quality, np.inf) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     def test__set_initial_positons(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         Particle._set_initial_positions(self.particle_mock) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         for stage in self.stages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             self.position_updaters[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 stage].set_initial_control_params.assert_any_call() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def test__set_initial_velocities(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         Particle._set_initial_velocities(self.particle_mock) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         i = 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         for stage in self.stages: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             self.assertEqual(self.particle_mock.current_velocities[stage], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                              0.01 * (i + 1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             i += 1 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     def test_initialize(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         Particle.initialize(self.particle_mock) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         for stage in self.stages: | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             self.assertEqual(self.particle_mock.position_updaters[stage].stage, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                              stage) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             self.assertIsInstance(self.particle_mock.position_updaters[stage], | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                                   PositionUpdater) | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             self.assertEqual( | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 self.particle_mock.velocity_calculators[stage].stage, | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 stage) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             self.assertIsInstance( | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                 self.particle_mock.velocity_calculators[stage], | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 VelocityCalculator) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         self.particle_mock._set_initial_positions.assart_any_call() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         self.particle_mock._set_initial_velocities.assart_any_call() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     def test_update_values(self): | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         Particle.update_values(self.particle_mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         self.particle_mock.update_input_vectors.assert_any_call() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         self.particle_mock.calculate_current_quality.assert_any_call() | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |         self.particle_mock._update_best_position.assert_any_call() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 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.