|
| 1 | +import importlib |
1 | 2 | import os |
2 | 3 | from typing import cast |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 | from aiohttp import BasicAuth, ClientSession, web |
6 | 7 | from yarl import URL |
7 | 8 |
|
8 | | -from main import create_app as _create_app |
9 | | - |
10 | 9 | from . import app_keys as ak |
11 | 10 |
|
12 | 11 |
|
13 | | -async def start_app(aiohttp_client): |
14 | | - app = await aiohttp_client(_create_app(), server_kwargs={"host": "0.0.0.0", "port": 8081}) |
| 12 | +def pytest_addoption(parser): |
| 13 | + parser.addini( |
| 14 | + "aidbox_create_app", |
| 15 | + "Dotted path to the create_app callable (module:name or module.name), e.g. main:create_app", |
| 16 | + default="main:create_app", |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +def _load_create_app(path: str): |
| 21 | + """Import and return the create_app callable from the given dotted path.""" |
| 22 | + if ":" in path: |
| 23 | + module_path, attr = path.split(":", 1) |
| 24 | + else: |
| 25 | + module_path, attr = path.rsplit(".", 1) |
| 26 | + mod = importlib.import_module(module_path) |
| 27 | + return getattr(mod, attr) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="session") |
| 31 | +def create_app(request): |
| 32 | + """App factory; override in conftest or set pytest ini option aidbox_create_app.""" |
| 33 | + path = request.config.getini("aidbox_create_app") |
| 34 | + return _load_create_app(path) |
| 35 | + |
| 36 | + |
| 37 | +async def start_app(aiohttp_client, create_app): |
| 38 | + app = await aiohttp_client(create_app(), server_kwargs={"host": "0.0.0.0", "port": 8081}) |
15 | 39 | sdk = cast(web.Application, app.server.app)[ak.sdk] |
16 | 40 | sdk._test_start_txid = -1 |
17 | 41 |
|
18 | 42 | return app |
19 | 43 |
|
20 | 44 |
|
21 | 45 | @pytest.fixture() |
22 | | -async def client(aiohttp_client): |
| 46 | +async def client(aiohttp_client, create_app): |
23 | 47 | """Instance of app's server and client""" |
24 | | - return await start_app(aiohttp_client) |
| 48 | + return await start_app(aiohttp_client, create_app) |
25 | 49 |
|
26 | 50 |
|
27 | 51 | class AidboxSession(ClientSession): |
|
0 commit comments