1+ import shutil
2+ import os
13import unittest
24
35from tests .test_helpers import TestHelper
6+ from MLC .config import get_working_directory
7+ from MLC .config import get_test_path
48from MLC .mlc_parameters .mlc_parameters import saved , Config
9+ from MLC .api .MLCLocal import MLCLocal
510
611from MLC .Simulation import Simulation
712from MLC .Application import Application , MLC_CALLBACKS
@@ -15,10 +20,26 @@ class ApplicationTest(unittest.TestCase):
1520 on_evaluate = 0
1621 on_new_generation = []
1722 on_finish = 0
23+ experiment_name = "test_application"
24+ workspace_dir = os .path .join (get_working_directory (), "workspace" )
25+ test_conf_path = os .path .join (get_test_path (), TestHelper .DEFAULT_CONF_FILENAME )
26+ experiment_dir = os .path .join (workspace_dir , experiment_name )
27+ mlc_local = None
1828
1929 @classmethod
2030 def setUpClass (cls ):
2131 TestHelper .load_default_configuration ()
32+ # Create the MLC workspace dir of this tests
33+ os .makedirs (ApplicationTest .workspace_dir )
34+
35+ # Create the MLCLocal and the workspace of MLC, and create
36+ # a new experiment to be used to test the callbacks
37+ ApplicationTest .mlc_local = MLCLocal (ApplicationTest .workspace_dir )
38+
39+ @classmethod
40+ def tearDownClass (cls ):
41+ # Erase the MLC workspace dir of this tests
42+ shutil .rmtree (ApplicationTest .workspace_dir )
2243
2344 def test_callback_on_evaluate (self ):
2445 with saved (Config .get_instance ()) as config :
@@ -37,18 +58,24 @@ def test_on_new_generation_callback(generation_number):
3758 def test_on_finish_callback ():
3859 ApplicationTest .on_finish += 1
3960
40- MLCRepository ._instance = None
41- simulation = Simulation ()
42-
43- app = Application (simulation , callbacks = {MLC_CALLBACKS .ON_START : test_on_start_callback ,
44- MLC_CALLBACKS .ON_EVALUATE : test_on_evaluate_callback ,
45- MLC_CALLBACKS .ON_NEW_GENERATION : test_on_new_generation_callback ,
46- MLC_CALLBACKS .ON_FINISH : test_on_finish_callback })
47- app .go (to_generation = 2 , from_generation = 0 )
61+ callbacks_dict = {MLC_CALLBACKS .ON_START : test_on_start_callback ,
62+ MLC_CALLBACKS .ON_EVALUATE : test_on_evaluate_callback ,
63+ MLC_CALLBACKS .ON_NEW_GENERATION : test_on_new_generation_callback ,
64+ MLC_CALLBACKS .ON_FINISH : test_on_finish_callback }
65+
66+ ApplicationTest .mlc_local .new_experiment (ApplicationTest .experiment_name ,
67+ ApplicationTest .test_conf_path )
68+ ApplicationTest .mlc_local .open_experiment (ApplicationTest .experiment_name )
69+ ApplicationTest .mlc_local .go (experiment_name = ApplicationTest .experiment_name ,
70+ to_generation = 2 ,
71+ from_generation = 0 ,
72+ callbacks = callbacks_dict )
73+ ApplicationTest .mlc_local .close_experiment (ApplicationTest .experiment_name )
74+ ApplicationTest .mlc_local .delete_experiment (ApplicationTest .experiment_name )
4875
4976 self .assertEqual (ApplicationTest .on_start , 1 )
50- self .assertEqual (ApplicationTest .on_evaluate , 2 * 10 )
51- self .assertEqual (ApplicationTest .on_new_generation , range (1 , 2 + 1 ))
77+ self .assertEqual (ApplicationTest .on_evaluate , 2 * 10 )
78+ self .assertEqual (ApplicationTest .on_new_generation , range (1 , 2 + 1 ))
5279 self .assertEqual (ApplicationTest .on_finish , 1 )
5380
5481 def test_list_of_callbacks (self ):
@@ -65,12 +92,17 @@ def test_on_start_callback():
6592 def test_on_start_callback_increment_2 ():
6693 ApplicationTest .on_start_counter_2 += 1
6794
68- MLCRepository ._instance = None
69- simulation = Simulation ()
7095 start_callbacks = [test_on_start_callback , test_on_start_callback_increment_2 ]
7196
72- app = Application (simulation , callbacks = {MLC_CALLBACKS .ON_START : start_callbacks })
73- app .go (to_generation = 2 , from_generation = 0 )
97+ ApplicationTest .mlc_local .new_experiment (ApplicationTest .experiment_name ,
98+ ApplicationTest .test_conf_path )
99+ ApplicationTest .mlc_local .open_experiment (ApplicationTest .experiment_name )
100+ ApplicationTest .mlc_local .go (experiment_name = ApplicationTest .experiment_name ,
101+ to_generation = 2 ,
102+ from_generation = 0 ,
103+ callbacks = {MLC_CALLBACKS .ON_START : start_callbacks })
104+ ApplicationTest .mlc_local .close_experiment (ApplicationTest .experiment_name )
105+ ApplicationTest .mlc_local .delete_experiment (ApplicationTest .experiment_name )
74106
75107 self .assertEqual (ApplicationTest .on_start , 1 )
76108 self .assertEqual (ApplicationTest .on_start_counter_2 , 1 )
0 commit comments