Python aioresponses.aioresponses() Examples
The following are 30
code examples of aioresponses.aioresponses().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
aioresponses
, or try the search function
.
Example #1
Source File: test_actions.py From rasa_core with Apache License 2.0 | 7 votes |
def test_remote_action_endpoint_responds_500( default_dispatcher_collecting, default_domain): tracker = DialogueStateTracker("default", default_domain.slots) endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: mocked.post('https://example.com/webhooks/actions', status=500) with pytest.raises(Exception) as execinfo: await remote_action.run(default_dispatcher_collecting, tracker, default_domain) assert "Failed to execute custom action." in str(execinfo.value)
Example #2
Source File: test_actions.py From rasa_core with Apache License 2.0 | 7 votes |
def test_remote_action_endpoint_responds_400( default_dispatcher_collecting, default_domain): tracker = DialogueStateTracker("default", default_domain.slots) endpoint = EndpointConfig("https://example.com/webhooks/actions") remote_action = action.RemoteAction("my_action", endpoint) with aioresponses() as mocked: # noinspection PyTypeChecker mocked.post( 'https://example.com/webhooks/actions', exception=ClientResponseError( 400, None, '{"action_name": "my_action"}')) with pytest.raises(Exception) as execinfo: await remote_action.run(default_dispatcher_collecting, tracker, default_domain) assert execinfo.type == ActionExecutionRejection assert "Custom action 'my_action' rejected to run" in str(execinfo.value)
Example #3
Source File: test_agents.py From asgard-api with MIT License | 7 votes |
def test_agent_app_list_zero_apps_running(self): self._prepare_additional_fixture_data() await self.pg_data_mocker.create() slave_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S3" async with HttpClientContext(app) as client: local_address = ( f"http://{client._server.host}:{client._server.port}" ) with aioresponses(passthrough=[local_address]) as rsps: build_mesos_cluster(rsps, slave_id) resp = await client.get( f"/agents/{slave_id}/apps", headers={"Authorization": f"Token {self.user_auth_key}"}, ) self.assertEqual(200, resp.status) data = await resp.json() self.assertEqual(0, len(data["apps"]))
Example #4
Source File: test_api_request.py From python-idex with MIT License | 6 votes |
def test_api_exception(): """Test API response Exception""" loop = asyncio.get_event_loop() with aioresponses() as m: json_obj = { "error": "Signature verification failed" } m.post('https://api.idex.market/return24Volume', payload=json_obj, status=200) async def _run_test(): client = await AsyncClient.create(api_key) with pytest.raises(IdexAPIException): await client.get_24hr_volume() loop.run_until_complete(_run_test())
Example #5
Source File: test_scale_apps.py From asgard-api with MIT License | 6 votes |
def test_tune_everything_in_multiple_apps(self): interface = AsgardInterface() app1 = ScalableApp("test1") app2 = ScalableApp("test2") app3 = ScalableApp("test3") decisions = [ Decision(app1.id, cpu=0.2, mem=10), Decision(app2.id, cpu=0.4, mem=20), Decision(app3.id, cpu=0.1, mem=9), ] with aioresponses() as rsps: rsps.put( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"deploymentId": "test2", "version": "1.0"}, ) applied_decisions = await interface.apply_decisions(decisions) self.assertEqual(len(applied_decisions), 3) for i in range(len(decisions)): self.assertEqual(applied_decisions[i]["id"], decisions[i].id) self.assertEqual(applied_decisions[i]["cpus"], decisions[i].cpu) self.assertEqual(applied_decisions[i]["mem"], decisions[i].mem)
Example #6
Source File: test_scale_apps.py From asgard-api with MIT License | 6 votes |
def test_tune_everything_in_one_app(self): interface = AsgardInterface() app = ScalableApp("test") decisions = [Decision(app.id, cpu=0.3, mem=9)] with aioresponses() as rsps: rsps.put( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"deploymentId": "test1", "version": "1.0"}, ) applied_decisions = await interface.apply_decisions(decisions) self.assertEqual(len(applied_decisions), 1) self.assertEqual(applied_decisions[0]["id"], decisions[0].id) self.assertEqual(applied_decisions[0]["cpus"], decisions[0].cpu) self.assertEqual(applied_decisions[0]["mem"], decisions[0].mem)
Example #7
Source File: test_interpreter.py From rasa_core with Apache License 2.0 | 6 votes |
def test_http_interpreter(): with aioresponses() as mocked: mocked.post("https://example.com/parse") endpoint = EndpointConfig('https://example.com') interpreter = RasaNLUHttpInterpreter(endpoint=endpoint) await interpreter.parse(text='message_text', message_id='1134') r = latest_request( mocked, "POST", "https://example.com/parse") query = json_of_latest_request(r) response = {'project': 'default', 'q': 'message_text', 'message_id': '1134', 'model': None, 'token': None} assert query == response
Example #8
Source File: test_processor.py From rasa_core with Apache License 2.0 | 6 votes |
def test_http_parsing(): message = UserMessage('lunch?') endpoint = EndpointConfig('https://interpreter.com') with aioresponses() as mocked: mocked.post('https://interpreter.com/parse', repeat=True, status=200) inter = RasaNLUHttpInterpreter(endpoint=endpoint) try: await MessageProcessor( inter, None, None, None, None)._parse_message(message) except KeyError: pass # logger looks for intent and entities, so we except r = latest_request( mocked, 'POST', "https://interpreter.com/parse") assert r assert json_of_latest_request(r)['message_id'] == message.message_id
Example #9
Source File: test_forecast.py From darksky with MIT License | 6 votes |
def get_forecast_async(): async def get_async_data(): darksky = DarkSkyAsync("api_key") with aioresponses.aioresponses() as resp: resp.get(re.compile(".+"), status=200, payload=copy.deepcopy(DATA)) result = await darksky.get_forecast( DATA["latitude"], DATA["longitude"], client_session=aiohttp.ClientSession() ) return result loop = asyncio.get_event_loop() return loop.run_until_complete(get_async_data())
Example #10
Source File: test_interactive.py From rasa_core with Apache License 2.0 | 6 votes |
def test_print_history(mock_endpoint): tracker_dump = utils.read_file( "data/test_trackers/tracker_moodbot.json") sender_id = uuid.uuid4().hex url = '{}/conversations/{}/tracker?include_events=AFTER_RESTART'.format( mock_endpoint.url, sender_id) with aioresponses() as mocked: mocked.get(url, body=tracker_dump, headers={"Accept": "application/json"}) await interactive._print_history(sender_id, mock_endpoint) assert latest_request(mocked, 'get', url) is not None
Example #11
Source File: test_mesos_client.py From asgard-api with MIT License | 6 votes |
def test_mesos_client_try_all_mesos_master_urls_on_exception(self): """ Se uma (ou mais) das URLs estiver com problemas, devemos tentar todas as outras antes de retornar uma exception """ agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S0" agent_info = get_fixture( "agents/ead07ffb-5a61-42c9-9386-21b680597e6c-S0/info.json" ) async with MesosClient(*settings.MESOS_API_URLS) as mesos: with aioresponses() as rsps: rsps.get( f"{settings.MESOS_API_URLS[0]}/slaves?slave_id={agent_id}", exception=Exception("Connection Error"), ) rsps.get( f"{settings.MESOS_API_URLS[1]}/slaves?slave_id={agent_id}", status=200, payload={"slaves": [agent_info], "recovered_slaves": []}, ) agent = await mesos.get_agent_by_id(agent_id=agent_id) self.assertEqual(agent_id, agent.id)
Example #12
Source File: test_http_client.py From asgard-api with MIT License | 6 votes |
def test_headers_passed_on_the_request_ovewrites_default_headers( self ): """ Se um header passado em `client.get(..., headers={})` tiver o mesmo nome de um outro header passado na instanciação do http client, o header do request deve ser usado """ default_headers = {"X-Header": "Value"} additional_headers = {"X-More": "Other", "X-Header": "Override"} client = HttpClient(headers=default_headers) with aioresponses() as rsps: rsps.get(TEST_URL, status=200) resp = await client.get(TEST_URL, headers=additional_headers) self.assertEqual(HTTPStatus.OK, resp.status) req = rsps.requests.get(("get", URL(TEST_URL))) expected_headers = {**additional_headers} self.assertEqual(expected_headers, req[0].kwargs.get("headers"))
Example #13
Source File: test_http_client.py From asgard-api with MIT License | 6 votes |
def test_can_merge_default_headers_with_headers_passed_on_the_request_real_request( self ): """ O aioresponses não funciona quando usamos default_headers no ClientSession. """ default_headers = {"X-Header": "Value"} additional_headers = {"X-More": "Other"} client = HttpClient(headers=default_headers) resp = await client.get( "https://httpbin.org/headers", headers=additional_headers ) self.assertEqual(HTTPStatus.OK, resp.status) resp_data = await resp.json() self.assertEqual("Value", resp_data["headers"].get("X-Header")) self.assertEqual("Other", resp_data["headers"].get("X-More"))
Example #14
Source File: test_agent_services.py From asgard-api with MIT License | 6 votes |
def test_get_apps_running_for_agent_mesos_orchestrator_zero_apps( self ): async with HttpClientContext(app) as client: local_address = ( f"http://{client._server.host}:{client._server.port}" ) with aioresponses(passthrough=[local_address]) as rsps: agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S44" build_mesos_cluster(rsps, agent_id) agent = await self.agents_service.get_agent_by_id( agent_id, self.user, self.account, self.mesos_orchestrator ) apps = await self.agents_service.get_apps_running_for_agent( self.user, agent, self.mesos_orchestrator ) self.assertEquals([], apps)
Example #15
Source File: test_agent_services.py From asgard-api with MIT License | 6 votes |
def test_get_apps_running_for_agent_mesos_orchestrator_some_apps( self ): async with HttpClientContext(app) as client: local_address = ( f"http://{client._server.host}:{client._server.port}" ) with aioresponses(passthrough=[local_address]) as rsps: agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S9" build_mesos_cluster(rsps, agent_id) self.account.owner = "asgard" agent = await self.agents_service.get_agent_by_id( agent_id, self.user, self.account, self.mesos_orchestrator ) apps = await self.agents_service.get_apps_running_for_agent( self.user, agent, self.mesos_orchestrator ) self.assertEquals(1, len(apps))
Example #16
Source File: test_marathon_apps_backend.py From asgard-api with MIT License | 6 votes |
def test_get_app_stats_has_some_data(self): with aioresponses() as rsps: agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S0" build_mesos_cluster(rsps, agent_id) add_agent_task_stats( rsps, agent_id, index_name="asgard-app-stats-2019-03-29-*" ) stats = await self.apps_backend.get_app_stats( MesosApp(id="infra-asgard-api"), self.interval, self.user, self.account, ) self.assertEqual( AppStats(cpu_pct="4.51", ram_pct="22.68", cpu_thr_pct="2.89"), stats, )
Example #17
Source File: test_mesos_orchestrator.py From asgard-api with MIT License | 6 votes |
def test_get_apps_returns_empty_list_if_no_apps_running_on_agent( self ): agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S4" slave = get_fixture(f"agents/{agent_id}/info.json") slave_id = slave["id"] self.account.owner = slave["attributes"]["owner"] with aioresponses() as rsps: build_mesos_cluster(rsps, agent_id) agent = await self.mesos_backend.get_agent_by_id( slave_id, self.user, self.account ) apps = await self.mesos_backend.get_apps_running_for_agent( self.user, agent ) self.assertEqual(0, len(apps))
Example #18
Source File: test_mesos_orchestrator.py From asgard-api with MIT License | 6 votes |
def test_get_apps_returns_empty_list_if_agent_not_found(self): slave_id = "39e1a8e3-0fd1-4ba6-981d-e01318944957-S2" with aioresponses() as rsps: rsps.get( f"{settings.MESOS_API_URLS[0]}/redirect", status=301, headers={"Location": settings.MESOS_API_URLS[0]}, ) rsps.get( f"{settings.MESOS_API_URLS[0]}/slaves?slave_id={slave_id}", payload={"slaves": []}, status=200, ) agent = await self.mesos_backend.get_agent_by_id( slave_id, self.user, self.account ) apps = await self.mesos_backend.get_apps_running_for_agent( self.user, agent ) self.assertEqual(0, len(apps))
Example #19
Source File: test_mesos_orchestrator.py From asgard-api with MIT License | 6 votes |
def test_get_agent_by_id_return_None_if_agent_not_found(self): slave_id = "39e1a8e3-0fd1-4ba6-981d-e01318944957-S2" with aioresponses() as rsps: rsps.get( f"{settings.MESOS_API_URLS[0]}/redirect", status=301, headers={"Location": settings.MESOS_API_URLS[0]}, ) rsps.get( f"{settings.MESOS_API_URLS[0]}/slaves?slave_id={slave_id}", payload={"slaves": []}, status=200, ) agent = await self.mesos_backend.get_agent_by_id( slave_id, # Agent from asgard-infra namespace self.user, self.account, ) self.assertIsNone(agent)
Example #20
Source File: test_mesos_orchestrator.py From asgard-api with MIT License | 5 votes |
def test_get_apps_returns_apps_running_on_agent(self): agent_id = "ead07ffb-5a61-42c9-9386-21b680597e6c-S0" slave = get_fixture(f"agents/{agent_id}/info.json") slave_id = slave["id"] slave_owner = slave["attributes"]["owner"] self.account.owner = slave_owner with aioresponses() as rsps: build_mesos_cluster(rsps, agent_id) agent = await self.mesos_backend.get_agent_by_id( slave_id, self.user, self.account ) apps = await self.mesos_backend.get_apps_running_for_agent( self.user, agent ) self.assertEqual(5, len(apps)) expected_app_ids = sorted( [ "captura/wetl/visitcentral", "portal/api", "captura/kirby/feeder", "infra/asgard/api", "infra/rabbitmq", ] ) self.assertEqual(expected_app_ids, sorted([app.id for app in apps]))
Example #21
Source File: conftest.py From yui with GNU Affero General Public License v3.0 | 5 votes |
def response_mock(): with aioresponses.aioresponses() as m: yield m
Example #22
Source File: test_fetch_apps_data.py From asgard-api with MIT License | 5 votes |
def test_get_all_apps_data_no_data_found(self): scaler = AsgardInterface() with aioresponses() as rsps: rsps.get( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"apps": []}, ) apps = await scaler.fetch_all_apps() self.assertEqual([], apps)
Example #23
Source File: test_fetch_apps_labels.py From asgard-api with MIT License | 5 votes |
def test_all_limits_are_defined(self): interface = AsgardInterface() with aioresponses() as rsps: rsps.get( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={ "apps": [ { "id": "/test_app", "mem": "0.2", "cpus": "0.5", "labels": { "asgard.autoscale.cpu": "0.2", "asgard.autoscale.mem": "0.2", "asgard.autoscale.min_cpu_limit": "0.2", "asgard.autoscale.max_cpu_limit": "2", "asgard.autoscale.min_mem_limit": "0.2", "asgard.autoscale.max_mem_limit": "2", }, } ] }, ) apps = await interface.fetch_all_apps() self.assertEqual(1, len(apps)) self.assertEqual(0.2, apps[0].min_cpu_scale_limit) self.assertEqual(2, apps[0].max_cpu_scale_limit) self.assertEqual(0.2, apps[0].min_mem_scale_limit) self.assertEqual(2, apps[0].max_mem_scale_limit)
Example #24
Source File: test_mesos_models.py From asgard-api with MIT License | 5 votes |
def test_tasks_list_from_one_app_no_tasks_running_on_agent(self): with aioresponses() as rsps: build_mesos_cluster(rsps, self.agent_id) app_id = "apps/http/myapp" tasks = await self.agent.tasks(app_id=app_id) self.assertEqual(0, len(tasks))
Example #25
Source File: test_state_checker.py From asgard-api with MIT License | 5 votes |
def test_get_scalable_apps_stats_no_scalable_apps(self): state_checker = PeriodicStateChecker(AsgardCloudInterface()) with aioresponses() as rsps: rsps.get( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"apps": []}, ) scalable_apps = await state_checker.get_scalable_apps_stats() self.assertEqual(0, len(scalable_apps))
Example #26
Source File: test_scale_apps.py From asgard-api with MIT License | 5 votes |
def test_mem_decision_is_rounded_to_0_decimal_places(self): interface = AsgardInterface() decisions = [Decision("test", mem=64.26707)] body_fixture = [{"id": "test", "mem": 64}] headers_fixture = { "Content-Type": "application/json", "Authorization": f"Token {settings.AUTOSCALER_AUTH_TOKEN}", } with aioresponses() as rsps: rsps.put( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"deploymentId": "test", "version": "1.0"}, ) await interface.apply_decisions(decisions) calls = rsps.requests.get( ("put", URL(f"{settings.ASGARD_API_ADDRESS}/v2/apps")) ) self.assertIsNotNone(calls) self.assertEqual(body_fixture, calls[0].kwargs.get("json")) self.assertEqual( headers_fixture, interface._asgard_client._http_client.default_headers, )
Example #27
Source File: test_http_client.py From asgard-api with MIT License | 5 votes |
def test_reuse_session_on_subsequent_requests(self): client = HttpClient() with aioresponses() as rsps: rsps.get(TEST_URL, status=200) rsps.get(TEST_URL, status=200) await client.get(TEST_URL) client_session = client._session await client.get(TEST_URL) self.assertTrue(client_session is client._session)
Example #28
Source File: test_scale_apps.py From asgard-api with MIT License | 5 votes |
def test_http_request_is_sent_with_correct_parameters(self): interface = AsgardInterface() decisions = [Decision("test", mem=64, cpu=0.4)] body_fixture = [{"id": "test", "mem": 64, "cpus": 0.4}] headers_fixture = { "Content-Type": "application/json", "Authorization": f"Token {settings.AUTOSCALER_AUTH_TOKEN}", } with aioresponses() as rsps: rsps.put( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"deploymentId": "test", "version": "1.0"}, ) await interface.apply_decisions(decisions) calls = rsps.requests.get( ("put", URL(f"{settings.ASGARD_API_ADDRESS}/v2/apps")) ) self.assertIsNotNone(calls) self.assertEqual(body_fixture, calls[0].kwargs.get("json")) self.assertEqual( headers_fixture, interface._asgard_client._http_client.default_headers, )
Example #29
Source File: test_mesos_client.py From asgard-api with MIT License | 5 votes |
def test_mesos_client_get_agents_empty_cluster(self): """ Retorna a lista de agents do cluster mesos """ async with MesosClient(*settings.MESOS_API_URLS) as mesos: with aioresponses() as rsps: build_mesos_cluster(rsps) agents = await mesos.get_agents() self.assertEquals(0, len(agents))
Example #30
Source File: test_scale_apps.py From asgard-api with MIT License | 5 votes |
def test_cpu_decision_is_rounded_to_3_decimal_places(self): interface = AsgardInterface() decisions = [Decision("test", cpu=0.436_721_072_367)] body_fixture = [{"id": "test", "cpus": 0.437}] headers_fixture = { "Content-Type": "application/json", "Authorization": f"Token {settings.AUTOSCALER_AUTH_TOKEN}", } with aioresponses() as rsps: rsps.put( f"{settings.ASGARD_API_ADDRESS}/v2/apps", status=200, payload={"deploymentId": "test", "version": "1.0"}, ) await interface.apply_decisions(decisions) calls = rsps.requests.get( ("put", URL(f"{settings.ASGARD_API_ADDRESS}/v2/apps")) ) self.assertIsNotNone(calls) self.assertEqual(body_fixture, calls[0].kwargs.get("json")) self.assertEqual( headers_fixture, interface._asgard_client._http_client.default_headers, )