Skip to content

Commit

Permalink
Create a api_request based fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 committed Feb 18, 2024
1 parent 4511536 commit 643110c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import asyncio
from collections import deque
from collections.abc import Callable
import logging
from typing import Any

from httpx import AsyncClient
import pytest
import respx

from axis.configuration import Configuration
from axis.device import AxisDevice
from axis.vapix.models.api import ApiRequest

LOGGER = logging.getLogger(__name__)

Expand All @@ -32,6 +35,27 @@ async def axis_device(respx_mock: respx.router.MockRouter) -> AxisDevice:
await session.aclose()


@pytest.fixture(name="mock_api_request")
def api_request_fixture(
respx_mock: respx.router.MockRouter,
) -> Callable[[ApiRequest, Any], respx.router.MockRouter]:
"""Mock API request."""
content_type_to_keyword = {
"application/json": "json",
}

def _register_route(
api_request: ApiRequest, response_data: Any
) -> respx.router.MockRouter:
kwargs = {content_type_to_keyword[api_request.content_type]: response_data}
return respx_mock.request(
method=api_request.method,
url=api_request.path,
).respond(**kwargs)

return _register_route


class TcpServerProtocol(asyncio.Protocol):
"""Simple socket server that responds with preset responses."""

Expand Down
21 changes: 13 additions & 8 deletions tests/test_api_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

from axis.device import AxisDevice
from axis.vapix.interfaces.api_discovery import ApiDiscoveryHandler
from axis.vapix.models.api_discovery import ApiId, ApiStatus
from axis.vapix.models.api_discovery import (
ApiId,
ApiStatus,
GetSupportedVersionsRequest,
ListApisRequest,
)


@pytest.fixture
Expand All @@ -28,11 +33,9 @@ async def test_api_status_enum():
assert ApiStatus("unsupported") is ApiStatus.UNKNOWN


async def test_get_api_list(respx_mock, api_discovery: ApiDiscoveryHandler):
async def test_get_api_list(mock_api_request, api_discovery: ApiDiscoveryHandler):
"""Test get_api_list call."""
route = respx_mock.post("/axis-cgi/apidiscovery.cgi").respond(
json=GET_API_LIST_RESPONSE,
)
route = mock_api_request(ListApisRequest, GET_API_LIST_RESPONSE)
assert api_discovery.supported
await api_discovery.update()

Expand All @@ -56,10 +59,12 @@ async def test_get_api_list(respx_mock, api_discovery: ApiDiscoveryHandler):
assert item.version == "1.0"


async def test_get_supported_versions(respx_mock, api_discovery: ApiDiscoveryHandler):
async def test_get_supported_versions(
mock_api_request, api_discovery: ApiDiscoveryHandler
):
"""Test get_supported_versions."""
route = respx_mock.post("/axis-cgi/apidiscovery.cgi").respond(
json=GET_SUPPORTED_VERSIONS_RESPONSE,
route = mock_api_request(
GetSupportedVersionsRequest, GET_SUPPORTED_VERSIONS_RESPONSE
)
response = await api_discovery.get_supported_versions()

Expand Down

0 comments on commit 643110c

Please sign in to comment.