nvchecker/tests/conftest.py

53 lines
1.4 KiB
Python
Raw Normal View History

2017-07-04 09:04:29 +00:00
import configparser
import pytest
import asyncio
2018-05-08 10:18:10 +00:00
import io
2017-07-04 09:04:29 +00:00
from nvchecker.get_version import get_version as _get_version
2018-05-08 10:18:10 +00:00
from nvchecker.core import Source
class TestSource(Source):
def __init__(self, future, *args, **kwargs):
super().__init__(*args, **kwargs)
self._future = future
def on_update(self, name, version, oldver):
self._future.set_result(version)
def on_exception(self, name, exc):
self._future.set_exception(exc)
@pytest.fixture(scope="module")
async def run_source():
async def __call__(conf):
future = asyncio.Future()
file = io.StringIO(conf)
file.name = '<StringIO>'
s = TestSource(future, file)
await s.check()
return await future
return __call__
2017-07-04 09:04:29 +00:00
@pytest.fixture(scope="module")
async def get_version():
async def __call__(name, config):
if isinstance(config, dict):
_config = configparser.ConfigParser(dict_type=dict, allow_no_value=True)
_config.read_dict({name: config})
config = _config[name]
return await _get_version(name, config)
2017-07-04 09:04:29 +00:00
return __call__
2017-12-22 06:15:05 +00:00
@pytest.fixture(scope="module")
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.
"""
loop = asyncio.get_event_loop()
yield loop