|
4 | 4 | # which is available at https://spdx.org/licenses/BSD-3-Clause-Clear.html or # |
5 | 5 | # see the "LICENSE.md" file for more details. # |
6 | 6 | ###################################################################################### |
7 | | -"""Tests for executing fit multiple times on multi-table data""" |
| 7 | +"""Various integration tests""" |
8 | 8 |
|
9 | 9 | import os |
10 | 10 | import platform |
|
13 | 13 | import subprocess |
14 | 14 | import tempfile |
15 | 15 | import unittest |
| 16 | +from unittest.mock import MagicMock, patch |
16 | 17 |
|
17 | 18 | import khiops.core as kh |
| 19 | +import khiops.core.internals.filesystems as fs |
18 | 20 | from khiops.core.exceptions import KhiopsEnvironmentError |
19 | 21 | from khiops.core.internals.runner import KhiopsLocalRunner |
20 | 22 | from khiops.extras.docker import KhiopsDockerRunner |
@@ -208,6 +210,67 @@ def test_runner_environment_initialization(self): |
208 | 210 |
|
209 | 211 | self.assertEqual(env_khiops_api_mode, "true") |
210 | 212 |
|
| 213 | + def test_khiops_and_khiops_coclustering_are_run_with_mpi(self): |
| 214 | + """Test that MODL and MODL_Coclustering are run with MPI""" |
| 215 | + |
| 216 | + # Get current runner |
| 217 | + runner = kh.get_runner() |
| 218 | + |
| 219 | + # Get path to the Iris dataset |
| 220 | + iris_data_dir = fs.get_child_path(runner.samples_dir, "Iris") |
| 221 | + |
| 222 | + # Create the subprocess.Popen mock |
| 223 | + mock_popen = MagicMock() |
| 224 | + mock_popen.return_value.__enter__.return_value.communicate.return_value = ( |
| 225 | + b"", |
| 226 | + b"", |
| 227 | + ) |
| 228 | + mock_popen.return_value.__enter__.return_value.returncode = 0 |
| 229 | + |
| 230 | + # Run Khiops through an API function, using the mocked Popen, to capture |
| 231 | + # its arguments |
| 232 | + with patch("subprocess.Popen", mock_popen): |
| 233 | + kh.check_database( |
| 234 | + fs.get_child_path(iris_data_dir, "Iris.kdic"), |
| 235 | + "Iris", |
| 236 | + fs.get_child_path(iris_data_dir, "Iris.txt"), |
| 237 | + ) |
| 238 | + |
| 239 | + # Check that the mocked Popen call arguments list starts with the MPI |
| 240 | + # arguments, followed by the Khiops command |
| 241 | + expected_command_args = runner.mpi_command_args + [runner.khiops_path] |
| 242 | + self.assertTrue(len(mock_popen.call_args.args) > 0) |
| 243 | + self.assertTrue(len(mock_popen.call_args.args[0]) > len(expected_command_args)) |
| 244 | + self.assertEqual( |
| 245 | + mock_popen.call_args.args[0][: len(expected_command_args)], |
| 246 | + expected_command_args, |
| 247 | + ) |
| 248 | + |
| 249 | + # Run Khiops Coclustering through an API function, using the mocked Popen |
| 250 | + # to capture its arguments |
| 251 | + # Nest context managers for Python 3.8 compatibility |
| 252 | + with patch("subprocess.Popen", mock_popen): |
| 253 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 254 | + kh.train_coclustering( |
| 255 | + fs.get_child_path(iris_data_dir, "Iris.kdic"), |
| 256 | + "Iris", |
| 257 | + fs.get_child_path(iris_data_dir, "Iris.txt"), |
| 258 | + ["SepalLength", "PetalLength"], |
| 259 | + fs.get_child_path(temp_dir, "IrisCoclusteringResults.khcj"), |
| 260 | + ) |
| 261 | + |
| 262 | + # Check that the mocked Popen call arguments list starts with the MPI |
| 263 | + # arguments, followed by the Khiops Coclustering command |
| 264 | + expected_command_args = runner.mpi_command_args + [ |
| 265 | + runner.khiops_coclustering_path |
| 266 | + ] |
| 267 | + self.assertTrue(len(mock_popen.call_args.args) > 0) |
| 268 | + self.assertTrue(len(mock_popen.call_args.args[0]) > len(expected_command_args)) |
| 269 | + self.assertEqual( |
| 270 | + mock_popen.call_args.args[0][: len(expected_command_args)], |
| 271 | + expected_command_args, |
| 272 | + ) |
| 273 | + |
211 | 274 |
|
212 | 275 | class KhiopsMultitableFitTests(unittest.TestCase): |
213 | 276 | """Test if Khiops estimator can be fitted on multi-table data""" |
|
0 commit comments