nvchecker/tests/conftest.py

90 lines
2.3 KiB
Python
Raw Normal View History

2020-08-11 09:43:03 +00:00
# MIT licensed
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
2017-07-04 09:04:29 +00:00
import asyncio
import structlog
2020-08-17 07:11:14 +00:00
from typing import Optional
2017-07-04 09:04:29 +00:00
2020-08-17 07:11:14 +00:00
import toml
import pytest
2018-05-08 10:18:10 +00:00
2020-08-17 07:11:14 +00:00
from nvchecker import core
from nvchecker import __main__ as main
from nvchecker.util import Entries, VersData, RawResult
async def run(
entries: Entries, max_concurrency: int = 20,
keys_toml: Optional[str] = None,
) -> VersData:
token_q = core.token_queue(max_concurrency)
result_q: asyncio.Queue[RawResult] = asyncio.Queue()
if keys_toml:
keymanager = core.KeyManager.from_str(keys_toml)
else:
keymanager = core.KeyManager(None)
futures = core.dispatch(
entries, token_q, result_q,
keymanager, 1,
)
2018-05-08 10:18:10 +00:00
2020-08-17 07:11:14 +00:00
oldvers: VersData = {}
result_coro = core.process_result(oldvers, result_q)
runner_coro = core.run_tasks(futures)
2020-08-17 07:11:14 +00:00
return await main.run(result_coro, runner_coro)
2018-05-08 10:18:10 +00:00
@pytest.fixture(scope="module")
2020-08-17 07:11:14 +00:00
async def get_version():
async def __call__(name, config):
entries = {name: config}
newvers = await run(entries)
2020-08-17 08:21:02 +00:00
return newvers.get(name)
2018-05-08 10:18:10 +00:00
return __call__
2017-07-04 09:04:29 +00:00
@pytest.fixture(scope="module")
2020-08-17 07:11:14 +00:00
async def run_str():
async def __call__(str):
entries = toml.loads(str)
newvers = await run(entries)
return newvers.popitem()[1]
2017-07-04 09:04:29 +00:00
2020-08-17 07:11:14 +00:00
return __call__
2017-07-04 09:04:29 +00:00
2020-08-17 07:11:14 +00:00
@pytest.fixture(scope="module")
async def run_str_multi():
async def __call__(str):
entries = toml.loads(str)
newvers = await run(entries)
return newvers
2017-07-04 09:04:29 +00:00
return __call__
2017-07-04 09:04:29 +00:00
@pytest.fixture(scope="session")
2017-07-04 09:04:29 +00:00
def event_loop(request):
"""Override pytest-asyncio's event_loop fixture,
Don't create an instance of the default event loop for each test case.
We need the same ioloop across tests for the aiohttp support.
"""
loop = asyncio.get_event_loop()
yield loop
@pytest.fixture(scope="session", autouse=True)
def raise_on_logger_msg():
def proc(logger, method_name, event_dict):
if method_name in ('warning', 'error'):
2019-03-07 08:45:14 +00:00
if 'exc_info' in event_dict:
raise event_dict['exc_info']
if not event_dict['event'].startswith(('rate limited', 'no-result')):
raise RuntimeError(event_dict['event'])
return event_dict['event']
structlog.configure([proc])
def pytest_configure(config):
# register an additional marker
config.addinivalue_line(
'markers', 'needs_net: mark test to require Internet access',
)