1
1
"""Unit tests for the AnnDataFactory class."""
2
2
3
+ from unittest .mock import patch
4
+
3
5
import numpy as np
4
6
import pandas as pd
5
7
import pytest
8
10
from alphabase .psm_reader .keys import PsmDfCols
9
11
10
12
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 (
14
16
{
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 ],
17
20
}
18
21
)
19
22
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
+
20
28
with pytest .raises (ValueError , match = "Missing required columns: \['intensity'\]" ):
21
29
# when
22
30
AnnDataFactory (psm_df )
23
31
24
32
25
33
def test_create_anndata_with_valid_dataframe ():
26
34
"""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 ()
34
36
factory = AnnDataFactory (psm_df )
35
37
36
38
# when
@@ -39,7 +41,9 @@ def test_create_anndata_with_valid_dataframe():
39
41
assert adata .shape == (2 , 2 )
40
42
assert adata .obs_names .tolist () == ["raw1" , "raw2" ]
41
43
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
+ )
43
47
44
48
45
49
def test_create_anndata_with_missing_intensity_values ():
@@ -97,3 +101,20 @@ def test_create_anndata_with_empty_dataframe():
97
101
adata = factory .create_anndata ()
98
102
99
103
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