1- import json
21from pathlib import Path
32from typing import List
4- from unittest .mock import patch
53
64import pytest
7- import yaml
85from click .testing import CliRunner
96
107from vivarium_profiling .tools .cli import profile_sim
@@ -47,29 +44,19 @@ def run_profile_sim_command(
4744 assert result .exit_code == 0
4845
4946
50- def test_profile_sim_parameters ():
51- """Test that profile_sim has the expected parameters."""
52- expected_parameters = {
53- "model_specification" ,
54- "results_directory" ,
55- "skip_writing" ,
56- "skip_processing" ,
57- "profiler" ,
58- }
59- actual_parameters = {param .name for param in profile_sim .params }
60- assert actual_parameters == expected_parameters
61-
62-
63- @patch ("subprocess.run" )
64- def test_profile_sim_scalene_default (
65- mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
66- ):
47+ def test_profile_sim_scalene (mocker , runner : CliRunner , model_spec : str , results_dir : Path ):
6748 """Test profile_sim with scalene profiler (default)."""
6849 # Mock successful subprocess run
50+ mock_subprocess_run = mocker .patch ("subprocess.run" )
6951 mock_subprocess_run .return_value = None
7052
7153 # Run the command
72- run_profile_sim_command (runner , model_spec , results_dir )
54+ run_profile_sim_command (
55+ runner ,
56+ model_spec ,
57+ results_dir ,
58+ extra_args = ["--profiler" , "scalene" ],
59+ )
7360
7461 # Verify subprocess was called with correct scalene command
7562 mock_subprocess_run .assert_called_once ()
@@ -89,45 +76,39 @@ def test_profile_sim_scalene_default(
8976 assert outfile_path .endswith (".json" )
9077
9178
92- @patch ("subprocess.run" )
93- @patch ("pstats.Stats" )
79+ def test_profile_sim_cprofile_skip_processing (
80+ mocker , runner : CliRunner , model_spec : str , results_dir : Path
81+ ):
82+ """Test profile_sim with cprofile and processing skipped."""
83+ mocker .patch ("subprocess.run" )
84+ run_profile_sim_command (runner , model_spec , results_dir , ["--skip_processing" ])
85+
86+
9487def test_profile_sim_cprofile_with_processing (
95- mock_pstats , mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
88+ mocker , runner : CliRunner , model_spec : str , results_dir : Path
9689):
9790 """Test profile_sim with cprofile and post-processing enabled."""
91+ mock_subprocess_run = mocker .patch ("subprocess.run" )
9892 mock_subprocess_run .return_value = None
9993
94+ mock_pstats = mocker .patch ("pstats.Stats" )
10095 mock_stats_instance = mock_pstats .return_value
10196
102- run_profile_sim_command (runner , model_spec , results_dir , [ "--profiler" , "cprofile" ] )
97+ run_profile_sim_command (runner , model_spec , results_dir )
10398
10499 # Verify that pstats was used for processing
105100 mock_pstats .assert_called_once ()
106101 mock_stats_instance .sort_stats .assert_called_once_with ("cumulative" )
107102 mock_stats_instance .print_stats .assert_called_once ()
108103
109104
110- @patch ("subprocess.run" )
111- def test_profile_sim_cprofile_skip_processing (
112- mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
113- ):
114- """Test profile_sim with cprofile and processing skipped."""
115- mock_subprocess_run .return_value = None
116-
117- run_profile_sim_command (
118- runner , model_spec , results_dir , ["--profiler" , "cprofile" , "--skip_processing" ]
119- )
120-
121-
122- @patch ("subprocess.run" )
123105def test_profile_sim_skip_writing (
124- mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
106+ mocker , runner : CliRunner , model_spec : str , results_dir : Path
125107):
126108 """Test profile_sim with skip_writing flag."""
127- # Mock successful subprocess run
128- mock_subprocess_run . return_value = None
109+ mock_subprocess_run = mocker . patch ( " subprocess. run" )
110+ mocker . patch ( "pstats.Stats" )
129111
130- # Run the command with skip_writing
131112 run_profile_sim_command (runner , model_spec , results_dir , ["--skip_writing" ])
132113
133114 # Verify the config override was empty (indicating no output directory set)
@@ -137,12 +118,13 @@ def test_profile_sim_skip_writing(
137118 assert config_override == "{}"
138119
139120
140- @patch ("subprocess.run" )
141121def test_profile_sim_results_directory_structure (
142- mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
122+ mocker , runner : CliRunner , model_spec : str , results_dir : Path
143123):
144124 """Test that profile_sim creates the expected directory structure and file paths."""
145- mock_subprocess_run .return_value = None
125+ mocker .patch ("subprocess.run" )
126+ mocker .patch ("pstats.Stats" )
127+
146128 run_profile_sim_command (runner , model_spec , results_dir )
147129
148130 # Check that results directory exists and has timestamped subdirectory
@@ -157,13 +139,15 @@ def test_profile_sim_results_directory_structure(
157139 assert all (part .isdigit () for part in parts )
158140
159141
160- @patch ("subprocess.run" )
161142def test_profile_sim_extra_args (
162- mock_subprocess_run , runner : CliRunner , model_spec : str , results_dir : Path
143+ mocker , runner : CliRunner , model_spec : str , results_dir : Path
163144):
164145 """Test that profile_sim passes extra arguments to scalene."""
165- mock_subprocess_run .return_value = None
166- run_profile_sim_command (runner , model_spec , results_dir , ["--cpu-only" , "--html" ])
146+ mock_subprocess_run = mocker .patch ("subprocess.run" )
147+
148+ run_profile_sim_command (
149+ runner , model_spec , results_dir , ["--profiler" , "scalene" , "--cpu-only" , "--html" ]
150+ )
167151
168152 # Verify extra arguments were passed to scalene
169153 call_args = mock_subprocess_run .call_args [0 ][0 ]
0 commit comments