Spaces:
Running
Running
Download tests/test_http_server.py from Melikshah/geoql-env: direct link, hf CLI and curl.
- Browser
- Download file 4.09 kB
-
https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_http_server.py
- Command line
-
hf download hf://spaces/Melikshah/geoql-env/tests/test_http_server.py
-
curl -L -o test_http_server.py https://huggingface.co/spaces/Melikshah/geoql-env/resolve/main/tests/test_http_server.py
4.09 kB
| import json | |
| import re | |
| import threading | |
| import urllib.error | |
| import urllib.request | |
| import pytest | |
| from geoql_env import http_server as H | |
| from geoql_env import tasks | |
| from geoql_env.engine import Result | |
| from geoql_env.tasks import Interpretation, Task | |
| TASK = Task("t", "Which cafes are here?", "real", "set", (Interpretation("A", "nwr(1); out; // gold", "x"),), | |
| family="containment", split="dev") | |
| class FakeEngine: | |
| def query(self, world, text): | |
| ids = {"gold": (1, 2), "half": (1,)}[re.search(r"// (\w+)", text).group(1)] | |
| return Result("ok", elements=[{"type": "node", "id": i, "tags": {"amenity": "cafe"}} for i in ids]) | |
| def base(monkeypatch): | |
| monkeypatch.setattr(tasks, "load_all", lambda: [TASK]) | |
| server = H.Server(("127.0.0.1", 0), engine=FakeEngine()) | |
| threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.01}, daemon=True).start() | |
| yield f"http://127.0.0.1:{server.server_port}" | |
| server.shutdown() | |
| server.server_close() | |
| def call(base, path, body=None, raw=None): | |
| data = raw if raw is not None else None if body is None else json.dumps(body).encode() | |
| request = urllib.request.Request(base + path, data=data, method="GET" if data is None else "POST") | |
| try: | |
| with urllib.request.urlopen(request) as response: | |
| return response.status, json.load(response) | |
| except urllib.error.HTTPError as ex: | |
| return ex.code, json.load(ex) | |
| def test_episode_over_http(base): | |
| assert call(base, "/health") == (200, {"status": "ok"}) | |
| assert call(base, "/tasks") == (200, {"tasks": [{"id": "t", "split": "dev", "family": "containment", | |
| "world": "real", "answer": "set"}]}) | |
| status, reset = call(base, "/reset", {"task": "t"}) | |
| assert status == 200 and reset["info"]["task"] == "t" and len(reset["observation"]["tools"]) == 4 | |
| episode = reset["episode_id"] | |
| status, step = call(base, "/step", {"episode_id": episode, | |
| "action": {"tool": "execute", "arguments": {"query": "nwr(1); out; // half"}}}) | |
| assert status == 200 and step["observation"].startswith("Tagged objects: 1") | |
| assert (step["reward"], step["terminated"], step["truncated"]) == (0.0, False, False) | |
| assert call(base, f"/state?episode_id={episode}")[1]["used"] == 1 | |
| status, step = call(base, "/step", {"episode_id": episode, | |
| "action": {"tool": "submit", "arguments": {"query": "nwr(1); out; // gold"}}}) | |
| assert (status, step["reward"], step["terminated"], step["info"]["passed"]) == (200, 1.0, True, True) | |
| assert call(base, "/step", {"episode_id": episode, "action": {"tool": "submit", "arguments": {}}}) == ( | |
| 409, {"error": "the episode is over"}) | |
| status, single = call(base, "/reset", {"split": "dev", "seed": 1, "mode": "single_shot"}) | |
| assert status == 200 and single["observation"]["tools"] == [] and single["episode_id"] != episode | |
| def test_errors(base, path, body, raw, status, error): | |
| got_status, got = call(base, path, body, raw) | |
| assert got_status == status and (error is None or got == {"error": error}) | |
| def test_invalid_action(base): | |
| episode = call(base, "/reset", {"task": "t"})[1]["episode_id"] | |
| assert call(base, "/step", {"episode_id": episode, "action": "execute"}) == ( | |
| 400, {"error": 'an action is {"tool": name, "arguments": {...}}'}) | |