|
| 1 | +import os |
1 | 2 | import unittest |
2 | 3 | from unittest.mock import MagicMock, patch |
3 | 4 |
|
@@ -122,6 +123,92 @@ def test_cli_invalid_pattern(self): |
122 | 123 | self.assertNotEqual(result.exit_code, 0) |
123 | 124 | self.assertIn("Invalid value for '--pattern'", result.output) |
124 | 125 |
|
| 126 | + @patch("tracestorm.cli.run_load_test") |
| 127 | + @patch("tracestorm.cli.os.makedirs") |
| 128 | + @patch("tracestorm.cli.datetime") |
| 129 | + def test_cli_with_output_dir( |
| 130 | + self, mock_datetime, mock_makedirs, mock_run_load_test |
| 131 | + ): |
| 132 | + """Test CLI with output directory option.""" |
| 133 | + mock_analyzer = MagicMock() |
| 134 | + mock_run_load_test.return_value = ([], mock_analyzer) |
| 135 | + mock_datetime.datetime.now.return_value.strftime.return_value = ( |
| 136 | + "20240101_120000" |
| 137 | + ) |
| 138 | + |
| 139 | + # Test with explicit output dir |
| 140 | + result = self.runner.invoke( |
| 141 | + main, |
| 142 | + [ |
| 143 | + "--model", |
| 144 | + "gpt-3.5-turbo", |
| 145 | + "--output-dir", |
| 146 | + "custom_output_dir", |
| 147 | + ], |
| 148 | + ) |
| 149 | + |
| 150 | + self.assertEqual(result.exit_code, 0) |
| 151 | + mock_makedirs.assert_called_with("custom_output_dir", exist_ok=True) |
| 152 | + mock_analyzer.export_json.assert_called_once() |
| 153 | + |
| 154 | + # Reset mocks |
| 155 | + mock_makedirs.reset_mock() |
| 156 | + mock_analyzer.reset_mock() |
| 157 | + |
| 158 | + # Test with default output dir |
| 159 | + result = self.runner.invoke( |
| 160 | + main, |
| 161 | + [ |
| 162 | + "--model", |
| 163 | + "gpt-3.5-turbo", |
| 164 | + ], |
| 165 | + ) |
| 166 | + |
| 167 | + self.assertEqual(result.exit_code, 0) |
| 168 | + mock_makedirs.assert_called_with( |
| 169 | + os.path.join("tracestorm_results", "20240101_120000"), exist_ok=True |
| 170 | + ) |
| 171 | + mock_analyzer.export_json.assert_called_once() |
| 172 | + |
| 173 | + @patch("tracestorm.cli.run_load_test") |
| 174 | + @patch("tracestorm.cli.os.makedirs") |
| 175 | + def test_cli_with_plot_option(self, mock_makedirs, mock_run_load_test): |
| 176 | + """Test CLI with plot option.""" |
| 177 | + mock_analyzer = MagicMock() |
| 178 | + mock_run_load_test.return_value = ([], mock_analyzer) |
| 179 | + |
| 180 | + # Test with plot enabled |
| 181 | + result = self.runner.invoke( |
| 182 | + main, |
| 183 | + [ |
| 184 | + "--model", |
| 185 | + "gpt-3.5-turbo", |
| 186 | + "--plot", |
| 187 | + "--output-dir", |
| 188 | + "test_dir", |
| 189 | + ], |
| 190 | + ) |
| 191 | + |
| 192 | + self.assertEqual(result.exit_code, 0) |
| 193 | + mock_analyzer.plot_cdf.assert_called_once() |
| 194 | + |
| 195 | + # Reset mock |
| 196 | + mock_analyzer.reset_mock() |
| 197 | + |
| 198 | + # Test with plot disabled (default) |
| 199 | + result = self.runner.invoke( |
| 200 | + main, |
| 201 | + [ |
| 202 | + "--model", |
| 203 | + "gpt-3.5-turbo", |
| 204 | + "--output-dir", |
| 205 | + "test_dir", |
| 206 | + ], |
| 207 | + ) |
| 208 | + |
| 209 | + self.assertEqual(result.exit_code, 0) |
| 210 | + mock_analyzer.plot_cdf.assert_not_called() |
| 211 | + |
125 | 212 |
|
126 | 213 | if __name__ == "__main__": |
127 | 214 | unittest.main() |
0 commit comments