veilid/veilid-python/tests/__init__.py

38 lines
969 B
Python
Raw Normal View History

2023-06-16 00:22:54 +00:00
import os
2023-06-17 18:34:09 +00:00
from functools import cache
from typing import AsyncGenerator
2023-06-15 01:06:10 +00:00
2023-06-17 18:34:09 +00:00
import pytest_asyncio
2023-06-16 00:22:54 +00:00
import veilid
2023-06-17 18:34:09 +00:00
from veilid.json_api import _JsonVeilidAPI
2023-06-16 00:22:54 +00:00
2023-06-17 18:34:09 +00:00
pytest_plugins = ("pytest_asyncio",)
2023-06-15 01:06:10 +00:00
2023-06-17 18:34:09 +00:00
@cache
def server_info() -> tuple[str, int]:
"""Return the hostname and port of the test server."""
VEILID_SERVER = os.getenv("VEILID_SERVER")
if VEILID_SERVER is None:
return "localhost", 5959
2023-06-15 01:06:10 +00:00
2023-06-17 18:34:09 +00:00
hostname, *rest = VEILID_SERVER.split(":")
if rest:
return hostname, int(rest[0])
return hostname, 5959
async def simple_update_callback(update: veilid.VeilidUpdate):
print(f"VeilidUpdate: {update}")
@pytest_asyncio.fixture
async def api_connection() -> AsyncGenerator[_JsonVeilidAPI, None]:
hostname, port = server_info()
api = await veilid.json_api_connect(hostname, port, simple_update_callback)
2023-06-16 00:22:54 +00:00
async with api:
2023-06-16 17:14:34 +00:00
# purge routes to ensure we start fresh
await api.debug("purge routes")
2023-06-16 00:22:54 +00:00
2023-06-17 18:34:09 +00:00
yield api