Skip to content

Assertions

tomodachi_testcontainers.assertions

DEFAULT_DATETIME_RANGE module-attribute

DEFAULT_DATETIME_RANGE = timedelta(seconds=10)

assert_datetime_within_range

assert_datetime_within_range(value, range=DEFAULT_DATETIME_RANGE)
PARAMETER DESCRIPTION
value

TYPE: datetime

range

TYPE: timedelta DEFAULT: DEFAULT_DATETIME_RANGE

Source code in src/tomodachi_testcontainers/assertions.py
def assert_datetime_within_range(value: datetime, range: timedelta = DEFAULT_DATETIME_RANGE) -> None:
    current_datetime = datetime.now(timezone.utc)
    start_datetime = current_datetime - range
    end_datetime = current_datetime + range
    assert start_datetime <= value <= end_datetime  # nosec: B101

assert_logs_contain

assert_logs_contain(container, contains)
PARAMETER DESCRIPTION
container

TYPE: DockerContainer

contains

TYPE: str

Source code in src/tomodachi_testcontainers/assertions.py
def assert_logs_contain(container: DockerContainer, contains: str) -> None:
    stdout_and_stderr_logs = cast(Tuple[bytes, bytes], container.get_logs())
    for log in stdout_and_stderr_logs:
        if contains in log.decode():
            return
    raise AssertionError(f"Expected logs to contain: '{contains}'; logs: {stdout_and_stderr_logs}")  # noqa: E702

assert_logs_not_contain

assert_logs_not_contain(container, contains)
PARAMETER DESCRIPTION
container

TYPE: DockerContainer

contains

TYPE: str

Source code in src/tomodachi_testcontainers/assertions.py
def assert_logs_not_contain(container: DockerContainer, contains: str) -> None:
    stdout_and_stderr_logs = cast(Tuple[bytes, bytes], container.get_logs())
    for log in stdout_and_stderr_logs:
        if contains in log.decode():
            raise AssertionError(
                f"Expected logs not to contain: '{contains}'; logs: {stdout_and_stderr_logs}"  # noqa: E702
            )

assert_logs_match_line_count

assert_logs_match_line_count(container, contains, count)
PARAMETER DESCRIPTION
container

TYPE: DockerContainer

contains

TYPE: str

count

TYPE: int

Source code in src/tomodachi_testcontainers/assertions.py
def assert_logs_match_line_count(container: DockerContainer, contains: str, count: int) -> None:
    stdout_logs = cast(bytes, container.get_logs()[0])
    stderr_logs = cast(bytes, container.get_logs()[1])
    stdout_and_stderr_logs = "\n".join([stdout_logs.decode(), stderr_logs.decode()])

    matched_lines = [log for log in stdout_and_stderr_logs.splitlines() if contains in log]
    error_msg = (
        f"Expected '{contains}' to be contained in {count} lines, found {len(matched_lines)} lines"  # noqa: E702
        f"; logs: {stdout_and_stderr_logs}"  # noqa: E702
    )
    assert len(matched_lines) == count, error_msg  # nosec: B101