aboutsummaryrefslogtreecommitdiff
path: root/tests/conftest.py
blob: 0072f26d639a81db6cb65fca27f4bcdba6b567ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""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)