Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
etianen committed Feb 4, 2024
1 parent b5a36aa commit 4f4b18a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
14 changes: 1 addition & 13 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
from __future__ import annotations

import asyncio
import logging
import threading
from collections.abc import Coroutine, Generator
from collections.abc import Generator
from contextlib import contextmanager
from functools import wraps
from time import sleep
from typing import Any, Callable

from logot import Captured, Logot
from logot._typing import P

logger = logging.getLogger("logot")


def asyncio_test(test_fn: Callable[P, Coroutine[Any, Any, None]]) -> Callable[P, None]:
@wraps(test_fn)
def asyncio_test_wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
asyncio.run(test_fn(*args, **kwargs))

return asyncio_test_wrapper


def lines(*lines: str) -> str:
return "\n".join(lines)

Expand Down
44 changes: 44 additions & 0 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from __future__ import annotations

import asyncio
from collections.abc import Coroutine
from functools import wraps
from typing import Any, Callable

import pytest

from logot import Captured, Logot, logged
from logot._typing import P
from tests import capture_soon, lines


def asyncio_test(test_fn: Callable[P, Coroutine[Any, Any, None]]) -> Callable[P, None]:
@wraps(test_fn)
def asyncio_test_wrapper(*args: P.args, **kwargs: P.kwargs) -> None:
asyncio.run(test_fn(*args, **kwargs))

return asyncio_test_wrapper


@asyncio_test
async def test_await_for_pass_immediate(logot: Logot) -> None:
logot.capture(Captured("INFO", "foo bar"))
await logot.await_for(logged.info("foo bar"))


@asyncio_test
async def test_await_for_pass_soon(logot: Logot) -> None:
with capture_soon(logot, Captured("INFO", "foo bar")):
await logot.await_for(logged.info("foo bar"))


@asyncio_test
async def test_await_for_fail(logot: Logot) -> None:
logot.capture(Captured("INFO", "boom!"))
with pytest.raises(AssertionError) as ex:
await logot.await_for(logged.info("foo bar"), timeout=0.1)
assert str(ex.value) == lines(
"Not logged:",
"",
"[INFO] foo bar",
)
26 changes: 1 addition & 25 deletions tests/test_logot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest

from logot import Captured, Logot, logged
from tests import asyncio_test, capture_soon, lines, logger
from tests import capture_soon, lines, logger


def test_capturing() -> None:
Expand Down Expand Up @@ -80,30 +80,6 @@ def test_wait_for_fail(logot: Logot) -> None:
)


@asyncio_test
async def test_await_for_pass_immediate(logot: Logot) -> None:
logot.capture(Captured("INFO", "foo bar"))
await logot.await_for(logged.info("foo bar"))


@asyncio_test
async def test_await_for_pass_soon(logot: Logot) -> None:
with capture_soon(logot, Captured("INFO", "foo bar")):
await logot.await_for(logged.info("foo bar"))


@asyncio_test
async def test_await_for_fail(logot: Logot) -> None:
logot.capture(Captured("INFO", "boom!"))
with pytest.raises(AssertionError) as ex:
await logot.await_for(logged.info("foo bar"), timeout=0.1)
assert str(ex.value) == lines(
"Not logged:",
"",
"[INFO] foo bar",
)


def test_clear(logot: Logot) -> None:
logot.capture(Captured("INFO", "foo bar"))
logot.clear()
Expand Down

0 comments on commit 4f4b18a

Please sign in to comment.