import logging import os import mock import pytest from mlair.configuration import prepare_host, set_experiment_name, set_bootstrap_path, check_path_and_create, \ set_experiment_path, ROOT_PATH from mlair.helpers import PyTestRegex class TestPrepareHost: @mock.patch("socket.gethostname", side_effect=["ZAM144", "zam347", "jrtest", "jwtest", "runner-6HmDp9Qd-project-2411-concurrent-01"]) @mock.patch("getpass.getuser", return_value="testUser") @mock.patch("os.path.exists", return_value=True) def test_prepare_host(self, mock_host, mock_user, mock_path): assert prepare_host() == "/home/testUser/Data/toar_daily/" assert prepare_host() == "/home/testUser/Data/toar_daily/" assert prepare_host() == "/p/project/cjjsc42/testUser/DATA/toar_daily/" assert prepare_host() == "/p/project/deepacf/intelliaq/testUser/DATA/MLAIR/" assert prepare_host() == '/home/testUser/mlair/data/' @mock.patch("socket.gethostname", return_value="NotExistingHostName") @mock.patch("getpass.getuser", return_value="zombie21") def test_prepare_host_unknown(self, mock_user, mock_host): assert prepare_host() == os.path.join(os.path.abspath(os.getcwd()), 'data') @mock.patch("getpass.getuser", return_value="zombie21") @mock.patch("mlair.configuration.path_config.check_path_and_create", side_effect=PermissionError) @mock.patch("os.path.exists", return_value=False) def test_error_handling(self, mock_path_exists, mock_cpath, mock_user): # if "runner-6HmDp9Qd-project-2411-concurrent" not in platform.node(): # mock_host.return_value = "linux-aa9b" with pytest.raises(NotADirectoryError) as e: prepare_host() assert PyTestRegex(r"path '.*' does not exist for host '.*'\.") == e.value.args[0] with pytest.raises(NotADirectoryError) as e: prepare_host(False) # assert "does not exist for host 'linux-aa9b'" in e.value.args[0] assert PyTestRegex(r"path '.*' does not exist for host '.*'\.") == e.value.args[0] @mock.patch("socket.gethostname", side_effect=["zam347"]) @mock.patch("getpass.getuser", return_value="testUser") @mock.patch("os.path.exists", return_value=False) @mock.patch("os.makedirs", side_effect=None) def test_os_path_exists(self, mock_host, mock_user, mock_path, mock_check): path = prepare_host() assert path == "/home/testUser/Data/toar_daily/" class TestSetExperimentName: def test_set_experiment_name(self): exp_name = set_experiment_name() assert exp_name == "TestExperiment" exp_name = set_experiment_name(name="2019-11-14") assert exp_name == "2019-11-14_network" def test_set_experiment_name_sampling(self): exp_name = set_experiment_name(sampling="hourly") assert exp_name == "TestExperiment_hourly" exp_name = set_experiment_name(sampling="daily") assert exp_name == "TestExperiment_daily" def test_set_experiment_path(self): exp_path = set_experiment_path("TestExperiment") assert exp_path == os.path.abspath(os.path.join(ROOT_PATH, "TestExperiment")) exp_path = set_experiment_path(name="2019-11-14_network", path=os.path.join(os.path.dirname(__file__), "test2")) assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "test2", "2019-11-14_network")) def test_set_experiment_path_given_path(self): exp_path = set_experiment_path("TestExperiment", path=os.path.dirname(__file__)) assert exp_path == os.path.abspath(os.path.join(os.path.dirname(__file__), "TestExperiment")) class TestSetBootstrapPath: @mock.patch("os.makedirs", side_effect=None) def test_bootstrap_path_is_none(self, mock_makedir): bootstrap_path = set_bootstrap_path(None, 'TestDataPath/') assert bootstrap_path == os.path.abspath('TestDataPath/bootstrap') @mock.patch("os.makedirs", side_effect=None) def test_bootstap_path_is_given(self, mock_makedir): bootstrap_path = set_bootstrap_path('Test/path/to/boots', None) assert bootstrap_path == os.path.abspath('./Test/path/to/boots') class TestCheckPath: def test_check_path_and_create(self, caplog): caplog.set_level(logging.DEBUG) path = 'data/test' assert not os.path.exists('data/test') check_path_and_create(path) assert os.path.exists('data/test') assert caplog.messages[0] == "Created path: data/test" check_path_and_create(path) assert caplog.messages[1] == "Path already exists: data/test" os.rmdir('data/test')