Skip to content

Commit

Permalink
TST: absorb fixture from pytest-aiohttp due to conda-forge availability
Browse files Browse the repository at this point in the history
  • Loading branch information
klauer committed Aug 31, 2023
1 parent 816e2eb commit fe01c4e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
1 change: 0 additions & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ test:
- happi
- pytest
- pytest-asyncio
- pytest-aiohttp
- pytest-cov
- python-ldap
- pytmc
Expand Down
1 change: 0 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ codecov
flake8
pytest
pytest-asyncio
pytest-aiohttp
pytest-cov

# plugin dependencies
Expand Down
46 changes: 45 additions & 1 deletion whatrecord/tests/test_server_handler.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""Requires pytest-aiohttp"""
import json
import logging
from typing import Any, Dict, Optional, Type, TypeVar
from typing import (Any, Awaitable, Callable, Dict, Generator, Optional, Type,
TypeVar, Union)

import aiohttp
import aiohttp.test_utils
import aiohttp.web
import apischema
import pytest
import pytest_asyncio
from aiohttp.test_utils import BaseTestServer, TestClient, TestServer
from aiohttp.web import Application

from .. import gateway
from ..common import RecordInstance, WhatRecord
Expand All @@ -22,6 +25,8 @@

from .test_server_state import ready_state, state # noqa

AiohttpClient = Callable[[Union[Application, BaseTestServer]], Awaitable[TestClient]]


@pytest.fixture()
def handler(ready_state: ServerState) -> ServerHandler: # noqa: F811
Expand All @@ -37,6 +42,45 @@ def server(handler: ServerHandler) -> aiohttp.web.Application:
return app


@pytest_asyncio.fixture
async def aiohttp_client() -> Generator[AiohttpClient, None, None]:
"""
Factory to create a TestClient instance.
Borrowed and modified from pytest-aiohttp, as a recent version is
unavailable on conda-forge for our testing purposes.
aiohttp_client(app, **kwargs)
aiohttp_client(server, **kwargs)
aiohttp_client(raw_server, **kwargs)
"""
clients = []

async def go(
__param: Union[Application, BaseTestServer],
*,
server_kwargs: Optional[Dict[str, Any]] = None,
**kwargs: Any,
) -> TestClient:
if isinstance(__param, Application):
server_kwargs = server_kwargs or {}
server = TestServer(__param, **server_kwargs)
client = TestClient(server, **kwargs)
elif isinstance(__param, BaseTestServer):
client = TestClient(__param, **kwargs)
else:
raise ValueError(f"Unknown argument type: {type(__param)}")

await client.start_server()
clients.append(client)
return client

yield go

while clients:
await clients.pop().close()


@pytest_asyncio.fixture()
async def client(server: aiohttp.web.Application, aiohttp_client):
return await aiohttp_client(server)
Expand Down

0 comments on commit fe01c4e

Please sign in to comment.