def test_downstream_success_blacklisted_headers_removed( self, mock_awsproxy): # Given mock_execute_downstream = CoroutineMock() mock_execute_downstream.return_value = HTTPResponse( request=HTTPRequest(url="https://awsservice.amazonaws.com/"), code=200, headers=HTTPHeaders({ "Host": "awsservice.amazonaws.com", "X-Amz-RequestId": "foo-abc", "Transfer-Encoding": "chunked", }), buffer=BytesIO(b"SomeResponse"), ) mock_instance = mock_awsproxy.return_value mock_instance.execute_downstream = mock_execute_downstream # When response = self.fetch("/awsproxy") # Then mock_execute_downstream.assert_awaited_once() assert 200 == response.code assert b"SomeResponse" == response.body assert "Transfer-Encoding" not in response.headers assert "foo-abc" == response.headers["X-Amz-RequestId"] assert "awsservice.amazonaws.com" == response.headers["Host"]
async def test_async_run(): coro = CoroutineMock() WrappedFunction._EVENT_LOOP = asyncio.get_event_loop() f = WrappedFunction(coro, name='coro_mock') f.run() await asyncio.sleep(0.05) coro.assert_awaited_once()
async def test_interrupt_receive_packet(self): action = "receivePacket" src = "src" payload = "payload" digest = "digest" msg = { "Action": action, "Src": src, "Payload": payload, "Digest": digest } mock_receive_packet = CoroutineMock() with patch( "nkn_client.websocket.nkn_api.NknWebsocketApiClient.receive_packet", new=mock_receive_packet): client = NknWebsocketApiClient() await client.interrupt(msg) mock_receive_packet.assert_awaited_once() mock_receive_packet.assert_awaited_once_with(Action=action, Src=src, Payload=payload, Digest=digest)
async def test_stop_awaits_for_the_currently_running_tasks_to_end(self): t1, t2 = CoroutineMock(), CoroutineMock() self.task_runner.running_tasks = {t1(), t2()} with patch.object(self.task_runner.clock, "stop"): await self.task_runner.stop(self.app) t1.assert_awaited_once() t2.assert_awaited_once()
async def test_disconnect(self): mock_ws = MagicMock() mock_disconnect = CoroutineMock() mock_ws.disconnect = mock_disconnect self._client._ws = mock_ws await self._client.disconnect() mock_disconnect.assert_awaited_once()
async def test_stop_stops_the_current_clock_ticker(self): clock = ClockTicker(seconds=2) clock._running = True _main_task = CoroutineMock() with asynctest.patch.object(clock, "_main_task", _main_task()): await clock.stop() _main_task.assert_awaited_once() self.assertFalse(clock._running)
async def test_init_connection(ds: IdexDatastream): ws_mock = Mock() create_conn_mock = CoroutineMock(return_value=ws_mock) ds.create_connection = create_conn_mock await ds._init_connection() create_conn_mock.assert_awaited_once() assert ds._ws is ws_mock another_mock_ws = Mock() await ds._init_connection(another_mock_ws) assert ds._ws is another_mock_ws
async def test_config_callback_on_change(config_service, monkeypatch): callback = mock.Mock() callback_coroutine = CoroutineMock() config.register_callback("DB_PASSWORD", callback) config.register_callback("CONTROL_SERVER_PORT", callback_coroutine) assert config.DB_PASSWORD == "banana" assert config.CONTROL_SERVER_PORT == 4000 monkeypatch.setenv("CONFIGURATION_FILE", "tests/data/refresh_conf.yaml") await asyncio.sleep(10) assert config.DB_PASSWORD == "apple" assert config.CONTROL_SERVER_PORT == 4001 callback.assert_called_once() callback_coroutine.assert_awaited_once()
async def test_play_track_plays_the_track(self, example_music_manager, monkeypatch): """ When a track is requested to be played, perform the following steps: - Get the path (url or file path) for the track - Create a MediaPlayer instance - Call the play() method on the media player - Wait for it to start playing - Set the volume with _set_master_volume() - While the media player is_playing(), wait """ media_player_mock = MagicMock() is_playing_mock = MagicMock(side_effect=[True, True, False ]) # Only plays for 2 steps media_player_mock.is_playing = is_playing_mock get_track_path_mock = MagicMock(return_value="url") sleep_mock = CoroutineMock() wait_for_start_mock = CoroutineMock() set_master_volume_mock = CoroutineMock( ) # necessary because it will use asyncio.sleep and mess up the numbers monkeypatch.setattr("src.music.music_manager.vlc.MediaPlayer", MagicMock(return_value=media_player_mock)) monkeypatch.setattr("src.music.music_manager.utils.get_track_path", get_track_path_mock) monkeypatch.setattr( "src.music.music_manager.MusicManager._wait_for_current_player_to_be_playing", wait_for_start_mock) monkeypatch.setattr( "src.music.music_manager.MusicManager._set_master_volume", set_master_volume_mock) monkeypatch.setattr("src.music.music_manager.asyncio.sleep", sleep_mock) example_music_manager.volume = 55 group = example_music_manager.groups[0] track_list = group.track_lists[0] track = track_list.tracks[0] await example_music_manager._play_track(group=group, track_list=track_list, track=track) get_track_path_mock.assert_called_once() media_player_mock.play.assert_called_once() wait_for_start_mock.assert_awaited_once() set_master_volume_mock.assert_awaited_once_with( example_music_manager.volume, set_global=False) assert is_playing_mock.call_count == 3 # is_playing becomes False at the 3rd call, so stop assert sleep_mock.await_count == 2 # Two times for while player is playing
def test_downstream_error_with_body(self, mock_awsproxy): # Given mock_execute_downstream = CoroutineMock() mock_execute_downstream.side_effect = HTTPClientError( code=403, response=HTTPResponse(request=HTTPRequest("/foo"), code=403, buffer=BytesIO(b"AccessDenied")), ) mock_instance = mock_awsproxy.return_value mock_instance.execute_downstream = mock_execute_downstream # When response = self.fetch("/awsproxy") # Then mock_execute_downstream.assert_awaited_once() assert 403 == response.code assert b"AccessDenied" == response.body
async def test_returns_status_from_underlying_call( self, run_query, flow_run_id, payload_response, monkeypatch ): """ This test should ensure that the `status` field should be determined based on the underlying `api.states.set_flow_run_state()` call. """ mock_state_api = CoroutineMock(return_value=payload_response) monkeypatch.setattr( "src.prefect_server.graphql.states.api.states.set_flow_run_state", mock_state_api, ) result = await run_query( query=self.mutation, variables=dict( input=dict( states=[ dict( flow_run_id=flow_run_id, version=1, state=Running().serialize(), ) ] ) ), ) mock_state_api.assert_awaited_once() assert ( result.data.set_flow_run_states.states[0].status == payload_response["status"] )