Refactored custom commands module into layered architecture with typed domain objects and comprehensive tests.
CI / Formatting (push) Successful in 6s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 15s
CI / Tests (Python 3.13) (push) Successful in 14s
CI / Tests (Python 3.14) (push) Successful in 11s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 6s
CI / Formatting (push) Successful in 6s
CI / Linting (push) Successful in 5s
CI / Tests (Python 3.12) (push) Successful in 15s
CI / Tests (Python 3.13) (push) Successful in 14s
CI / Tests (Python 3.14) (push) Successful in 11s
CI / Type Checking (push) Successful in 9s
CI / Spelling (push) Successful in 6s
This commit is contained in:
@@ -29,6 +29,7 @@ from freezegun import freeze_time
|
||||
|
||||
from owlbot.builtin_modules.custom_commands.placeholder_handlers import _format_duration
|
||||
from owlbot.builtin_modules.custom_commands.placeholders import process_placeholders
|
||||
from owlbot.builtin_modules.custom_commands.types import Command
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from owlbot.api.storage import ModuleStorage
|
||||
@@ -40,6 +41,49 @@ CREATE_COUNTERS = (
|
||||
)
|
||||
|
||||
|
||||
class _StorageCounterAccessor:
|
||||
"""CounterAccessor backed by raw ModuleStorage for placeholder tests."""
|
||||
|
||||
def __init__(self, storage: ModuleStorage) -> None:
|
||||
self._storage = storage
|
||||
|
||||
async def get_counter(self, name: str) -> int:
|
||||
row = await self._storage.fetch_one(
|
||||
"SELECT value FROM counters WHERE name = ?", (name,)
|
||||
)
|
||||
return int(row["value"]) if row else 0
|
||||
|
||||
async def set_counter(self, name: str, value: int) -> int:
|
||||
result = await self._storage.fetch_value(
|
||||
"INSERT OR REPLACE INTO counters (name, value) "
|
||||
"VALUES (?, ?) RETURNING value",
|
||||
(name, value),
|
||||
)
|
||||
return int(result) # type: ignore[arg-type]
|
||||
|
||||
async def adjust_counter(self, name: str, delta: int) -> int:
|
||||
result = await self._storage.fetch_value(
|
||||
"INSERT INTO counters (name, value) VALUES (?, ?) "
|
||||
"ON CONFLICT(name) DO UPDATE SET value = value + ? "
|
||||
"RETURNING value",
|
||||
(name, delta, delta),
|
||||
)
|
||||
return int(result) # type: ignore[arg-type]
|
||||
|
||||
|
||||
def _make_command(use_count: int = 1) -> Command:
|
||||
"""Build a minimal Command snapshot for placeholder tests."""
|
||||
return Command(
|
||||
name="test",
|
||||
response="",
|
||||
use_count=use_count,
|
||||
created_at="2026-01-01T00:00:00+00:00",
|
||||
updated_at="2026-01-01T00:00:00+00:00",
|
||||
requires_moderator=False,
|
||||
cooldown=0,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def placeholder_storage(storage: ModuleStorage) -> ModuleStorage:
|
||||
"""Storage instance with the counters table created."""
|
||||
@@ -60,8 +104,8 @@ async def process(
|
||||
template,
|
||||
args or [],
|
||||
user,
|
||||
use_count,
|
||||
storage,
|
||||
_make_command(use_count),
|
||||
_StorageCounterAccessor(storage),
|
||||
max_depth=max_depth,
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user