Skip to content

Commit

Permalink
fix pytest_addoptions hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zerlok committed Nov 14, 2024
1 parent 4345eff commit 898b0b1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 49 deletions.
63 changes: 63 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
import typing as t
from contextlib import nullcontext
from datetime import timedelta

import pytest

# FIXME: find a way to import Parser
# NOTE: fixes strange error `Module "_pytest.config" does not explicitly export attribute "Parser"`.
from _pytest.config import Config, Parser # type: ignore[attr-defined]
from brokrpc.abc import BrokerDriver
from brokrpc.broker import Broker
from brokrpc.options import BrokerOptions
from yarl import URL

from tests.stub.driver import StubBrokerDriver, StubConsumer


@pytest.hookimpl(trylast=True)
def pytest_addoption(parser: Parser) -> None:
def parse_seconds(value: str) -> timedelta:
return timedelta(seconds=float(value))

parser.addoption(
"--broker-url",
type=URL,
default=URL("amqp://guest:guest@localhost:5672/"),
)
parser.addoption(
"--broker-driver",
type=str,
choices=["aiormq"],
default="aiormq",
)
parser.addoption(
"--broker-retry-delay",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retry-delay-mode",
type=str,
choices=["constant", "multiplier", "exponential"],
default=None,
)
parser.addoption(
"--broker-retry-max-delay",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retries-timeout",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retries-limit",
type=int,
default=None,
)


def parse_broker_options(config: Config) -> BrokerOptions:
return BrokerOptions(
url=config.getoption("broker_url"),
driver=config.getoption("broker_driver"),
retry_delay=config.getoption("broker_retry_delay"),
retry_delay_mode=config.getoption("broker_retry_delay_mode"),
retry_max_delay=config.getoption("broker_retry_max_delay"),
retries_timeout=config.getoption("broker_retries_timeout"),
retries_limit=config.getoption("broker_retries_limit"),
)


@pytest.fixture()
def broker_driver() -> StubBrokerDriver:
return StubBrokerDriver()
Expand Down
53 changes: 4 additions & 49 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,15 @@
import typing as t
from datetime import timedelta
from dataclasses import replace

import pytest

# FIXME: find a way to import Parser
# NOTE: fixes strange error `Module "_pytest.config" does not explicitly export attribute "Parser"`.
from _pytest.config import Parser # type: ignore[attr-defined]
from _pytest.fixtures import SubRequest
from brokrpc.broker import Broker
from brokrpc.options import BrokerOptions
from brokrpc.rpc.client import Client
from brokrpc.rpc.server import Server
from brokrpc.serializer.json import JSONSerializer
from yarl import URL


def pytest_addoption(parser: Parser) -> None:
def parse_seconds(value: str) -> timedelta:
return timedelta(seconds=float(value))

parser.addoption(
"--broker-url",
type=URL,
default=URL("amqp://guest:guest@localhost:5672/"),
)
parser.addoption(
"--broker-retry-delay",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retry-delay-mode",
type=str,
choices=["constant", "multiplier", "exponential"],
default=None,
)
parser.addoption(
"--broker-retry-max-delay",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retries-timeout",
type=parse_seconds,
default=None,
)
parser.addoption(
"--broker-retries-limit",
type=int,
default=None,
)
from tests.conftest import parse_broker_options


@pytest.fixture(
Expand All @@ -58,14 +18,9 @@ def parse_seconds(value: str) -> timedelta:
]
)
def rabbitmq_options(request: SubRequest) -> BrokerOptions:
return BrokerOptions(
url=request.config.getoption("broker_url"),
return replace(
parse_broker_options(request.config),
driver=request.param,
retry_delay=request.config.getoption("broker_retry_delay"),
retry_delay_mode=request.config.getoption("broker_retry_delay_mode"),
retry_max_delay=request.config.getoption("broker_retry_max_delay"),
retries_timeout=request.config.getoption("broker_retries_timeout"),
retries_limit=request.config.getoption("broker_retries_limit"),
)


Expand Down

0 comments on commit 898b0b1

Please sign in to comment.