"""Set up fixtures for tests""" import io 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() def uploadable_file_object(filename): "Return an 'object' representing the file to be uploaded." with open(f"tests/test_data/{filename}", "br") as the_file: return (io.BytesIO(the_file.read()), filename)