about summary refs log tree commit diff
path: root/test/python
diff options
context:
space:
mode:
Diffstat (limited to 'test/python')
-rw-r--r--test/python/server_test_case.py42
-rw-r--r--test/python/test_hello_world.py40
-rw-r--r--test/python/test_version.py32
3 files changed, 77 insertions, 37 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)
diff --git a/test/python/test_hello_world.py b/test/python/test_hello_world.py
index d146b89..989fc85 100644
--- a/test/python/test_hello_world.py
+++ b/test/python/test_hello_world.py
@@ -1,46 +1,12 @@
-"""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
-"""
+"""Mirrors tests/hello_world.rs."""
 
 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}")
+from server_test_case import HOST, PORT, ServerTestCase
 
-    @classmethod
-    def tearDownClass(cls):
-        cls.proc.terminate()
-        cls.proc.wait(timeout=5)
 
+class HelloWorldTest(ServerTestCase):
     def test_hello_world_returns_ok_with_greeting(self):
         conn = http.client.HTTPConnection(HOST, int(PORT), timeout=5)
         try:
diff --git a/test/python/test_version.py b/test/python/test_version.py
new file mode 100644
index 0000000..2fb46e9
--- /dev/null
+++ b/test/python/test_version.py
@@ -0,0 +1,32 @@
+"""Mirrors tests/version.rs."""
+
+import http.client
+import json
+import unittest
+
+from server_test_case import HOST, PORT, ServerTestCase
+
+
+class VersionTest(ServerTestCase):
+    def test_version_returns_ok_with_json_record(self):
+        conn = http.client.HTTPConnection(HOST, int(PORT), timeout=5)
+        try:
+            conn.request("GET", "/version")
+            response = conn.getresponse()
+            body = response.read()
+        finally:
+            conn.close()
+        self.assertEqual(response.status, 200)
+
+        record = json.loads(body)
+        self.assertEqual(record["name"], "GeneNetwork4")
+        self.assertEqual(record["short_name"], "GN4AI")
+        self.assertEqual(record["version"], 4.0)
+        self.assertEqual(record["release"], 1)
+        # date and git are set at build time (see build.rs); just check shape.
+        self.assertEqual(len(record["date"]), 8)
+        self.assertTrue(record["git"])
+
+
+if __name__ == "__main__":
+    unittest.main()