diff options
Diffstat (limited to 'test/python')
| -rw-r--r-- | test/python/test_openapi.py | 2 | ||||
| -rw-r--r-- | test/python/test_species_by_name.py | 56 |
2 files changed, 57 insertions, 1 deletions
diff --git a/test/python/test_openapi.py b/test/python/test_openapi.py index 03100b9..0815d34 100644 --- a/test/python/test_openapi.py +++ b/test/python/test_openapi.py @@ -20,7 +20,7 @@ class OpenApiTest(ServerTestCase): doc = json.loads(body) self.assertEqual(doc["info"]["title"], "GeneNetwork4 API") - for path in ("/", "/version", "/species"): + for path in ("/", "/version", "/species", "/species/{species}"): self.assertIn(path, doc["paths"]) diff --git a/test/python/test_species_by_name.py b/test/python/test_species_by_name.py new file mode 100644 index 0000000..86f80a3 --- /dev/null +++ b/test/python/test_species_by_name.py @@ -0,0 +1,56 @@ +"""Mirrors tests/species_by_name.rs. Needs a live MariaDB (SQL_URI); skips +with a message rather than failing when none is reachable, same as +test_species.py. +""" + +import http.client +import json +import socket +import unittest + +from server_test_case import HOST, PORT, ServerTestCase + + +def database_reachable(): + try: + with socket.create_connection(("127.0.0.1", 3306), timeout=0.5): + return True + except OSError: + return False + + +class SpeciesByNameTest(ServerTestCase): + def test_species_by_name_mouse_returns_species_row(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("GET", "/species/mouse") + response = conn.getresponse() + body = response.read() + finally: + conn.close() + self.assertEqual(response.status, 200) + + species = json.loads(body) + self.assertEqual(species["name"], "mouse") + self.assertEqual(species["full_name"], "Mus musculus") + self.assertEqual(species["taxonomy_id"], 10090) + + def test_species_by_name_unknown_species_returns_404(self): + if not database_reachable(): + self.skipTest("no database on 127.0.0.1:3306") + + conn = http.client.HTTPConnection(HOST, int(PORT), timeout=10) + try: + conn.request("GET", "/species/not-a-real-species") + response = conn.getresponse() + response.read() + finally: + conn.close() + self.assertEqual(response.status, 404) + + +if __name__ == "__main__": + unittest.main() |
