From 6760d322637a3d875242a66e9c1a784866d7df1d Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 15 Jun 2022 08:05:11 +0300 Subject: Setup test fixtures and initial tests for web-UI --- tests/conftest.py | 30 +++++++++++++++++++++ tests/qc_app/test_entry.py | 55 +++++++++++++++++++++++++++++++++++++++ tests/qc_app/test_parse.py | 12 +++++++++ tests/test_instance_dir/config.py | 11 ++++++++ 4 files changed, 108 insertions(+) create mode 100644 tests/qc_app/test_entry.py create mode 100644 tests/qc_app/test_parse.py create mode 100644 tests/test_instance_dir/config.py (limited to 'tests') diff --git a/tests/conftest.py b/tests/conftest.py index 6ef5374..70bbd37 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,40 @@ """Set up fixtures for tests""" +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() diff --git a/tests/qc_app/test_entry.py b/tests/qc_app/test_entry.py new file mode 100644 index 0000000..d2908ee --- /dev/null +++ b/tests/qc_app/test_entry.py @@ -0,0 +1,55 @@ +"""Test the entry module in the web-ui""" +import io + +def test_basic_elements_present_in_index_page(client): + """ + GIVEN: A flask application testing client + WHEN: the index page is requested with the "POST" method and no datat + THEN: verify that the response contains error notifications + """ + response = client.get("/") + assert response.status_code == 200 + ## form present + assert b'