def test_injector_not_provided(): """ Should return an exception if not provided. """ i = DependencyContainer() with pytest.raises(Exception): i.get('test')
def test_injector_abstract_class(): """ Should not be able to provide an abstract class. """ i = DependencyContainer() with pytest.raises(Exception): i.provide('test', AbstractClass)
def test_injector_manual_argument_in_constructor(): """ Should be able to manually pass arguments for the constructor. """ i = DependencyContainer() i.provide('dummy_c', DummyC, my_other_dep=1, my_array=[42]) dummy_c = i.get('dummy_c') assert dummy_c is not None assert dummy_c.my_array == [42] assert dummy_c.my_other_dep == 1
def test_injector_missing_dependencies(): """ Should not be able to instantiate if a dependency is missing. """ i = DependencyContainer() i.provide('test', DummyC) with pytest.raises(Exception): i.get('test')
def test_injector_double_provide(): """ Should not be able to provide the same thing twice. """ i = DependencyContainer() i.provide('test', DummyA) with pytest.raises(Exception): i.provide('test', DummyA)
async def _get_container(simulation: bool, stub_lidar: bool, stub_socket_can: bool) -> DependencyContainer: """ Build the dependency container. """ i = DependencyContainer() i.provide('configuration', CONFIG) i.provide('protobuf_router', ProtobufRouter) i.provide('odometry_function', lambda: odometry_arc) i.provide('position_controller', PositionController) i.provide('motor_gateway', MotorGateway) i.provide('motion_controller', MotionController) i.provide('trajectory_controller', TrajectoryController) i.provide('strategy_controller', StrategyController) i.provide('symmetry_controller', SymmetryController) i.provide('obstacle_controller', ObstacleController) i.provide('debug_controller', DebugController) i.provide('match_action_controller', MatchActionController) i.provide('probe', Probe) i.provide('event_loop', asyncio.get_event_loop()) i.provide('http_client', HTTPClient) i.provide('web_browser_client', WebBrowserClient) i.provide('replay_saver', ReplaySaver) if simulation: i.provide('simulation_configuration', SIMULATION_CONFIG) i.provide('simulation_router', SimulationRouter) i.provide('simulation_runner', SimulationRunner) i.provide( 'simulation_state', SimulationState(time=0, cups=[], left_tick=0, right_tick=0, left_speed=0, right_speed=0, queue_speed_left=deque(), queue_speed_right=deque(), last_position_update=0, last_lidar_update=0)) i.provide('simulation_gateway', SimulationGateway) i.provide('clock', FakeClock) else: i.provide('clock', RealClock) if simulation or stub_lidar: i.provide('lidar_adapter', SimulatedLIDARAdapter) else: rplidar_obj = rplidar.RPLidar(list_ports.comports()[0].device) i.provide('rplidar_object', rplidar_obj) i.provide('lidar_adapter', RPLIDARAdapter) servo_adapter_list: List[SocketAdapter] = [] if simulation or stub_socket_can: i.provide('motor_board_adapter', LoopbackSocketAdapter) for _ in range(NB_SERVO_BOARDS): servo_adapter_list.append(LoopbackSocketAdapter()) else: i.provide('motor_board_adapter', ISOTPSocketAdapter, address=NET_ADDRESS_MOTOR, adapter_name='motor_board') for index in range(NB_SERVO_BOARDS): servo_adapter_list.append( ISOTPSocketAdapter(address=NET_ADDRESSES_SERVO[index], adapter_name="servo_board_" + str(index))) i.provide('servo_adapters_list', servo_adapter_list) i.provide('actuator_gateway', ActuatorGateway) i.provide('actuator_controller', ActuatorController) return i
async def _get_container(simulation: bool, stub_lidar: bool, stub_socket_can: bool) -> DependencyContainer: """ Build the dependency container. """ i = DependencyContainer() i.provide('configuration', CONFIG) i.provide('protobuf_handler', ProtobufHandler) i.provide('odometry_controller', OdometryController) i.provide('localization_controller', LocalizationController) i.provide('motion_controller', MotionController) i.provide('strategy_controller', StrategyController) i.provide('symmetry_controller', SymmetryController) i.provide('lidar_controller', LidarController) i.provide('debug_controller', DebugController) i.provide('match_action_controller', MatchActionController) i.provide('motion_gateway', MotionGateway) i.provide('simulation_probe', SimulationProbe) i.provide('event_loop', asyncio.get_event_loop()) if simulation: i.provide('simulation_configuration', SIMULATION_CONFIG) i.provide('event_queue', EventQueue()) i.provide('simulation_handler', SimulationHandler) i.provide('simulation_runner', SimulationRunner) i.provide( 'simulation_state', SimulationState(time=0, cups=[], left_tick=0, right_tick=0, last_position_update=0, last_lidar_update=0)) i.provide('simulation_gateway', SimulationGateway) i.provide('http_client', HTTPClient) i.provide('web_browser_client', WebBrowserClient) i.provide('replay_saver', ReplaySaver) if simulation or stub_lidar: i.provide('lidar_adapter', SimulatedLIDARAdapter) else: rplidar_obj = rplidar.RPLidar(list_ports.comports()[0].device) i.provide('rplidar_object', rplidar_obj) i.provide('lidar_adapter', RPLIDARAdapter) if simulation or stub_socket_can: i.provide('socket_adapter', LoopbackSocketAdapter) i.provide('motor_board_adapter', LoopbackSocketAdapter) else: reader, writer = await tcp.get_reader_writer('localhost', 32000) i.provide('motor_board_adapter', TCPSocketAdapter, reader=reader, writer=writer) return i
def test_injector_happy_path(): """ Happy path. """ i = DependencyContainer() i.provide('my_dep', DummyA) i.provide('my_array', [1, 2, 3]) i.provide('my_other_dep', DummyB) i.provide('dummy_c', DummyC) dummy_c = i.get('dummy_c') assert dummy_c is not None assert dummy_c.my_array == [1, 2, 3] assert isinstance(dummy_c.my_other_dep, DummyB) assert isinstance(dummy_c.my_other_dep.my_dep, DummyA)