Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests page 3 #319

Merged
merged 11 commits into from
Sep 17, 2024
4 changes: 2 additions & 2 deletions alphastats/gui/utils/overview_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def display_matrix():
st.markdown("**DataFrame used for analysis** *preview*")
st.markdown(text)

df = get_display_matrix(st.session_state.user_session_id)
csv = convert_df(st.session_state.dataset.mat, st.session_state.user_session_id)
df = get_display_matrix()
csv = convert_df(st.session_state.dataset.mat)

st.dataframe(df)

Expand Down
1 change: 1 addition & 0 deletions alphastats/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from alphastats.loader.MaxQuantLoader import *


# TODO: Currenlty only used by tests, but should maybe be used more widely
def load_data(file, type, **kwargs):
type = type.lower()
if type == "maxquant":
Expand Down
53 changes: 53 additions & 0 deletions tests/gui/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from streamlit.testing.v1 import AppTest
from alphastats.load_data import load_data
from pathlib import Path
from alphastats import DataSet
from io import BytesIO

# TODO: Turn the helpers into fixtures

APP_FOLDER = Path(__file__).parent / "../../alphastats/gui/"
TEST_INPUT_FILES_PATH = APP_FOLDER / "../../testfiles"


def print_session_state(apptest: AppTest):
"""Prints the session state of the AppTest object.
Not used productively, but for debugging purposes."""
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)


def create_dataset_alphapept():
JuliaS92 marked this conversation as resolved.
Show resolved Hide resolved
"""Creates a dataset object from the alphapept testfiles."""
loader = load_data(
file=str(TEST_INPUT_FILES_PATH / "alphapept/results_proteins.csv"),
type="alphapept",
)
metadata_path = TEST_INPUT_FILES_PATH / "alphapept/metadata.csv"
return DataSet(
loader=loader,
metadata_path=str(metadata_path),
sample_column="sample",
)


def data_buf(file_path: str):
"""Helper function to open a data file from the testfiles folder and return a BytesIO object.

Additionally add filename as attribute."""
with open(TEST_INPUT_FILES_PATH / file_path, "rb") as f:
buf = BytesIO(f.read())
buf.name = file_path.split("/")[-1]
return buf


def metadata_buf(file_path: str):
"""Helper function to open a metadata file from the testfiles folder and return a BytesIO object.

Additionally add filename as attribute and set the metadatafile in the session state."""
with open(TEST_INPUT_FILES_PATH / file_path, "rb") as f:
buf = BytesIO(f.read())
buf.name = file_path.split("/")[-1]
return buf
41 changes: 6 additions & 35 deletions tests/gui/test_02_import_data.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
from streamlit.testing.v1 import AppTest
from pathlib import Path
from unittest.mock import MagicMock, patch
from io import BytesIO
from .conftest import APP_FOLDER, data_buf, metadata_buf


def print_session_state(apptest: AppTest):
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)


APP_FOLDER = Path(__file__).parent / Path("../../alphastats/gui/")
TESTED_PAGE = f"{APP_FOLDER}/pages/02_Import Data.py"
TEST_INPUT_FILES_PATH = APP_FOLDER / "../../testfiles"


def test_page_02_loads_without_input():
Expand Down Expand Up @@ -69,26 +60,6 @@ def test_page_02_loads_example_data(mock_page_link: MagicMock):
assert "statistic_options" in at.session_state


def _data_buf(file_path: str):
"""Helper function to open a data file from the testfiles folder and return a BytesIO object.

Additionally add filename as attribute."""
with open(TEST_INPUT_FILES_PATH / file_path, "rb") as f:
buf = BytesIO(f.read())
buf.name = file_path.split("/")[-1]
return buf


def _metadata_buf(file_path: str, at: AppTest):
"""Helper function to open a metadata file from the testfiles folder and return a BytesIO object.

Additionally add filename as attribute and set the metadatafile in the session state."""
with open(TEST_INPUT_FILES_PATH / file_path, "rb") as f:
buf = BytesIO(f.read())
buf.name = file_path.split("/")[-1]
return buf


