about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-06-16 13:33:31 +0300
committerFrederick Muriuki Muriithi2022-06-16 13:33:31 +0300
commitc6b182d346dcf6d3b1cb98956aa3066067f00757 (patch)
tree4f40b729aea86ce8da93fa4da6244d186f0e87c1
parentd2605cb72d7cdbc7d3cc633b94a451c0acd2edbb (diff)
downloadgn-uploader-c6b182d346dcf6d3b1cb98956aa3066067f00757.tar.gz
Add more UI tests
- Test upload with missing or invalid data
- Test triggering the parsing of the file
-rw-r--r--tests/conftest.py6
-rw-r--r--tests/qc_app/__init__.py0
-rw-r--r--tests/qc_app/test_entry.py47
-rw-r--r--tests/qc_app/test_parse.py48
-rw-r--r--tests/test_instance_dir/__init__.py0
5 files changed, 83 insertions, 18 deletions
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
--- /dev/null
+++ b/tests/qc_app/__init__.py
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'<input type="submit" value="upload file"' in response.data
 
 def test_post_notifies_errors_if_no_data_is_provided(client):
+    """
+    GIVEN: A flask application testing client
+    WHEN: the index page is requested with the "POST" method and with no
+          data provided
+    THEN: ensure the system responds woit the appropriate error messages
+    """
     response = client.post("/", data={})
+    assert response.status_code == 200
     assert (
         b'<span class="alert alert-error">Invalid file type provided.</span>'
         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&amp;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'<span class="alert alert-error">Invalid file type provided.</span>'),
+     ({"filetype": "average"},
+      b'<span class="alert alert-error">No file was uploaded.</span>'),
+     ({"filetype": "standard-error"},
+      b'<span class="alert alert-error">No file was uploaded.</span>')))
+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
--- /dev/null
+++ b/tests/test_instance_dir/__init__.py