-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_arg_config_manager.py
More file actions
596 lines (503 loc) · 20.5 KB
/
test_arg_config_manager.py
File metadata and controls
596 lines (503 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import argparse
import logging
import os
import shutil
import tempfile
import unittest
from unittest.mock import MagicMock, mock_open, patch
import tests.data as data
from CodeEntropy.config.arg_config_manager import ConfigManager
from CodeEntropy.main import main
class test_arg_config_manager(unittest.TestCase):
"""
Unit tests for the ConfigManager.
"""
def setUp(self):
"""
Setup test data and output directories.
"""
self.test_data_dir = os.path.dirname(data.__file__)
self.test_dir = tempfile.mkdtemp(prefix="CodeEntropy_")
self.config_file = os.path.join(self.test_dir, "config.yaml")
# Create a mock config file
with patch("builtins.open", new_callable=mock_open) as mock_file:
self.setup_file(mock_file)
with open(self.config_file, "w") as f:
f.write(mock_file.return_value.read())
# Change to test directory
self._orig_dir = os.getcwd()
os.chdir(self.test_dir)
def tearDown(self):
"""
Clean up after each test.
"""
os.chdir(self._orig_dir)
if os.path.exists(self.test_dir):
shutil.rmtree(self.test_dir)
def list_data_files(self):
"""
List all files in the test data directory.
"""
return os.listdir(self.test_data_dir)
def setup_file(self, mock_file):
"""
Mock the contents of a configuration file.
"""
mock_file.return_value = mock_open(
read_data="--- \n \nrun1:\n "
"top_traj_file: ['/path/to/tpr', '/path/to/trr']\n "
"selection_string: 'all'\n "
"start: 0\n "
"end: -1\n "
"step: 1\n "
"bin_width: 30\n "
"tempra: 298.0\n "
"verbose: False\n "
"thread: 1\n "
"output_file: 'output_file.json'\n "
"force_partitioning: 0.5\n "
"water_entropy: False"
).return_value
@patch("builtins.open", new_callable=mock_open)
@patch("os.path.exists", return_value=True)
def test_load_config(self, mock_exists, mock_file):
"""
Test loading a valid configuration file.
"""
arg_config = ConfigManager()
self.setup_file(mock_file)
config = arg_config.load_config(self.config_file)
self.assertIn("run1", config)
self.assertEqual(
config["run1"]["top_traj_file"], ["/path/to/tpr", "/path/to/trr"]
)
@patch("builtins.open", side_effect=FileNotFoundError)
def test_load_config_file_not_found(self, mock_file):
"""
Test loading a configuration file that does not exist.
"""
arg_config = ConfigManager()
with self.assertRaises(FileNotFoundError):
arg_config.load_config(self.config_file)
@patch.object(ConfigManager, "load_config", return_value=None)
def test_no_cli_no_yaml(self, mock_load_config):
"""Test behavior when no CLI arguments and no YAML file are provided."""
with self.assertRaises(SystemExit) as context:
main()
self.assertEqual(context.exception.code, 1)
def test_invalid_run_config_type(self):
"""
Test that passing an invalid type for run_config raises a TypeError.
"""
arg_config = ConfigManager()
args = MagicMock()
invalid_configs = ["string", 123, 3.14, ["list"], {("tuple_key",): "value"}]
for invalid in invalid_configs:
with self.assertRaises(TypeError):
arg_config.merge_configs(args, invalid)
@patch(
"argparse.ArgumentParser.parse_args",
return_value=MagicMock(
top_traj_file=["/path/to/tpr", "/path/to/trr"],
selection_string="all",
start=0,
end=-1,
step=1,
bin_width=30,
tempra=298.0,
verbose=False,
thread=1,
output_file="output_file.json",
force_partitioning=0.5,
water_entropy=False,
),
)
def test_setup_argparse(self, mock_args):
"""
Test parsing command-line arguments.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args()
self.assertEqual(args.top_traj_file, ["/path/to/tpr", "/path/to/trr"])
self.assertEqual(args.selection_string, "all")
@patch(
"argparse.ArgumentParser.parse_args",
return_value=MagicMock(
top_traj_file=["/path/to/tpr", "/path/to/trr"],
start=10,
water_entropy=False,
),
)
def test_setup_argparse_false_boolean(self, mock_args):
"""
Test that non-boolean arguments are parsed correctly.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args()
self.assertEqual(args.top_traj_file, ["/path/to/tpr", "/path/to/trr"])
self.assertEqual(args.start, 10)
self.assertFalse(args.water_entropy)
def test_str2bool_true_variants(self):
"""Test that various string representations of True are correctly parsed."""
arg_config = ConfigManager()
self.assertTrue(arg_config.str2bool("true"))
self.assertTrue(arg_config.str2bool("True"))
self.assertTrue(arg_config.str2bool("t"))
self.assertTrue(arg_config.str2bool("yes"))
self.assertTrue(arg_config.str2bool("1"))
def test_str2bool_false_variants(self):
"""Test that various string representations of False are correctly parsed."""
arg_config = ConfigManager()
self.assertFalse(arg_config.str2bool("false"))
self.assertFalse(arg_config.str2bool("False"))
self.assertFalse(arg_config.str2bool("f"))
self.assertFalse(arg_config.str2bool("no"))
self.assertFalse(arg_config.str2bool("0"))
def test_str2bool_boolean_passthrough(self):
"""Test that boolean values passed directly are returned unchanged."""
arg_config = ConfigManager()
self.assertTrue(arg_config.str2bool(True))
self.assertFalse(arg_config.str2bool(False))
def test_str2bool_invalid_input(self):
"""Test that invalid string inputs raise an ArgumentTypeError."""
arg_config = ConfigManager()
with self.assertRaises(Exception) as context:
arg_config.str2bool("maybe")
self.assertIn("Boolean value expected", str(context.exception))
def test_str2bool_empty_string(self):
"""Test that an empty string raises an ArgumentTypeError."""
arg_config = ConfigManager()
with self.assertRaises(Exception) as context:
arg_config.str2bool("")
self.assertIn("Boolean value expected", str(context.exception))
def test_str2bool_unexpected_number(self):
"""Test that unexpected numeric strings raise an ArgumentTypeError."""
arg_config = ConfigManager()
with self.assertRaises(Exception) as context:
arg_config.str2bool("2")
self.assertIn("Boolean value expected", str(context.exception))
def test_cli_overrides_defaults(self):
"""
Test if CLI parameters override default values.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args(
["--top_traj_file", "/cli/path", "--selection_string", "cli_value"]
)
self.assertEqual(args.top_traj_file, ["/cli/path"])
self.assertEqual(args.selection_string, "cli_value")
def test_cli_overrides_yaml(self):
"""
Test if CLI parameters override YAML parameters correctly.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args(
["--top_traj_file", "/cli/path", "--selection_string", "cli_value"]
)
run_config = {"top_traj_file": ["/yaml/path"], "selection_string": "yaml_value"}
merged_args = arg_config.merge_configs(args, run_config)
self.assertEqual(merged_args.top_traj_file, ["/cli/path"])
self.assertEqual(merged_args.selection_string, "cli_value")
def test_cli_overrides_yaml_with_multiple_values(self):
"""
Ensures that CLI arguments override YAML when multiple values are provided in
YAML.
"""
arg_config = ConfigManager()
yaml_config = {"top_traj_file": ["/yaml/path1", "/yaml/path2"]}
args = argparse.Namespace(top_traj_file=["/cli/path"])
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.top_traj_file, ["/cli/path"])
def test_yaml_overrides_defaults(self):
"""
Test if YAML parameters override default values.
"""
run_config = {"top_traj_file": ["/yaml/path"], "selection_string": "yaml_value"}
args = argparse.Namespace()
arg_config = ConfigManager()
merged_args = arg_config.merge_configs(args, run_config)
self.assertEqual(merged_args.top_traj_file, ["/yaml/path"])
self.assertEqual(merged_args.selection_string, "yaml_value")
def test_yaml_does_not_override_cli_if_set(self):
"""
Ensure YAML does not override CLI arguments that are set.
"""
arg_config = ConfigManager()
yaml_config = {"bin_width": 50}
args = argparse.Namespace(bin_width=100)
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.bin_width, 100)
def test_yaml_overrides_defaults_when_no_cli(self):
"""
Test if YAML parameters override default values when no CLI input is given.
"""
arg_config = ConfigManager()
yaml_config = {
"top_traj_file": ["/yaml/path"],
"bin_width": 50,
}
args = argparse.Namespace()
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.top_traj_file, ["/yaml/path"])
self.assertEqual(merged_args.bin_width, 50)
def test_yaml_none_does_not_override_defaults(self):
"""
Ensures that YAML values set to `None` do not override existing CLI values.
"""
arg_config = ConfigManager()
yaml_config = {"bin_width": None}
args = argparse.Namespace(bin_width=100)
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.bin_width, 100)
def test_hierarchy_cli_yaml_defaults(self):
"""
Test if CLI arguments override YAML, and YAML overrides defaults.
"""
arg_config = ConfigManager()
yaml_config = {
"top_traj_file": ["/yaml/path", "/yaml/path"],
"bin_width": "50",
}
args = argparse.Namespace(
top_traj_file=["/cli/path", "/cli/path"], bin_width=100
)
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.top_traj_file, ["/cli/path", "/cli/path"])
self.assertEqual(merged_args.bin_width, 100)
def test_merge_configs(self):
"""
Test merging default arguments with a run configuration.
"""
arg_config = ConfigManager()
args = MagicMock(
top_traj_file=None,
selection_string=None,
start=None,
end=None,
step=None,
bin_width=None,
tempra=None,
verbose=None,
thread=None,
output_file=None,
force_partitioning=None,
water_entropy=None,
)
run_config = {
"top_traj_file": ["/path/to/tpr", "/path/to/trr"],
"selection_string": "all",
"start": 0,
"end": -1,
"step": 1,
"bin_width": 30,
"tempra": 298.0,
"verbose": False,
"thread": 1,
"output_file": "output_file.json",
"force_partitioning": 0.5,
"water_entropy": False,
}
merged_args = arg_config.merge_configs(args, run_config)
self.assertEqual(merged_args.top_traj_file, ["/path/to/tpr", "/path/to/trr"])
self.assertEqual(merged_args.selection_string, "all")
def test_merge_with_none_yaml(self):
"""
Ensure merging still works if no YAML config is provided.
"""
arg_config = ConfigManager()
args = argparse.Namespace(top_traj_file=["/cli/path"])
yaml_config = None
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.top_traj_file, ["/cli/path"])
@patch("CodeEntropy.config.arg_config_manager.logger")
def test_merge_configs_sets_debug_logging(self, mock_logger):
"""
Ensure logging is set to DEBUG when verbose=True.
"""
arg_config = ConfigManager()
args = argparse.Namespace(verbose=True)
for key in arg_config.arg_map:
if not hasattr(args, key):
setattr(args, key, None)
# Mock logger handlers
mock_handler = MagicMock()
mock_logger.handlers = [mock_handler]
arg_config.merge_configs(args, {})
mock_logger.setLevel.assert_called_with(logging.DEBUG)
mock_handler.setLevel.assert_called_with(logging.DEBUG)
mock_logger.debug.assert_called_with(
"Verbose mode enabled. Logger set to DEBUG level."
)
@patch("CodeEntropy.config.arg_config_manager.logger")
def test_merge_configs_sets_info_logging(self, mock_logger):
"""
Ensure logging is set to INFO when verbose=False.
"""
arg_config = ConfigManager()
args = argparse.Namespace(verbose=False)
for key in arg_config.arg_map:
if not hasattr(args, key):
setattr(args, key, None)
# Mock logger handlers
mock_handler = MagicMock()
mock_logger.handlers = [mock_handler]
arg_config.merge_configs(args, {})
mock_logger.setLevel.assert_called_with(logging.INFO)
mock_handler.setLevel.assert_called_with(logging.INFO)
@patch("argparse.ArgumentParser.parse_args")
def test_default_values(self, mock_parse_args):
"""
Test if argument parser assigns default values correctly.
"""
arg_config = ConfigManager()
mock_parse_args.return_value = MagicMock(
top_traj_file=["example.top", "example.traj"]
)
parser = arg_config.setup_argparse()
args = parser.parse_args()
self.assertEqual(args.top_traj_file, ["example.top", "example.traj"])
def test_fallback_to_defaults(self):
"""
Ensure arguments fall back to defaults if neither YAML nor CLI provides them.
"""
arg_config = ConfigManager()
yaml_config = {}
args = argparse.Namespace()
merged_args = arg_config.merge_configs(args, yaml_config)
self.assertEqual(merged_args.step, 1)
self.assertEqual(merged_args.end, -1)
@patch(
"argparse.ArgumentParser.parse_args", return_value=MagicMock(top_traj_file=None)
)
def test_missing_required_arguments(self, mock_args):
"""
Test behavior when required arguments are missing.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args()
with self.assertRaises(ValueError):
if not args.top_traj_file:
raise ValueError(
"The 'top_traj_file' argument is required but not provided."
)
def test_invalid_argument_type(self):
"""
Test handling of invalid argument types.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
with self.assertRaises(SystemExit):
parser.parse_args(["--start", "invalid"])
@patch(
"argparse.ArgumentParser.parse_args", return_value=MagicMock(start=-1, end=-10)
)
def test_edge_case_argument_values(self, mock_args):
"""
Test parsing of edge case values.
"""
arg_config = ConfigManager()
parser = arg_config.setup_argparse()
args = parser.parse_args()
self.assertEqual(args.start, -1)
self.assertEqual(args.end, -10)
@patch("builtins.open", new_callable=mock_open, read_data="--- \n")
@patch("os.path.exists", return_value=True)
def test_empty_yaml_config(self, mock_exists, mock_file):
"""
Test behavior when an empty YAML file is provided.
Should use defaults or raise an appropriate error.
"""
arg_config = ConfigManager()
config = arg_config.load_config(self.config_file)
self.assertIsInstance(config, dict)
self.assertEqual(config, {})
def test_input_parameters_validation_all_valid(self):
"""Test that input_parameters_validation passes with all valid inputs."""
manager = ConfigManager()
u = MagicMock()
u.trajectory = [0] * 100
args = MagicMock(
start=10,
end=90,
step=1,
bin_width=30,
temperature=298.0,
force_partitioning=0.5,
)
with patch.dict(
"CodeEntropy.config.arg_config_manager.arg_map",
{"force_partitioning": {"default": 0.5}},
):
manager.input_parameters_validation(u, args)
def test_check_input_start_valid(self):
"""Test that a valid 'start' value does not raise an error."""
args = MagicMock(start=50)
u = MagicMock()
u.trajectory = [0] * 100
ConfigManager()._check_input_start(u, args)
def test_check_input_start_invalid(self):
"""Test that an invalid 'start' value raises a ValueError."""
args = MagicMock(start=150)
u = MagicMock()
u.trajectory = [0] * 100
with self.assertRaises(ValueError):
ConfigManager()._check_input_start(u, args)
def test_check_input_end_valid(self):
"""Test that a valid 'end' value does not raise an error."""
args = MagicMock(end=100)
u = MagicMock()
u.trajectory = [0] * 100
ConfigManager()._check_input_end(u, args)
def test_check_input_end_invalid(self):
"""Test that an 'end' value exceeding trajectory length raises a ValueError."""
args = MagicMock(end=101)
u = MagicMock()
u.trajectory = [0] * 100
with self.assertRaises(ValueError):
ConfigManager()._check_input_end(u, args)
@patch("CodeEntropy.config.arg_config_manager.logger")
def test_check_input_step_negative(self, mock_logger):
"""Test that a negative 'step' value triggers a warning."""
args = MagicMock(step=-1)
ConfigManager()._check_input_step(args)
mock_logger.warning.assert_called_once()
def test_check_input_bin_width_valid(self):
"""Test that a valid 'bin_width' value does not raise an error."""
args = MagicMock(bin_width=180)
ConfigManager()._check_input_bin_width(args)
def test_check_input_bin_width_invalid_low(self):
"""Test that a negative 'bin_width' value raises a ValueError."""
args = MagicMock(bin_width=-10)
with self.assertRaises(ValueError):
ConfigManager()._check_input_bin_width(args)
def test_check_input_bin_width_invalid_high(self):
"""Test that a 'bin_width' value above 360 raises a ValueError."""
args = MagicMock(bin_width=400)
with self.assertRaises(ValueError):
ConfigManager()._check_input_bin_width(args)
def test_check_input_temperature_valid(self):
"""Test that a valid 'temperature' value does not raise an error."""
args = MagicMock(temperature=298.0)
ConfigManager()._check_input_temperature(args)
def test_check_input_temperature_invalid(self):
"""Test that a negative 'temperature' value raises a ValueError."""
args = MagicMock(temperature=-5)
with self.assertRaises(ValueError):
ConfigManager()._check_input_temperature(args)
@patch("CodeEntropy.config.arg_config_manager.logger")
def test_check_input_force_partitioning_warning(self, mock_logger):
"""Test that a non-default 'force_partitioning' value triggers a warning."""
args = MagicMock(force_partitioning=0.7)
with patch.dict(
"CodeEntropy.config.arg_config_manager.arg_map",
{"force_partitioning": {"default": 0.5}},
):
ConfigManager()._check_input_force_partitioning(args)
mock_logger.warning.assert_called_once()
if __name__ == "__main__":
unittest.main()