@patch("streamlit.file_uploader")
@patch(
"streamlit.page_link"
Expand All @@ -114,22 +85,22 @@ def test_page_02_loads_maxquant_testfiles(
at.run()

# User uploads the data file
mock_file_uploader.side_effect = [_data_buf(DATA_FILE), None]
mock_file_uploader.side_effect = [data_buf(DATA_FILE), None]
at.run()

# User uploads the metadata file
mock_file_uploader.side_effect = [
_data_buf(DATA_FILE),
_metadata_buf(METADATA_FILE, at),
data_buf(DATA_FILE),
metadata_buf(METADATA_FILE),
]
at.run()

assert not at.exception

# User clicks the Load Data button
mock_file_uploader.side_effect = [
_data_buf(DATA_FILE),
_metadata_buf(METADATA_FILE, at),
data_buf(DATA_FILE),
metadata_buf(METADATA_FILE),
]
at.button(key="_create_dataset").click()
at.run()
Expand Down
26 changes: 13 additions & 13 deletions tests/gui/test_03_data_overview.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
from streamlit.testing.v1 import AppTest
from pathlib import Path
from unittest.mock import MagicMock, patch
import pandas as pd
from io import BytesIO
from .conftest import create_dataset_alphapept, APP_FOLDER

TESTED_PAGE = f"{APP_FOLDER}/pages/03_Data Overview.py"

def print_session_state(apptest: AppTest):
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)

def test_page_03_loads_without_input():
"""Test if the page loads without any input and inititalizes the session state with the correct values."""
at = AppTest(TESTED_PAGE, default_timeout=200)
at.run()

assert not at.exception

APP_FOLDER = Path(__file__).parent / Path("../../alphastats/gui/")
APP_FILE = f"{APP_FOLDER}/pages/03_Data Overview.py"
TEST_INPUT_FILES = f"{APP_FOLDER}/../../testfiles"

def test_page_03_loads_with_input():
"""Test if the page loads with input and inititalizes the session state with the correct values."""
at = AppTest(TESTED_PAGE, default_timeout=200)
at.run()

def test_page_03_loads_without_input():
"""Test if the page loads without any input and inititalizes the session state with the correct values."""
at = AppTest(APP_FILE, default_timeout=200)
at.session_state["dataset"] = create_dataset_alphapept()
at.run()

assert not at.exception
36 changes: 6 additions & 30 deletions tests/gui/test_04_preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
from alphastats import DataSet
from alphastats.load_data import load_data
from streamlit.testing.v1 import AppTest
from pathlib import Path
from unittest.mock import MagicMock, patch
import pandas as pd
from io import BytesIO
from .conftest import create_dataset_alphapept, APP_FOLDER


APP_FOLDER = Path(__file__).parent / Path("../../alphastats/gui/")
APP_FILE = f"{APP_FOLDER}/pages/03_Preprocessing.py"
TEST_INPUT_FILES = f"{APP_FOLDER}/../../testfiles"


def print_session_state(apptest: AppTest):
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)


def create_dataset_alphapept():
loader = load_data(
file=TEST_INPUT_FILES + "/alphapept/results_proteins.csv", type="alphapept"
)
metadata_path = TEST_INPUT_FILES + "/alphapept/metadata.csv"
return DataSet(
loader=loader,
metadata_path=metadata_path,
sample_column="sample",
)
TESTED_PAGE = f"{APP_FOLDER}/pages/03_Preprocessing.py"


def test_page_04_loads_without_input():
"""Test if the page loads without any input and inititalizes the session state with the correct values."""
at = AppTest(APP_FILE, default_timeout=200)
at = AppTest(TESTED_PAGE, default_timeout=200)
at.run()

assert not at.exception


def test_page_04_loads_with_input():
"""Test if the page loads with input and inititalizes the session state with the correct values."""
at = AppTest(APP_FILE, default_timeout=200)
"""Test if the page loads with input and serves the processing interface correctly."""
at = AppTest(TESTED_PAGE, default_timeout=200)
at.run()

at.session_state["dataset"] = create_dataset_alphapept()
Expand All @@ -54,7 +30,7 @@ def test_page_04_loads_with_input():

def test_page_04_runs_preprocessreset_alphapept():
"""Test if the page preprocesses and resets preprocessing without exceptions."""
at = AppTest(APP_FILE, default_timeout=200)
at = AppTest(TESTED_PAGE, default_timeout=200)
at.run()

at.session_state["dataset"] = create_dataset_alphapept()
Expand Down
Loading