|
| 1 | +import subprocess |
| 2 | +import tempfile |
| 3 | +import time |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import httpx |
| 7 | +import pytest |
| 8 | + |
| 9 | +from classify.renderers.html import resolve_path, to_html |
| 10 | + |
| 11 | + |
| 12 | +class DummyClass: |
| 13 | + some_attr = 7 |
| 14 | + |
| 15 | + def one(self): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="session") |
| 20 | +def classify_server(): |
| 21 | + ds_proc = subprocess.Popen( |
| 22 | + [ |
| 23 | + "classify", |
| 24 | + "tests.renderers.test_html.DummyClass", |
| 25 | + "--renderer", |
| 26 | + "html", |
| 27 | + "--serve", |
| 28 | + "--port", |
| 29 | + "8008", |
| 30 | + ], |
| 31 | + stdout=subprocess.PIPE, |
| 32 | + stderr=subprocess.STDOUT, |
| 33 | + ) |
| 34 | + |
| 35 | + # Give the server time to start |
| 36 | + time.sleep(1) |
| 37 | + |
| 38 | + # Check it started successfully |
| 39 | + assert not ds_proc.poll(), ds_proc.stdout.read().decode("utf-8") |
| 40 | + |
| 41 | + yield ds_proc |
| 42 | + |
| 43 | + # Shut it down at the end of the pytest session |
| 44 | + ds_proc.terminate() |
| 45 | + |
| 46 | + |
| 47 | +def test_to_html(dummy_class): |
| 48 | + with tempfile.TemporaryDirectory() as path: |
| 49 | + path = Path(path) # noqa: PLW2901 |
| 50 | + to_html(dummy_class, output_path=path, serve=False, port=8000) |
| 51 | + |
| 52 | + output = path / "classify.html" |
| 53 | + assert output.exists() |
| 54 | + |
| 55 | + content = output.read_text() |
| 56 | + assert dummy_class.name in content |
| 57 | + |
| 58 | + |
| 59 | +def test_to_html_and_serve(classify_server): # noqa: ARG001 |
| 60 | + response = httpx.get("http://127.0.0.1:8008/") |
| 61 | + assert response.status_code == 200 # noqa: PLR2004 |
| 62 | + |
| 63 | + |
| 64 | +def test_resolve_path_with_path(): |
| 65 | + with tempfile.TemporaryDirectory() as parent: |
| 66 | + path = Path(parent) / "testing" |
| 67 | + assert not path.exists() |
| 68 | + |
| 69 | + with resolve_path(path) as made_path: |
| 70 | + assert made_path == path |
| 71 | + assert made_path.exists() |
| 72 | + |
| 73 | + |
| 74 | +def test_resolve_path_without_path(): |
| 75 | + with resolve_path(None) as path: |
| 76 | + assert path.exists() |
| 77 | + |
| 78 | + assert not path.exists() |
0 commit comments