Skip to content

Commit

Permalink
✅ add tests for AsyncClient
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidRomanovizc committed Apr 15, 2024
1 parent de106ff commit efab869
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 10 deletions.
35 changes: 27 additions & 8 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,42 @@

import pytest
import requests_mock
from aioresponses import aioresponses


@pytest.fixture
def mock_api() -> typing.Any:
def _encode(geocode: str, api_key: str = "well-known-key") -> str:
params = {"format": "json", "apikey": api_key, "geocode": geocode}
query = urlencode(params)
return f"https://geocode-maps.yandex.ru/1.x/?{query}"
def _encode(geocode: str, api_key: str = "well-known-key") -> str:
params = {"format": "json", "apikey": api_key, "geocode": geocode}
query = urlencode(params)
return f"https://geocode-maps.yandex.ru/1.x/?{query}"

with requests_mock.mock() as _m:
yield lambda resp, status, **encode_kw: _m.get(

def _mock_setup(mock: typing.Any, resp: str, status: str, **encode_kw: typing.Any) -> typing.Any:
if isinstance(mock, aioresponses):
return mock.get(
_encode(**encode_kw),
payload=load_fixture(resp) if isinstance(resp, str) else resp,
status=status,
)
else:
return mock.get(
_encode(**encode_kw),
json=load_fixture(resp) if isinstance(resp, str) else resp,
status_code=status,
)


@pytest.fixture
def mock_api() -> typing.Any:
with requests_mock.mock() as _m:
yield lambda resp, status, **encode_kw: _mock_setup(_m, resp, status, **encode_kw)


@pytest.fixture
def async_mock_api() -> typing.Any:
with aioresponses() as _m:
yield lambda resp, status, **encode_kw: _mock_setup(_m, resp, status, **encode_kw)


def load_fixture(fixture_name: str) -> dict[str, typing.Any]:
with open(f"./tests/fixtures/{fixture_name}.json") as fixture:
return json.load(fixture) # type: ignore
Expand Down
45 changes: 44 additions & 1 deletion tests/test_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from yandex_geocoder import Client, InvalidKey, NothingFound, UnexpectedResponse
from yandex_geocoder import AsyncClient, Client, InvalidKey, NothingFound, UnexpectedResponse


def test_returns_found_address(mock_api: typing.Any) -> None:
Expand All @@ -16,6 +16,15 @@ def test_returns_found_address(mock_api: typing.Any) -> None:
)


@pytest.mark.asyncio
async def test_returns_found_address_async(async_mock_api: typing.Any) -> None:
async_mock_api("address_found", 200, geocode="37.587093,55.733969")
client = AsyncClient("well-known-key")

resp = await client.address(Decimal("37.587093"), Decimal("55.733969"))
assert resp == "Россия, Москва, улица Льва Толстого, 16"


def test_raises_if_address_not_found(mock_api: typing.Any) -> None:
mock_api("address_not_found", 200, geocode="337.587093,55.733969")
client = Client("well-known-key")
Expand All @@ -24,6 +33,15 @@ def test_raises_if_address_not_found(mock_api: typing.Any) -> None:
client.address(Decimal("337.587093"), Decimal("55.733969"))


@pytest.mark.asyncio
async def test_raises_if_address_not_found_async(async_mock_api: typing.Any) -> None:
async_mock_api("address_not_found", 200, geocode="337.587093,55.733969")
client = AsyncClient("well-known-key")

with pytest.raises(NothingFound, match='Nothing found for "337.587093 55.733969"'):
await client.address(Decimal("337.587093"), Decimal("55.733969"))


