22import unittest
33from unittest .mock import MagicMock , mock_open , patch
44
5- from CodeEntropy .main_mcc import (
6- arg_map ,
7- load_config ,
8- main ,
9- merge_configs ,
10- setup_argparse ,
11- )
5+ from CodeEntropy .config .arg_config_manager import ConfigManager
6+ from CodeEntropy .main_mcc import main
127
138
149class test_maincc (unittest .TestCase ):
@@ -58,9 +53,12 @@ def test_load_config(self, mock_exists, mock_file):
5853 """
5954 Test loading a valid configuration file.
6055 """
56+
57+ arg_config = ConfigManager ()
58+
6159 self .setup_file (mock_file )
6260
63- config = load_config (self .config_file )
61+ config = arg_config . load_config (self .config_file )
6462
6563 self .assertIn ("run1" , config )
6664 self .assertEqual (
@@ -72,34 +70,35 @@ def test_load_config_file_not_found(self, mock_file):
7270 """
7371 Test loading a configuration file that does not exist.
7472 """
73+
74+ arg_config = ConfigManager ()
75+
7576 with self .assertRaises (FileNotFoundError ):
76- load_config (self .config_file )
77+ arg_config . load_config (self .config_file )
7778
78- @patch ( "CodeEntropy.main_mcc. load_config" , return_value = None )
79+ @patch . object ( ConfigManager , " load_config" , return_value = None )
7980 def test_no_cli_no_yaml (self , mock_load_config ):
80- """
81- Test behavior when no CLI arguments and no YAML file are provided.
82- Should raise an exception or use defaults.
83- """
81+ """Test behavior when no CLI arguments and no YAML file are provided."""
8482
8583 with self .assertRaises (ValueError ) as context :
8684 self .code_entropy ()
8785
88- self .assertTrue (
89- "No configuration file found, and no CLI arguments were provided."
90- in str ( context . exception )
86+ self .assertEqual (
87+ str ( context . exception ),
88+ "No configuration file found, and no CLI arguments were provided." ,
9189 )
9290
9391 def test_invalid_run_config_type (self ):
9492 """
9593 Test that passing an invalid type for run_config raises a TypeError.
9694 """
95+ arg_config = ConfigManager ()
9796 args = MagicMock ()
9897 invalid_configs = ["string" , 123 , 3.14 , ["list" ], {("tuple_key" ,): "value" }]
9998
10099 for invalid in invalid_configs :
101100 with self .assertRaises (TypeError ):
102- merge_configs (args , invalid )
101+ arg_config . merge_configs (args , invalid )
103102
104103 @patch (
105104 "argparse.ArgumentParser.parse_args" ,
@@ -124,7 +123,8 @@ def test_setup_argparse(self, mock_args):
124123 """
125124 Test parsing command-line arguments.
126125 """
127- parser = setup_argparse ()
126+ arg_config = ConfigManager ()
127+ parser = arg_config .setup_argparse ()
128128 args = parser .parse_args ()
129129 self .assertEqual (args .top_traj_file , ["/path/to/tpr" , "/path/to/trr" ])
130130 self .assertEqual (args .selection_string , "all" )
@@ -133,7 +133,8 @@ def test_cli_overrides_defaults(self):
133133 """
134134 Test if CLI parameters override default values.
135135 """
136- parser = setup_argparse ()
136+ arg_config = ConfigManager ()
137+ parser = arg_config .setup_argparse ()
137138 args = parser .parse_args (
138139 ["--top_traj_file" , "/cli/path" , "--selection_string" , "cli_value" ]
139140 )
@@ -146,27 +147,30 @@ def test_yaml_overrides_defaults(self):
146147 """
147148 run_config = {"top_traj_file" : ["/yaml/path" ], "selection_string" : "yaml_value" }
148149 args = argparse .Namespace ()
149- merged_args = merge_configs (args , run_config )
150+ arg_config = ConfigManager ()
151+ merged_args = arg_config .merge_configs (args , run_config )
150152 self .assertEqual (merged_args .top_traj_file , ["/yaml/path" ])
151153 self .assertEqual (merged_args .selection_string , "yaml_value" )
152154
153155 def test_cli_overrides_yaml (self ):
154156 """
155157 Test if CLI parameters override YAML parameters correctly.
156158 """
157- parser = setup_argparse ()
159+ arg_config = ConfigManager ()
160+ parser = arg_config .setup_argparse ()
158161 args = parser .parse_args (
159162 ["--top_traj_file" , "/cli/path" , "--selection_string" , "cli_value" ]
160163 )
161164 run_config = {"top_traj_file" : ["/yaml/path" ], "selection_string" : "yaml_value" }
162- merged_args = merge_configs (args , run_config )
165+ merged_args = arg_config . merge_configs (args , run_config )
163166 self .assertEqual (merged_args .top_traj_file , ["/cli/path" ])
164167 self .assertEqual (merged_args .selection_string , "cli_value" )
165168
166169 def test_merge_configs (self ):
167170 """
168171 Test merging default arguments with a run configuration.
169172 """
173+ arg_config = ConfigManager ()
170174 args = MagicMock (
171175 top_traj_file = None ,
172176 selection_string = None ,
@@ -199,7 +203,7 @@ def test_merge_configs(self):
199203 "force_partitioning" : 0.5 ,
200204 "waterEntropy" : False ,
201205 }
202- merged_args = merge_configs (args , run_config )
206+ merged_args = arg_config . merge_configs (args , run_config )
203207 self .assertEqual (merged_args .top_traj_file , ["/path/to/tpr" , "/path/to/trr" ])
204208 self .assertEqual (merged_args .selection_string , "all" )
205209
@@ -208,12 +212,26 @@ def test_default_values(self, mock_parse_args):
208212 """
209213 Test if argument parser assigns default values correctly.
210214 """
211- default_args = {arg : params ["default" ] for arg , params in arg_map .items ()}
215+ arg_config = ConfigManager ()
216+
217+ # Ensure every argument gets a sensible default
218+ default_args = {
219+ arg : params .get ("default" , False if "action" in params else None )
220+ for arg , params in arg_config .arg_map .items ()
221+ }
222+
223+ # Mock argparse to return expected defaults
212224 mock_parse_args .return_value = MagicMock (** default_args )
213- parser = setup_argparse ()
225+
226+ parser = arg_config .setup_argparse ()
214227 args = parser .parse_args ()
215- for arg , params in arg_map .items ():
216- self .assertEqual (getattr (args , arg ), params ["default" ])
228+
229+ # Compare parsed args with expected defaults
230+ for arg , params in arg_config .arg_map .items ():
231+ expected_default = params .get (
232+ "default" , False if "action" in params else None
233+ )
234+ self .assertEqual (getattr (args , arg ), expected_default )
217235
218236 @patch (
219237 "argparse.ArgumentParser.parse_args" , return_value = MagicMock (top_traj_file = None )
@@ -222,7 +240,8 @@ def test_missing_required_arguments(self, mock_args):
222240 """
223241 Test behavior when required arguments are missing.
224242 """
225- parser = setup_argparse ()
243+ arg_config = ConfigManager ()
244+ parser = arg_config .setup_argparse ()
226245 args = parser .parse_args ()
227246 with self .assertRaises (ValueError ):
228247 if not args .top_traj_file :
@@ -234,7 +253,8 @@ def test_invalid_argument_type(self):
234253 """
235254 Test handling of invalid argument types.
236255 """
237- parser = setup_argparse ()
256+ arg_config = ConfigManager ()
257+ parser = arg_config .setup_argparse ()
238258 with self .assertRaises (SystemExit ):
239259 parser .parse_args (["--start" , "invalid" ])
240260
@@ -245,7 +265,8 @@ def test_edge_case_argument_values(self, mock_args):
245265 """
246266 Test parsing of edge case values.
247267 """
248- parser = setup_argparse ()
268+ arg_config = ConfigManager ()
269+ parser = arg_config .setup_argparse ()
249270 args = parser .parse_args ()
250271 self .assertEqual (args .start , - 1 )
251272 self .assertEqual (args .end , - 10 )
@@ -257,7 +278,10 @@ def test_empty_yaml_config(self, mock_exists, mock_file):
257278 Test behavior when an empty YAML file is provided.
258279 Should use defaults or raise an appropriate error.
259280 """
260- config = load_config (self .config_file )
281+
282+ arg_config = ConfigManager ()
283+
284+ config = arg_config .load_config (self .config_file )
261285
262286 self .assertIsInstance (config , dict )
263287 self .assertEqual (config , {})
0 commit comments