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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
"""Set up fixtures for tests"""
import io
import os
import socket
import subprocess
from contextlib import closing
import redis
import pytest
import jsonpickle
from qc_app import create_app
from quality_control.parsing import strain_names
from quality_control.errors import InvalidValue, DuplicateHeading
@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_url():
"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_url): # 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_url
})
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)
@pytest.fixture(scope="function")
def job_id():
"Return a default UUID4 string as the 'job_id' for test purposes"
return "934c55d8-396e-4959-90e1-2698e9205758"
@pytest.fixture(scope="function")
def redis_conn_with_fresh_job(redis_url, job_id): # pylint: disable=[redefined-outer-name]
"redis connection with fresh, unprocessed job"
the_job = {
"job_id": job_id, "command": "some_test_command", "status": "pending",
"filename": "/path/to/some/file.tsv", "percent": 0
}
with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
rconn.hset(name=job_id, mapping=the_job)
yield rconn
rconn.hdel(job_id, *the_job.keys())
rconn.delete(job_id)
@pytest.fixture(scope="function")
def redis_conn_with_in_progress_job_no_errors(redis_url, job_id): # pylint: disable=[redefined-outer-name]
"redis connection with partially processed job, with no errors"
the_job = {
"job_id": job_id, "command": "some_test_command",
"status": "Processing", "filename": "/path/to/some/file.tsv",
"percent": 32.242342
}
with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
rconn.hset(name=job_id, mapping=the_job)
yield rconn
rconn.hdel(job_id, *the_job.keys())
rconn.delete(job_id)
@pytest.fixture(scope="function")
def redis_conn_with_in_progress_job_some_errors(redis_url, job_id): # pylint: disable=[redefined-outer-name]
"redis connection with partially processed job, with some errors"
the_job = {
"job_id": job_id, "command": "some_test_command",
"status": "Processing", "filename": "/path/to/some/file.tsv",
"percent": 45.34245, "errors": jsonpickle.encode((
DuplicateHeading(
1, (5,13,19), "DupHead", "Heading 'DupHead' is repeated"),
InvalidValue(45, 2, "ohMy", "Invalid value 'ohMy'")))
}
with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
rconn.hset(name=job_id, mapping=the_job)
yield rconn
rconn.hdel(job_id, *the_job.keys())
rconn.delete(job_id)
@pytest.fixture(scope="function")
def redis_conn_with_completed_job_no_errors(redis_url, job_id): # pylint: disable=[redefined-outer-name]
"redis connection with completely processed job, with no errors"
the_job = {
"job_id": job_id, "command": "some_test_command",
"status": "success", "filename": "/path/to/some/file.tsv",
"percent": 100
}
with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
rconn.hset(name=job_id, mapping=the_job)
yield rconn
rconn.hdel(job_id, *the_job.keys())
rconn.delete(job_id)
@pytest.fixture(scope="function")
def redis_conn_with_completed_job_some_errors(redis_url, job_id): # pylint: disable=[redefined-outer-name]
"redis connection with completely processed job, with some errors"
the_job = {
"job_id": job_id, "command": "some_test_command",
"status": "success", "filename": "/path/to/some/file.tsv",
"percent": 100, "errors": jsonpickle.encode((
DuplicateHeading(
1, (5,13,19), "DupHead", "Heading 'DupHead' is repeated"),
InvalidValue(45, 2, "ohMy", "Invalid value 'ohMy'")))
}
with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
rconn.hset(name=job_id, mapping=the_job)
yield rconn
rconn.hdel(job_id, *the_job.keys())
rconn.delete(job_id)
|