Skip to content

Commit

Permalink
tests: docstrings added
Browse files Browse the repository at this point in the history
  • Loading branch information
KristinaGomoryova committed Jul 3, 2024
1 parent 0abc77b commit 829527d
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/test_process_metadata_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ def dataframe() -> pd.DataFrame:

@pytest.fixture
def processed_dataframe() -> pd.DataFrame:
"""Creates a dataframe corresponding to processed metadata file.
Returns:
pd.DataFrame: Expected processed metadata dataframe.
"""
d = {
"sampleName": [
"1_instrumental_blank_01",
Expand Down Expand Up @@ -110,6 +115,11 @@ def processed_dataframe() -> pd.DataFrame:

@pytest.fixture
def alkanes() -> pd.DataFrame:
"""Creates a dataframe corresponding to processed alkane file.
Returns:
pd.DataFrame: Expected dataframe matching alkanes test file.
"""
d = {
"carbon_number": [12, 13, 14, 15, 16, 17, 18, 19, 20],
"rt": [2.8, 3.0, 3.3, 3.7, 4.2, 4.6, 5.0, 5.4, 5.7],
Expand All @@ -127,13 +137,24 @@ def alkanes() -> pd.DataFrame:
],
)
def test_read_file(file_name: str, dataframe: pd.DataFrame):
"""Test importing the metadata file into pandas dataframe.
Args:
file_name (str): The path to the input data.
dataframe (pd.DataFrame): Dataframe containing the metadata.
"""
file_path = __location__.joinpath("test_data", file_name)
# file_path = os.path.join("tests", "test_data", file_name)
actual = read_file(str(file_path))
assert actual.equals(dataframe)


def test_read_file_error(dataframe: pd.DataFrame):
"""Test throwing a value error if incorrect file format is supplied.
Args:
dataframe (pd.DataFrame): Dataframe containing the metadata.
"""
file_path = os.path.join("tests", "test_data", "batch_specification1.prn")
with pytest.raises(
ValueError,
Expand All @@ -143,20 +164,38 @@ def test_read_file_error(dataframe: pd.DataFrame):


def test_save_dataframe_as_tsv(dataframe: pd.DataFrame, tmp_path: str):
"""Test saving the dataframe in tsv format.
Args:
dataframe (pd.DataFrame): The metadata dataframe.
tmp_path (str): A path where the .TSV will be exported.
"""
out_path = os.path.join(tmp_path, "batch_specification1.tsv")
save_dataframe_as_tsv(dataframe, out_path)
actual = pd.read_csv(out_path, sep="\t")
assert actual.equals(dataframe)


def test_read_save_dataframe_as_tsv_error(dataframe: pd.DataFrame, tmp_path: str):
"""Test throwing a value error if provided <fileName> is of different format than TSV.
Args:
dataframe (pd.DataFrame): The metadata dataframe.
tmp_path (str): A path where the .TSV will be exported.
"""
out_path = os.path.join(tmp_path, "batch_specification1.prn")
with pytest.raises(ValueError, match=r"Unsupported file format. Please point to a TSV file."):
save_dataframe_as_tsv(dataframe, out_path)


@pytest.mark.skip(reason="Test fails due to a inconsistency in the input file (metadata)")
def test_process_metadata_file(processed_dataframe: pd.DataFrame, tmp_path: str):
"""Tests processing the metadata file.
Args:
processed_dataframe (pd.DataFrame): Metadata dataframe.
tmp_path (str): Path where the processed dataframe will be exported.
"""
file_path = os.path.join("tests", "test_data", "batch_specification1.csv")
out_path = os.path.join(tmp_path, "processed_batch_specification1.tsv")
process_metadata_file(file_path, out_path)
Expand All @@ -173,6 +212,12 @@ def test_process_metadata_file(processed_dataframe: pd.DataFrame, tmp_path: str)
],
)
def test_read_file_colnames_input(file_name: str, dataframe: pd.DataFrame):
"""Tests whether there are correct columns on the input.
Args:
file_name (str): The name of the metadata file.
dataframe (pd.DataFrame): Expected dataframe.
"""
file_path = __location__.joinpath("test_data", file_name)
# file_path = os.path.join("tests", "test_data", file_name)
actual_df = read_file(str(file_path))
Expand All @@ -182,6 +227,12 @@ def test_read_file_colnames_input(file_name: str, dataframe: pd.DataFrame):


def test_process_metadata_file_colnames_output(processed_dataframe: pd.DataFrame, tmp_path: str):
"""Tests whether the processed dataframe outputs correct columns.
Args:
processed_dataframe (pd.DataFrame): An expected processed dataframe.
tmp_path (str): A path where the processed dataframe is exported.
"""
file_path = os.path.join("tests", "test_data", "batch_specification1.csv")
out_path = os.path.join(tmp_path, "processed_batch_specification1.tsv")
process_metadata_file(file_path, out_path)
Expand All @@ -192,6 +243,12 @@ def test_process_metadata_file_colnames_output(processed_dataframe: pd.DataFrame


def test_process_alkane_ri_file(alkanes: pd.DataFrame, tmp_path: str):
"""Tests processing of the alkanes input file.
Args:
alkanes (pd.DataFrame): An expected alkane dataframe.
tmp_path (str): A path where the processed alkane file is exported.
"""
file_path = os.path.join("tests", "test_data", "Alkane_RI_ATHLETE_1.txt")
out_path = os.path.join(tmp_path, "processed_Alkane_RI_ATHLETE_1.tsv")
process_alkane_ri_file(file_path, out_path)
Expand Down

0 comments on commit 829527d

Please sign in to comment.