Skip to content

Commit

Permalink
migrated to different http mock library
Browse files Browse the repository at this point in the history
  • Loading branch information
exxamalte committed Jan 9, 2024
1 parent eb5e5b0 commit 33d7341
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 74 deletions.
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pytest-timeout
pytest-xdist
pytest-cov
mock
aresponses
aioresponses
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Configuration for tests."""
import pytest
from aioresponses import aioresponses


@pytest.fixture
def mock_aioresponse():
"""Return aioresponse fixture."""
with aioresponses() as m:
yield m
77 changes: 36 additions & 41 deletions tests/test_feed.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test for the generic geojson feed."""
import asyncio
from http import HTTPStatus
from unittest.mock import MagicMock

import aiohttp
Expand All @@ -15,14 +16,13 @@


@pytest.mark.asyncio
async def test_update_ok(aresponses):
async def test_update_ok(mock_aioresponse):
"""Test updating feed is ok."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand Down Expand Up @@ -66,14 +66,13 @@ async def test_update_ok(aresponses):


@pytest.mark.asyncio
async def test_update_ok_with_filtering(aresponses):
async def test_update_ok_with_filtering(mock_aioresponse):
"""Test updating feed is ok."""
home_coordinates = (-37.0, 150.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand All @@ -90,14 +89,13 @@ async def test_update_ok_with_filtering(aresponses):


@pytest.mark.asyncio
async def test_update_ok_with_filter_override(aresponses):
async def test_update_ok_with_filter_override(mock_aioresponse):
"""Test updating feed is ok."""
home_coordinates = (-37.0, 150.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand All @@ -116,14 +114,13 @@ async def test_update_ok_with_filter_override(aresponses):


@pytest.mark.asyncio
async def test_update_geometries(aresponses):
async def test_update_geometries(mock_aioresponse):
"""Test updating feed is ok."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_3.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_3.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand Down Expand Up @@ -179,10 +176,10 @@ async def test_update_with_client_exception():


@pytest.mark.asyncio
async def test_update_with_request_exception(aresponses):
async def test_update_with_request_exception(mock_aioresponse):
"""Test updating feed results in error."""
home_coordinates = (-31.0, 151.0)
aresponses.add("test.url", "/badpath", "get", aresponses.Response(status=404))
mock_aioresponse.get("http://test.url/testpath", status=HTTPStatus.NOT_FOUND)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
feed = MockGeoJsonFeed(websession, home_coordinates, "http://test.url/badpath")
Expand All @@ -192,11 +189,11 @@ async def test_update_with_request_exception(aresponses):


@pytest.mark.asyncio
async def test_update_with_json_decode_error(aresponses):
async def test_update_with_json_decode_error(mock_aioresponse):
"""Test updating feed raises exception."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url", "/badjson", "get", aresponses.Response(text="NOT JSON", status=200)
mock_aioresponse.get(
"http://test.url/badjson", status=HTTPStatus.OK, body="NOT JSON"
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand All @@ -223,14 +220,13 @@ async def test_update_with_timeout_error():


@pytest.mark.asyncio
async def test_update_ok_feed_feature(aresponses):
async def test_update_ok_feed_feature(mock_aioresponse):
"""Test updating feed is ok."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_4.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_4.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand All @@ -254,14 +250,13 @@ async def test_update_ok_feed_feature(aresponses):


@pytest.mark.asyncio
async def test_unsupported_object(aresponses, caplog):
async def test_unsupported_object(mock_aioresponse, caplog):
"""Test updating feed is ok."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_5.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_5.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand Down
55 changes: 23 additions & 32 deletions tests/test_feed_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test for the generic geojson feed manager."""
import asyncio
from http import HTTPStatus

import aiohttp
import mock as async_mock
Expand All @@ -13,14 +14,13 @@


@pytest.mark.asyncio
async def test_feed_manager(aresponses):
async def test_feed_manager(mock_aioresponse):
"""Test the feed manager."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand Down Expand Up @@ -92,11 +92,10 @@ async def _remove_entity(external_id):
updated_entity_external_ids.clear()
removed_entity_external_ids.clear()

aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_2.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_2.json"),
)

await feed_manager.update()
Expand Down Expand Up @@ -140,11 +139,8 @@ async def _remove_entity(external_id):
updated_entity_external_ids.clear()
removed_entity_external_ids.clear()

aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(status=500),
mock_aioresponse.get(
"http://test.url/testpath", status=HTTPStatus.INTERNAL_SERVER_ERROR
)

await feed_manager.update()
Expand All @@ -160,11 +156,10 @@ async def _remove_entity(external_id):
updated_entity_external_ids.clear()
removed_entity_external_ids.clear()

aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)
await feed_manager.update_override(
filter_overrides=GeoJsonFeedFilterDefinition(radius=750.0)
Expand All @@ -177,14 +172,13 @@ async def _remove_entity(external_id):


@pytest.mark.asyncio
async def test_feed_manager_with_status_callback(aresponses):
async def test_feed_manager_with_status_callback(mock_aioresponse):
"""Test the feed manager."""
home_coordinates = (-31.0, 151.0)
aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(text=load_fixture("generic_feed_1.json"), status=200),
mock_aioresponse.get(
"http://test.url/testpath",
status=HTTPStatus.OK,
body=load_fixture("generic_feed_1.json"),
)

async with aiohttp.ClientSession(loop=asyncio.get_running_loop()) as websession:
Expand Down Expand Up @@ -252,11 +246,8 @@ async def _status(status_details):
removed_entity_external_ids.clear()
status_update.clear()

aresponses.add(
"test.url",
"/testpath",
"get",
aresponses.Response(status=500),
mock_aioresponse.get(
"http://test.url/testpath", status=HTTPStatus.INTERNAL_SERVER_ERROR
)

await feed_manager.update()
Expand Down

0 comments on commit 33d7341

Please sign in to comment.