From c6b182d346dcf6d3b1cb98956aa3066067f00757 Mon Sep 17 00:00:00 2001
From: Frederick Muriuki Muriithi
Date: Thu, 16 Jun 2022 13:33:31 +0300
Subject: Add more UI tests
- Test upload with missing or invalid data
- Test triggering the parsing of the file
---
tests/conftest.py | 6 +++++
tests/qc_app/__init__.py | 0
tests/qc_app/test_entry.py | 47 +++++++++++++++++++++++++++++-------
tests/qc_app/test_parse.py | 48 ++++++++++++++++++++++++++++++-------
tests/test_instance_dir/__init__.py | 0
5 files changed, 83 insertions(+), 18 deletions(-)
create mode 100644 tests/qc_app/__init__.py
create mode 100644 tests/test_instance_dir/__init__.py
(limited to 'tests')
diff --git a/tests/conftest.py b/tests/conftest.py
index 70bbd37..0072f26 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,4 +1,5 @@
"""Set up fixtures for tests"""
+import io
import os
import socket
import subprocess
@@ -38,3 +39,8 @@ def client(redis_server): # pylint: disable=[redefined-outer-name]
"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)
diff --git a/tests/qc_app/__init__.py b/tests/qc_app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/qc_app/test_entry.py b/tests/qc_app/test_entry.py
index d2908ee..31c43e1 100644
--- a/tests/qc_app/test_entry.py
+++ b/tests/qc_app/test_entry.py
@@ -1,5 +1,7 @@
"""Test the entry module in the web-ui"""
-import io
+import pytest
+
+from tests.conftest import uploadable_file_object
def test_basic_elements_present_in_index_page(client):
"""
@@ -26,7 +28,14 @@ def test_basic_elements_present_in_index_page(client):
assert b'Invalid file type provided.'
in response.data)
@@ -39,17 +48,37 @@ def test_post_with_correct_data(client):
GIVEN: A flask application testing client
WHEN: the index page is requested with the "POST" method and with the
appropriate data provided
- THEN: ....
- """
- with open("tests/test_data/no_data_errors.tsv", "br") as test_file:
- response = client.post(
- "/", data={
- "filetype": "average",
- "qc_text_file": (io.BytesIO(test_file.read()), "no_data_errors.tsv")
- })
+ THEN: ensure the system redirects to the parse endpoint with the filename
+ and filetype
+ """
+ response = client.post(
+ "/", data={
+ "filetype": "average",
+ "qc_text_file": uploadable_file_object("no_data_errors.tsv")
+ })
assert response.status_code == 302
assert b'Redirecting...' in response.data
assert (
b'/parse/parse?filename=no_data_errors.tsv&filetype=average'
in response.data)
+
+@pytest.mark.parametrize(
+ "request_data,error_message",
+ (({"filetype": "invalid_choice",
+ "qc_text_file": uploadable_file_object("no_data_errors.tsv")},
+ b'Invalid file type provided.'),
+ ({"filetype": "average"},
+ b'No file was uploaded.'),
+ ({"filetype": "standard-error"},
+ b'No file was uploaded.')))
+def test_post_with_missing_or_invalid_data(client, request_data,error_message):
+ """
+ GIVEN: A flask application testing client
+ WHEN: the index page is requested with the "POST" method and with data
+ either being missing or invalid
+ THEN: ensure that the system responds with the appropriate error message
+ """
+ response = client.post("/", data=request_data)
+ assert response.status_code == 200
+ assert error_message in response.data
diff --git a/tests/qc_app/test_parse.py b/tests/qc_app/test_parse.py
index 41d2c26..0a62df4 100644
--- a/tests/qc_app/test_parse.py
+++ b/tests/qc_app/test_parse.py
@@ -1,12 +1,42 @@
-import pytest
-
-def test_parse_with_existing_file(client, monkeypatch):
- monkeypatch.setattr(
- "qc_app.jobs.uuid4", lambda : "934c55d8-396e-4959-90e1-2698e9205758")
- resp = client.get(
- "/parse/parse?filename=no_data_errors.tsv&filetype=average")
- print(resp.status)
- print(resp.data)
+"""Test the 'parse' module in the web-ui"""
+import redis
+
+from qc_app.jobs import job
+from tests.conftest import uploadable_file_object
+
+def module_uuid4():
+ "module patch for the `uuid.uuid4()` function"
+ return "934c55d8-396e-4959-90e1-2698e9205758"
+
+def test_parse_with_existing_uploaded_file(client, redis_server, 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", module_uuid4)
+ # 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_server, decode_responses=True) as rconn:
+ the_job = job(rconn, module_uuid4())
+
+ assert the_job["job_id"] == module_uuid4()
+ assert the_job["filename"] == filename
+ assert the_job["command"] == " ".join([
+ "python3", "-m", "scripts.worker", filetype,
+ f"{client.application.config['UPLOAD_FOLDER']}/{filename}", redis_server,
+ module_uuid4()])
diff --git a/tests/test_instance_dir/__init__.py b/tests/test_instance_dir/__init__.py
new file mode 100644
index 0000000..e69de29
--
cgit v1.2.3