about summary refs log tree commit diff
path: root/tests/conftest.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/conftest.py')
-rw-r--r--tests/conftest.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 6ef5374..70bbd37 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,10 +1,40 @@
 """Set up fixtures for tests"""
+import os
+import socket
+import subprocess
+from contextlib import closing
 
 import pytest
 
+from qc_app import create_app
 from quality_control.parsing import strain_names
 
 @pytest.fixture(scope="session")
 def strains():
     """Parse the strains once every test session"""
     return strain_names("etc/strains.csv")
+
+def is_port_in_use(port: int) -> bool:
+    "Check whether `port` is in use"
+    with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sckt:
+        return sckt.connect_ex(("localhost", port)) == 0
+
+@pytest.fixture(scope="session")
+def redis_server():
+    "Fixture to launch a new redis instance and return appropriate URI"
+    port = next(# pylint: disable=[stop-iteration-return]
+        port for port in range(6379,65535) if not is_port_in_use(port))
+    redis_path = f"{os.getenv('GUIX_ENVIRONMENT')}/bin/redis-server"
+    command = [redis_path, "--port", str(port)]
+    process = subprocess.Popen(command) # pylint: disable=[consider-using-with]
+    yield f"redis://localhost:{port}"
+    process.kill()
+
+@pytest.fixture(scope="module")
+def client(redis_server): # pylint: disable=[redefined-outer-name]
+    "Fixture for test client"
+    app = create_app(f"{os.path.abspath('.')}/tests/test_instance_dir")
+    app.config.update({
+        "REDIS_URL": redis_server
+    })
+    yield app.test_client()