aboutsummaryrefslogtreecommitdiff
path: root/tests/qc_app/test_parse.py
blob: a4bfdd5cb4a2c31cc27865f37a83c8172b3a825f (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
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
"""Test the 'parse' module in the web-ui"""
import redis

import pytest

from qc_app.jobs import job
from tests.conftest import uploadable_file_object

def test_parse_with_existing_uploaded_file(
        client, redis_url, job_id, monkeypatch):
    """
    GIVEN: 1. A flask application testing client
           2. A valid file, and filetype
    WHEN: The file is uploaded, and the parsing triggered
    THEN: Ensure that:
          1. the system redirects to the job/parse status page
          2. the job is placed on redis for processing
    """
    monkeypatch.setattr("qc_app.jobs.uuid4", lambda : job_id)
    # Upload a file
    filename = "no_data_errors.tsv"
    filetype = "average"
    client.post(
        "/", data={
            "filetype": filetype,
            "qc_text_file": uploadable_file_object(filename)})
    # Checks
    resp = client.get(f"/parse/parse?filename={filename}&filetype={filetype}")
    assert resp.status_code == 302
    assert b'Redirecting...' in resp.data
    assert b'/parse/status/934c55d8-396e-4959-90e1-2698e9205758' in resp.data

    with redis.Redis.from_url(redis_url, decode_responses=True) as rconn:
        the_job = job(rconn, job_id)

    assert the_job["job_id"] == job_id
    assert the_job["filename"] == filename
    assert the_job["command"] == " ".join([
        "python3", "-m", "scripts.worker", filetype,
        f"{client.application.config['UPLOAD_FOLDER']}/{filename}", redis_url,
        job_id])

@pytest.mark.parametrize(
    "filename,uri,error_msgs",
    (("non_existent.file",
      "/parse/parse?filename=non_existent.file&filename=average",
      [(b'<span class="alert alert-error">Selected file does not exist '
        b'(any longer)</span>')]),
     ("non_existent.file",
      "/parse/parse?filename=non_existent.file&filename=standard-error",
      [(b'<span class="alert alert-error">Selected file does not exist '
        b'(any longer)</span>')]),
     ("non_existent.file",
      "/parse/parse?filename=non_existent.file&filename=percival",
      [(b'<span class="alert alert-error">Selected file does not exist '
        b'(any longer)</span>'),
       b'<span class="alert alert-error">Invalid filetype provided</span>']),
     ("no_data_errors.tsv",
      "/parse/parse?filename=no_data_errors.tsv&filename=percival",
      [b'<span class="alert alert-error">Invalid filetype provided</span>']),
     ("no_data_errors.tsv",
      "/parse/parse?filename=no_data_errors.tsv",
      [b'<span class="alert alert-error">No filetype provided</span>']),
     (None, "/parse/parse",
      [b'<span class="alert alert-error">No file provided</span>',
       b'<span class="alert alert-error">No filetype provided</span>'])))
def test_parse_with_non_uploaded_file(client, filename, uri, error_msgs):
    """
    GIVEN: 1. A flask application testing client
           2. A valid filetype
           3. A filename to a file that has not been uploaded yet
    WHEN: The parsing triggered
    THEN: Ensure that the appropriate errors are displayed
    """
    ## Conditionally upload files
    if filename and filename != "non_existent.file":
        client.post(
        "/", data={
            "filetype": "average",
            "qc_text_file": uploadable_file_object(filename)})
    # Trigger
    resp = client.get(uri,follow_redirects=True)
    ## Check that there was exactly one redirect
    assert len(resp.history) == 1 and resp.history[0].status_code == 302
    ## Check that redirect is to home page and is successful
    assert resp.request.path == "/"
    assert resp.status_code == 200
    ## Check that error(s) are displayed
    for error_msg in error_msgs:
        assert error_msg in resp.data