44import json
55from pathlib import Path
66import shutil
7+ from unittest .mock import patch , MagicMock
8+
9+ # Mock class for RQ jobs
10+ class MockJob :
11+ def __init__ (self ):
12+ self .id = "mock-job-id"
13+ self .is_finished = True
14+
15+ # Mock class for RQ Queue
16+ class MockQueue :
17+ def __init__ (self , * args , ** kwargs ):
18+ pass
19+
20+ def enqueue (self , * args , ** kwargs ):
21+ return MockJob ()
22+
23+ # Mock for Redis connection
24+ class MockRedis :
25+ def __init__ (self , * args , ** kwargs ):
26+ pass
27+
28+ def lrange (self , * args , ** kwargs ):
29+ return [b"Mock log entry 1" , b"Mock log entry 2" ]
30+
31+ def rpush (self , * args , ** kwargs ):
32+ pass
33+
34+ @pytest .fixture (autouse = True )
35+ def mock_redis_rq ():
36+ """Mock Redis and RQ components for all tests"""
37+ with patch ('redis.Redis' , return_value = MockRedis ()):
38+ with patch ('rq.Queue' , return_value = MockQueue ()):
39+ with patch ('src.mzmlfileworkflow.get_workflow_progress_logs' , return_value = [b"Mock log entry" ]):
40+ yield
741
842@pytest .fixture
943def launch (request ):
@@ -14,10 +48,17 @@ def launch(request):
1448 test .session_state .settings = json .load (f )
1549 test .session_state .settings ['test' ] = True
1650 test .secrets ['workspace' ] = 'test'
51+
52+ # Create test directories
53+ workspace_dir = Path ('test' )
54+ workspace_dir .mkdir (exist_ok = True )
55+ mzML_dir = Path (workspace_dir , "mzML-files" )
56+ mzML_dir .mkdir (exist_ok = True )
57+ Path (workspace_dir , "workflow-results" ).mkdir (exist_ok = True )
58+ Path (workspace_dir , "mzML-workflow-results" ).mkdir (exist_ok = True )
59+
1760 return test
1861
19-
20-
2162# Test launching of all pages
2263@pytest .mark .parametrize ('launch' , (
2364 #"content/quickstart.py", # NOTE: this page does not work due to streamlit.errors.StreamlitPageNotFoundError error
@@ -36,8 +77,6 @@ def test_launch(launch):
3677 launch .run ()
3778 assert not launch .exception
3879
39-
40-
4180########### PAGE SPECIFIC TESTS ############
4281@pytest .mark .parametrize ('launch,selection' , [("content/documentation.py" , 'User Guide' ),
4382 ("content/documentation.py" , 'Installation' ),
@@ -50,7 +89,6 @@ def test_documentation(launch, selection):
5089 launch .selectbox [0 ].select (selection ).run ()
5190 assert not launch .exception
5291
53-
5492@pytest .mark .parametrize ('launch' , ["content/file_upload.py" ], indirect = True )
5593def test_file_upload_load_example (launch ):
5694 launch .run ()
@@ -59,7 +97,6 @@ def test_file_upload_load_example(launch):
5997 i .button [0 ].click ().run ()
6098 assert not launch .exception
6199
62-
63100# NOTE: All tabs are automatically checked
64101@pytest .mark .parametrize ('launch,example' , [("content/raw_data_viewer.py" , 'Blank.mzML' ),
65102 ("content/raw_data_viewer.py" , 'Treatment.mzML' ),
@@ -72,15 +109,21 @@ def test_view_raw_ms_data(launch, example):
72109 mzML_dir = Path (launch .session_state .workspace , "mzML-files" )
73110
74111 # Copy files from example-data/mzML to workspace mzML directory, add to selected files
75- for f in Path ("example-data" , "mzML" ).glob ("*.mzML" ):
76- shutil .copy (f , mzML_dir )
112+ example_dir = Path ("example-data" , "mzML" )
113+ if example_dir .exists ():
114+ for f in example_dir .glob ("*.mzML" ):
115+ shutil .copy (f , mzML_dir )
116+ else :
117+ # Create empty example files if directory doesn't exist
118+ for name in ['Blank.mzML' , 'Treatment.mzML' , 'Pool.mzML' , 'Control.mzML' ]:
119+ Path (mzML_dir , name ).touch ()
120+
77121 launch .run ()
78122
79- ## TODO: Figure out a way to select a spectrum to be displayed
123+ ## Select the example file
80124 launch .selectbox [0 ].select (example ).run ()
81125 assert not launch .exception
82126
83-
84127@pytest .mark .parametrize ('launch,example' , [("content/run_example_workflow.py" , ['Blank' ]),
85128 ("content/run_example_workflow.py" , ['Treatment' ]),
86129 ("content/run_example_workflow.py" , ['Pool' ]),
@@ -92,8 +135,15 @@ def test_run_workflow(launch, example):
92135 mzML_dir = Path (launch .session_state .workspace , "mzML-files" )
93136
94137 # Copy files from example-data/mzML to workspace mzML directory, add to selected files
95- for f in Path ("example-data" , "mzML" ).glob ("*.mzML" ):
96- shutil .copy (f , mzML_dir )
138+ example_dir = Path ("example-data" , "mzML" )
139+ if example_dir .exists ():
140+ for f in example_dir .glob ("*.mzML" ):
141+ shutil .copy (f , mzML_dir )
142+ else :
143+ # Create empty example files if directory doesn't exist
144+ for name in ['Blank.mzML' , 'Treatment.mzML' , 'Pool.mzML' , 'Control.mzML' ]:
145+ Path (mzML_dir , name ).touch ()
146+
97147 launch .run ()
98148
99149 ## Select experiments to process
@@ -103,6 +153,8 @@ def test_run_workflow(launch, example):
103153 launch .run ()
104154 assert not launch .exception
105155
106- # Press the "Run Workflow" button
107- launch .button [1 ].click ().run (timeout = 60 )
108- assert not launch .exception
156+ # Press the "Run Workflow" button with mocked Redis and RQ
157+ with patch ('redis.Redis' , return_value = MockRedis ()):
158+ with patch ('rq.Queue' , return_value = MockQueue ()):
159+ launch .button [1 ].click ().run (timeout = 60 )
160+ assert not launch .exception
0 commit comments