Skip to content

Commit 9d9f6d1

Browse files
committed
Added tests for the import experiment feature
* #35
1 parent f2a7410 commit 9d9f6d1

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

MLC/GUI/ExperimentsManager.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44

55
from MLC.api.mlc import DuplicatedExperimentError
6+
from MLC.api.mlc import ImportExperimentPathNotExistException
67
from MLC.Common import util
78
from MLC.Log.log import get_gui_logger
89
from MLC.mlc_parameters.mlc_parameters import Config
@@ -100,7 +101,19 @@ def remove_experiment(self, experiment_name):
100101

101102
def import_experiment(self, experiment_path):
102103
experiment_name = os.path.split(experiment_path)[1].split(".")[0]
103-
self._mlc_local.import_experiment(experiment_path)
104+
try:
105+
self._mlc_local.import_experiment(experiment_path)
106+
except DuplicatedExperimentError:
107+
logger.error("Experiment {0} could not be imported. "
108+
"It already exists.".format(experiment_name))
109+
except ImportExperimentPathNotExistException, err:
110+
logger.error("Experiment {0} could not be imported. "
111+
"Experiment path given does not exists. Exception msg: {1}"
112+
.format(experiment_name, err))
113+
except Exception, err:
114+
logger.error("Experiment {0} could not be imported. {1}"
115+
.format(experiment_name, err))
116+
104117
self._experiment_list.append(experiment_name)
105118
self.load_experiment_info(experiment_name)
106119

MLC/api/MLCLocal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from MLC.api.mlc import InvalidExperimentException
1717
from MLC.api.mlc import EvaluationScriptNotExistException
1818
from MLC.api.mlc import PreevaluationScriptNotExistException
19+
from MLC.api.mlc import ImportExperimentPathNotExistException
1920
from MLC.Application import Application
2021
from MLC.config import get_templates_path
2122
from MLC.config import set_working_directory
@@ -117,6 +118,9 @@ def delete_experiment(self, experiment_name):
117118
logger.info("[MLC_LOCAL] Error while trying to delete experiment file: {0}".format(file))
118119

119120
def import_experiment(self, experiment_path):
121+
if not os.path.exists(experiment_path):
122+
raise ImportExperimentPathNotExistException(experiment_path)
123+
120124
experiment_name = os.path.split(experiment_path)[1].split(".")[0]
121125
self._create_experiment_dir(experiment_name)
122126

MLC/api/mlc.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ def __init__(self, experiment_name, script_path):
3535
"existent evaluation script. Script: {1}"
3636
.format(experiment_name, script_path))
3737

38+
class ImportExperimentPathNotExistException(MLCException):
39+
40+
def __init__(self, experiment_path):
41+
MLCException.__init__(self, "Import Experiment Error: Path {0} does not exists."
42+
.format(experiment_path))
3843

3944
class MLC:
4045

tests/mlc/api/test_mlc.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class MLCWorkspaceTest(unittest.TestCase):
2727
NEW_EXPERIMENT = "test_new_experiment"
2828
NEW_CONFIGURATION = None
2929

30-
3130
@classmethod
3231
def setUpClass(cls):
3332
try:
@@ -296,6 +295,41 @@ def test_go_and_get_generations(self):
296295
os.unlink(os.path.join(MLCWorkspaceTest.WORKSPACE_DIR, "test_go_and_check") + ".mlc")
297296
pass
298297

298+
def test_import_non_existent_experiment(self):
299+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
300+
301+
import_experiment_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
302+
"test4.mlc")
303+
mlc.import_experiment(import_experiment_path)
304+
self.assertTrue("test4" in mlc.get_workspace_experiments())
305+
mlc.delete_experiment("test4")
306+
307+
def test_import_existent_experiment(self):
308+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
309+
mlc.new_experiment("test4", MLCWorkspaceTest.ORIGINAL_CONFIGURATION)
310+
311+
import_experiment_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
312+
"test4.mlc")
313+
try:
314+
mlc.import_experiment(import_experiment_path)
315+
self.assertTrue(False)
316+
except MLC.api.mlc.DuplicatedExperimentError:
317+
self.assertTrue(True)
318+
finally:
319+
mlc.delete_experiment("test4")
320+
321+
def test_import_experiment_with_erroneous_path(self):
322+
mlc = MLCLocal(working_dir=MLCWorkspaceTest.WORKSPACE_DIR)
323+
import_experiment_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
324+
"test5.mlc")
325+
try:
326+
mlc.import_experiment(import_experiment_path)
327+
self.assertTrue(False)
328+
mlc.delete_experiment("test5")
329+
except MLC.api.mlc.ImportExperimentPathNotExistException:
330+
self.assertTrue(True)
331+
self.assertFalse("test5" in mlc.get_workspace_experiments())
332+
299333
def _assert_key_value(self, dictionary, key, value):
300334
self.assertIsInstance(dictionary, dict)
301335
self.assertIn(key, dictionary)

0 commit comments

Comments
 (0)