Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a api_request based fixture #348

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading