Skip to content

Commit

Permalink
Merge pull request #176 from klauer/dev_conda
Browse files Browse the repository at this point in the history
BLD: epicsmacrolib is now on conda-forge
  • Loading branch information
klauer committed Aug 31, 2023
2 parents ab9bbd4 + fe01c4e commit 71200d9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ requirements:
- python >=3.9
- aiohttp
- apischema
- epicsmacrolib
- graphviz
- jinja2
- lark-parser
Expand All @@ -35,7 +36,7 @@ test:
- flake8
- happi
- pytest
- pytest-aiohttp
- pytest-asyncio
- pytest-cov
- python-ldap
- pytmc
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
codecov
flake8
pytest
pytest-aiohttp
pytest-asyncio
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 71200d9

Please sign in to comment.