def test_raises_for_invalid_api_key(mock_api: typing.Any) -> None:
mock_api(
{"statusCode": 403, "error": "Forbidden", "message": "Invalid key"},
Expand All @@ -37,6 +55,20 @@ def test_raises_for_invalid_api_key(mock_api: typing.Any) -> None:
client.address(Decimal("37.587093"), Decimal("55.733969"))


@pytest.mark.asyncio
async def test_raises_for_invalid_api_key_async(async_mock_api: typing.Any) -> None:
async_mock_api(
{"statusCode": 403, "error": "Forbidden", "message": "Invalid key"},
403,
geocode="37.587093,55.733969",
api_key="unkown-api-key",
)
client = AsyncClient("unkown-api-key")

with pytest.raises(InvalidKey):
await client.address(Decimal("37.587093"), Decimal("55.733969"))


def test_raises_for_unknown_response(mock_api: typing.Any) -> None:
mock_api({}, 500, geocode="37.587093,55.733969")
client = Client("well-known-key")
Expand All @@ -45,3 +77,14 @@ def test_raises_for_unknown_response(mock_api: typing.Any) -> None:
client.address(Decimal("37.587093"), Decimal("55.733969"))

assert "status_code=500, body=b'{}'" in exc_info.value.args


@pytest.mark.asyncio
async def test_raises_for_unknown_response_async(async_mock_api: typing.Any) -> None:
async_mock_api({}, 500, geocode="37.587093,55.733969")
client = AsyncClient("well-known-key")

with pytest.raises(UnexpectedResponse) as exc_info:
await client.address(Decimal("37.587093"), Decimal("55.733969"))

assert "status_code=500, body=b'{}'" in exc_info.value.args
47 changes: 46 additions & 1 deletion tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from yandex_geocoder import Client, InvalidKey, NothingFound, UnexpectedResponse
from yandex_geocoder import AsyncClient, Client, InvalidKey, NothingFound, UnexpectedResponse


def test_returns_found_coordinates(mock_api: typing.Any) -> None:
Expand All @@ -16,6 +16,17 @@ def test_returns_found_coordinates(mock_api: typing.Any) -> None:
)


@pytest.mark.asyncio
async def test_returns_found_coordinates_async(async_mock_api: typing.Any) -> None:
async_mock_api("coords_found", 200, geocode="Москва Льва Толстого 16")
client = AsyncClient("well-known-key")

assert await client.coordinates("Москва Льва Толстого 16") == (
Decimal("37.587093"),
Decimal("55.733969"),
)


def test_raises_if_coordinates_not_found(mock_api: typing.Any) -> None:
mock_api("coords_not_found", 200, geocode="абырвалг")
client = Client("well-known-key")
Expand All @@ -24,6 +35,15 @@ def test_raises_if_coordinates_not_found(mock_api: typing.Any) -> None:
client.coordinates("абырвалг")


@pytest.mark.asyncio
async def test_raises_if_coordinates_not_found_async(async_mock_api: typing.Any) -> None:
async_mock_api("coords_not_found", 200, geocode="абырвалг")
client = AsyncClient("well-known-key")

with pytest.raises(NothingFound, match='Nothing found for "абырвалг"'):
await client.coordinates("абырвалг")


def test_raises_for_invalid_api_key(mock_api: typing.Any) -> None:
mock_api(
{"statusCode": 403, "error": "Forbidden", "message": "Invalid key"},
Expand All @@ -37,6 +57,20 @@ def test_raises_for_invalid_api_key(mock_api: typing.Any) -> None:
client.coordinates("Москва Льва Толстого 16")


@pytest.mark.asyncio
async def test_raises_for_invalid_api_key_async(async_mock_api: typing.Any) -> None:
async_mock_api(
{"statusCode": 403, "error": "Forbidden", "message": "Invalid key"},
403,
geocode="Москва Льва Толстого 16",
api_key="unkown-api-key",
)
client = AsyncClient("unkown-api-key")

with pytest.raises(InvalidKey):
await client.coordinates("Москва Льва Толстого 16")


def test_raises_for_unknown_response(mock_api: typing.Any) -> None:
mock_api({}, 500, geocode="Москва Льва Толстого 16")
client = Client("well-known-key")
Expand All @@ -45,3 +79,14 @@ def test_raises_for_unknown_response(mock_api: typing.Any) -> None:
client.coordinates("Москва Льва Толстого 16")

assert "status_code=500, body=b'{}'" in exc_info.value.args


@pytest.mark.asyncio
async def test_raises_for_unknown_response_async(async_mock_api: typing.Any) -> None:
async_mock_api({}, 500, geocode="Москва Льва Толстого 16")
client = AsyncClient("well-known-key")

with pytest.raises(UnexpectedResponse) as exc_info:
await client.coordinates("Москва Льва Толстого 16")

assert "status_code=500, body=b'{}'" in exc_info.value.args

0 comments on commit efab869

Please sign in to comment.