From 8eb6bdbe9a3c461f51f8f011e0784deeff6296e6 Mon Sep 17 00:00:00 2001 From: Payne Date: Wed, 4 Jun 2025 19:31:55 +0200 Subject: [PATCH] Add basic pytest suite --- pyproject.toml | 7 +++++- tests/__init__.py | 0 tests/test_basic.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/test_basic.py diff --git a/pyproject.toml b/pyproject.toml index f18ba03..1425ab5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -52,4 +52,9 @@ dtop = "dtop.main:main" include = ["dtop*"] [tool.setuptools.package-data] -dtop = ["utils/normalize_logs.py"] \ No newline at end of file +dtop = ["utils/normalize_logs.py"] + +[tool.pytest.ini_options] +addopts = "-ra" +testpaths = ["tests"] +python_files = ["test_*.py"] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_basic.py b/tests/test_basic.py new file mode 100644 index 0000000..16d64a0 --- /dev/null +++ b/tests/test_basic.py @@ -0,0 +1,53 @@ +import types +import datetime +import pytest +from dtop.core.docker_tui import DockerTUI + +class DummyContainer: + def __init__(self, id, name, image_tags=None, status='running'): + self.id = id + self.name = name + self.image = types.SimpleNamespace(tags=image_tags or []) + self.status = status + self.attrs = { + 'Created': '2024-01-01T00:00:00Z', + 'State': { + 'Running': status == 'running', + 'StartedAt': '2024-01-01T00:00:00Z' + } + } + +class DummyClient: + def __init__(self, containers): + self.containers = types.SimpleNamespace(list=lambda all=True: containers) + +@pytest.fixture +def dummy_tui(monkeypatch): + containers = [ + DummyContainer('1', 'alpha', ['img1'], 'running'), + DummyContainer('2', 'bravo', ['img2'], 'exited') + ] + monkeypatch.setattr('docker.from_env', lambda: DummyClient(containers)) + monkeypatch.setattr('dtop.core.stats.schedule_stats_collection_sync', lambda *a, **k: None) + return DockerTUI() + +def test_fetch_containers(dummy_tui): + conts = dummy_tui.fetch_containers() + assert len(conts) == 2 + assert {c.name for c in conts} == {'alpha', 'bravo'} + +def test_sort_containers(dummy_tui): + containers = dummy_tui.fetch_containers() + dummy_tui.sort_column = 0 # NAME + sorted_list = dummy_tui.sort_containers(containers) + assert [c.name for c in sorted_list] == ['alpha', 'bravo'] + +from dtop.utils.utils import format_bytes, format_datetime + +def test_format_bytes(): + assert format_bytes(1024) == '1.0KB' + assert format_bytes(1048576) == '1.0MB' + +def test_format_datetime(): + iso = '2024-05-05T12:34:56Z' + assert format_datetime(iso) == '2024-05-05 12:34:56'