about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-01 15:12:06 +0000
committerPjotr <wrk@bosa.lan>2026-10-01 15:12:18 +0000
commit457a463286c2feb1e6d90460acb65927177fcc09 (patch)
tree1395d40eaad15f9775b603dce1eee1ef4d6c06f0 /test/python
parentb6374cf8d80c161cfed761d4c97eff320f311539 (diff)
downloadgenenetwork4-457a463286c2feb1e6d90460acb65927177fcc09.tar.gz
Add Python REST API test, run it from guix.scm.
* src/main.rs (main): Read GENENETWORK4_PORT environment variable
for the listen port, default to 3000.
* test/python/test_hello_world.py: New file.
* guix.scm: Add (gnu packages python) to use-modules.
(genenetwork4-git)[native-inputs]: Add python.
(genenetwork4-git)[arguments]<#:phases>: Add 'python-rest-test
phase after 'install, running test/python/test_hello_world.py
against the installed binary on port 3737.
Diffstat (limited to 'test/python')
-rw-r--r--test/python/test_hello_world.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/python/test_hello_world.py b/test/python/test_hello_world.py
new file mode 100644
index 0000000..d146b89
--- /dev/null
+++ b/test/python/test_hello_world.py
@@ -0,0 +1,57 @@
+"""Mirrors tests/hello_world.rs: exercises the REST API over real HTTP,
+rather than in-process, since Python can only reach the server this way.
+
+Usage: GENENETWORK4_BINARY=/path/to/genenetwork4 python3 test_hello_world.py
+"""
+
+import http.client
+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 HelloWorldTest(unittest.TestCase):
+    @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)
+
+    def test_hello_world_returns_ok_with_greeting(self):
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=5)
+        try:
+            conn.request("GET", "/")
+            response = conn.getresponse()
+            body = response.read()
+        finally:
+            conn.close()
+        self.assertEqual(response.status, 200)
+        self.assertEqual(body, b"Hello, world!")
+
+
+if __name__ == "__main__":
+    unittest.main()