Skip to content

Commit f6bb454

Browse files
committed
refactor and add unit tests
1 parent c8cfb36 commit f6bb454

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

tests/unit/anndata/test_anndata_factory.py

+34-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Unit tests for the AnnDataFactory class."""
22

3+
from unittest.mock import patch
4+
35
import numpy as np
46
import pandas as pd
57
import pytest
@@ -8,29 +10,29 @@
810
from alphabase.psm_reader.keys import PsmDfCols
911

1012

11-
def test_initialization_with_missing_columns():
12-
"""Test that an error is raised when the input DataFrame is missing required columns."""
13-
psm_df = pd.DataFrame(
13+
def _get_test_psm_df():
14+
"""Return a test PSM DataFrame."""
15+
return pd.DataFrame(
1416
{
15-
PsmDfCols.RAW_NAME: ["raw1", "raw2"],
16-
PsmDfCols.PROTEINS: ["protein1", "protein2"],
17+
PsmDfCols.RAW_NAME: ["raw1", "raw1", "raw2"],
18+
PsmDfCols.PROTEINS: ["protein1", "protein2", "protein1"],
19+
PsmDfCols.INTENSITY: [100, 200, 300],
1720
}
1821
)
1922

23+
24+
def test_initialization_with_missing_columns():
25+
"""Test that an error is raised when the input DataFrame is missing required columns."""
26+
psm_df = _get_test_psm_df().drop(columns=[PsmDfCols.INTENSITY])
27+
2028
with pytest.raises(ValueError, match="Missing required columns: \['intensity'\]"):
2129
# when
2230
AnnDataFactory(psm_df)
2331

2432

2533
def test_create_anndata_with_valid_dataframe():
2634
"""Test that an AnnData object is created correctly from a valid input DataFrame."""
27-
psm_df = pd.DataFrame(
28-
{
29-
PsmDfCols.RAW_NAME: ["raw1", "raw1", "raw2"],
30-
PsmDfCols.PROTEINS: ["protein1", "protein2", "protein1"],
31-
PsmDfCols.INTENSITY: [100, 200, 300],
32-
}
33-
)
35+
psm_df = _get_test_psm_df()
3436
factory = AnnDataFactory(psm_df)
3537

3638
# when
@@ -39,7 +41,9 @@ def test_create_anndata_with_valid_dataframe():
3941
assert adata.shape == (2, 2)
4042
assert adata.obs_names.tolist() == ["raw1", "raw2"]
4143
assert adata.var_names.tolist() == ["protein1", "protein2"]
42-
assert np.array_equal(adata.X, np.array([[100, 200], [300, np.nan]]))
44+
assert np.array_equal(
45+
adata.X, np.array([[100, 200], [300, np.nan]]), equal_nan=True
46+
)
4347

4448

4549
def test_create_anndata_with_missing_intensity_values():
@@ -97,3 +101,20 @@ def test_create_anndata_with_empty_dataframe():
97101
adata = factory.create_anndata()
98102

99103
assert adata.shape == (0, 0)
104+
105+
106+
@patch("alphabase.psm_reader.psm_reader.psm_reader_provider.get_reader")
107+
def test_from_files(mock_reader):
108+
mock_reader.return_value.load.return_value = _get_test_psm_df()
109+
110+
factory = AnnDataFactory.from_files(["file1", "file2"], reader_type="maxquant")
111+
112+
# when
113+
adata = factory.create_anndata()
114+
115+
assert adata.shape == (2, 2)
116+
assert adata.obs_names.tolist() == ["raw1", "raw2"]
117+
assert adata.var_names.tolist() == ["protein1", "protein2"]
118+
assert np.array_equal(
119+
adata.X, np.array([[100, 200], [300, np.nan]]), equal_nan=True
120+
)

0 commit comments

Comments
 (0)