Download tests/test_app.py from marcuskeenton/inference-api-integration-tests: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/marcuskeenton/inference-api-integration-tests/resolve/main/tests/test_app.py
- Command line
-
hf download hf://spaces/marcuskeenton/inference-api-integration-tests/tests/test_app.py
-
curl -L -o test_app.py https://huggingface.co/spaces/marcuskeenton/inference-api-integration-tests/resolve/main/tests/test_app.py
1.64 kB
| import functools | |
| import http.server | |
| import json | |
| import threading | |
| from pathlib import Path | |
| import pytest | |
| from playwright.sync_api import sync_playwright | |
| ROOT = Path(__file__).resolve().parent.parent | |
| def page(): | |
| handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=ROOT) | |
| server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler) | |
| threading.Thread(target=server.serve_forever, daemon=True).start() | |
| with sync_playwright() as p: | |
| browser = p.chromium.launch() | |
| page = browser.new_page() | |
| page.goto(f"http://127.0.0.1:{server.server_port}/index.html") | |
| page.wait_for_selector("#run:not([disabled])", timeout=180_000) | |
| yield page | |
| browser.close() | |
| server.shutdown() | |
| def classify(page, text): | |
| page.fill("#text", text) | |
| page.click("#run") | |
| page.wait_for_selector('#output[data-ready="true"]', timeout=30_000) | |
| return json.loads(page.inner_text("#output")) | |
| def test_positive_text_round_trip(page): | |
| out = classify(page, " I really enjoyed this film.\n\n") | |
| assert set(out) == {"label", "score", "chars_used"} | |
| assert out["label"] == "positive" | |
| assert 0.5 < out["score"] <= 1.0 | |
| assert out["chars_used"] == len("I really enjoyed this film.") | |
| def test_negative_text(page): | |
| assert classify(page, "This was a waste of two hours.")["label"] == "negative" | |
| def test_long_input_is_trimmed_not_rejected(page): | |
| assert classify(page, "good " * 2000)["chars_used"] == 1000 | |
| def test_empty_input_is_an_error(page): | |
| assert classify(page, " ") == {"error": "Please enter some text."} | |