about summary refs log tree commit diff
path: root/test/python/server_test_case.py
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-01 16:01:20 +0000
committerPjotr <wrk@bosa.lan>2026-10-01 16:01:20 +0000
commite8fe116ef4a996a8281bf0b43bed5e9d39c766c8 (patch)
tree00170767d45e96ff4c53538201e50321265df38d /test/python/server_test_case.py
parent457a463286c2feb1e6d90460acb65927177fcc09 (diff)
downloadgenenetwork4-e8fe116ef4a996a8281bf0b43bed5e9d39c766c8.tar.gz
Add /version endpoint, extract shared python test helper.
* .gitignore: Add __pycache__.
* src/lib.rs (app): Add /version route.
(VersionInfo, version): New struct and handler, returning name,
short_name, version, release, date, and git as a JSON record.
* build.rs: New file.  Embeds GN4_GIT_HASH and GN4_COMMIT_DATE via
cargo:rustc-env, read from git rev-parse and git show of HEAD.
* Cargo.toml (dependencies): Add serde with derive feature.
[dev-dependencies]: Add serde_json.
* tests/version.rs: New file.
* test/python/server_test_case.py: New file.  ServerTestCase,
wait_for_port, BINARY, HOST, PORT moved here from
test/python/test_hello_world.py.
* test/python/test_hello_world.py: Use server_test_case.ServerTestCase
instead of duplicating setUpClass and tearDownClass.
* test/python/test_version.py: New file.
* guix.scm: Add (gnu packages version-control) to use-modules.
(genenetwork4-git)[native-inputs]: Add git-minimal.
(genenetwork4-git)[arguments]<#:phases>[python-rest-test]: Run
test/python with python3 -m unittest discover instead of invoking
test_hello_world.py directly, so test_version.py also runs.
Diffstat (limited to 'test/python/server_test_case.py')
-rw-r--r--test/python/server_test_case.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/python/server_test_case.py b/test/python/server_test_case.py
new file mode 100644
index 0000000..0025c4b
--- /dev/null
+++ b/test/python/server_test_case.py
@@ -0,0 +1,42 @@
+"""Shared helper for tests that exercise a running genenetwork4 server over
+real HTTP (Python can only reach the server this way, unlike the Rust
+integration tests under tests/, which call the router in-process).
+"""
+
+import os
+import socket
+import subprocess
+import time
+import unittest
+
+BINARY = os.environ.get("GENENETWORK4_BINARY", "genenetwork4")
+HOST = "127.0.0.1"
+PORT = os.environ.get("GENENETWORK4_PORT", "3000")
+
+
+def wait_for_port(host, port, timeout=10):
+    deadline = time.time() + timeout
+    while time.time() < deadline:
+        try:
+            with socket.create_connection((host, int(port)), timeout=0.5):
+                return True
+        except OSError:
+            time.sleep(0.1)
+    return False
+
+
+class ServerTestCase(unittest.TestCase):
+    """Spawns the genenetwork4 binary for the duration of the test class."""
+
+    @classmethod
+    def setUpClass(cls):
+        env = dict(os.environ, GENENETWORK4_PORT=PORT)
+        cls.proc = subprocess.Popen([BINARY], env=env)
+        if not wait_for_port(HOST, PORT):
+            cls.proc.terminate()
+            raise RuntimeError(f"server did not start listening on {HOST}:{PORT}")
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.proc.terminate()
+        cls.proc.wait(timeout=5)