Spaces:
Sleeping
Sleeping
rjorgec
commited on
Add files via upload
Browse files- tests/test_app_smoke.py +64 -0
- tests/test_basic.py +11 -0
tests/test_app_smoke.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_app_smoke.py
|
| 2 |
+
"""
|
| 3 |
+
Smoke test that simulates a CPU-only Hugging Face Spaces import.
|
| 4 |
+
It *does not* start any server or UI. If no app module exists, the test is skipped.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import sys
|
| 8 |
+
import pathlib
|
| 9 |
+
import importlib
|
| 10 |
+
import inspect
|
| 11 |
+
import pytest
|
| 12 |
+
|
| 13 |
+
def _import_app_module():
|
| 14 |
+
root = pathlib.Path(__file__).resolve().parents[1]
|
| 15 |
+
sys.path.insert(0, str(root))
|
| 16 |
+
sys.path.insert(0, str(root / "src"))
|
| 17 |
+
|
| 18 |
+
# Prefer real files if present; otherwise try a plain "app" import
|
| 19 |
+
candidates = []
|
| 20 |
+
if (root / "app.py").exists():
|
| 21 |
+
candidates.append("app")
|
| 22 |
+
if (root / "src" / "app.py").exists():
|
| 23 |
+
candidates.append("app")
|
| 24 |
+
if not candidates:
|
| 25 |
+
candidates = ["app"]
|
| 26 |
+
|
| 27 |
+
last_err = None
|
| 28 |
+
for name in candidates:
|
| 29 |
+
try:
|
| 30 |
+
return importlib.import_module(name)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
last_err = e
|
| 33 |
+
pytest.skip(f"No importable app module found (last error: {last_err})")
|
| 34 |
+
|
| 35 |
+
def _try_tiny_call(module) -> bool:
|
| 36 |
+
preferred = ("ping", "healthcheck", "health_check", "hello", "version", "get_version")
|
| 37 |
+
fallback = ("predict", "inference", "run", "main", "chord_bot")
|
| 38 |
+
|
| 39 |
+
def try_call(fn):
|
| 40 |
+
try:
|
| 41 |
+
sig = inspect.signature(fn)
|
| 42 |
+
if len(sig.parameters) == 0:
|
| 43 |
+
fn()
|
| 44 |
+
return True
|
| 45 |
+
if len(sig.parameters) == 1:
|
| 46 |
+
fn("test")
|
| 47 |
+
return True
|
| 48 |
+
except Exception:
|
| 49 |
+
return False
|
| 50 |
+
return False
|
| 51 |
+
|
| 52 |
+
called = False
|
| 53 |
+
for name in list(preferred) + list(fallback):
|
| 54 |
+
if hasattr(module, name) and callable(getattr(module, name)):
|
| 55 |
+
if try_call(getattr(module, name)):
|
| 56 |
+
called = True
|
| 57 |
+
break
|
| 58 |
+
return called
|
| 59 |
+
|
| 60 |
+
def test_import_app_and_optionally_call():
|
| 61 |
+
module = _import_app_module()
|
| 62 |
+
assert module is not None # Import alone verifies basic environment
|
| 63 |
+
# Try a non-server, tiny function call if present; ok if none fits
|
| 64 |
+
_try_tiny_call(module)
|
tests/test_basic.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# tests/test_basic.py
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
def helper_square(x: int) -> int:
|
| 5 |
+
return x * x
|
| 6 |
+
|
| 7 |
+
def test_math_and_helper():
|
| 8 |
+
# Deterministic, fast checks to verify the test runner works
|
| 9 |
+
assert 2 + 2 == 4
|
| 10 |
+
assert math.isclose(math.sqrt(9), 3.0)
|
| 11 |
+
assert helper_square(5) == 25
|