diff --git a/config_root/config/config_utils.py b/config_root/config/config_utils.py index e80753736..5594e70ec 100644 --- a/config_root/config/config_utils.py +++ b/config_root/config/config_utils.py @@ -12,9 +12,9 @@ import re from typing import Any, Iterable, List, Optional -import config_root.config.config_ as crococon import pandas as pd +import config_root.config.config_ as crococon import helpers.hdbg as hdbg import helpers.hdict as hdict import helpers.hdocker as hdocker @@ -86,7 +86,6 @@ def replace_shared_dir_paths( return new_config -# TODO(gp): Add unit tests. def sort_config_string(txt: str) -> str: """ Sort a string representing a Config in alphabetical order by the first diff --git a/config_root/config/test/test_config.py b/config_root/config/test/test_config.py index 88b093fa6..8a080813e 100644 --- a/config_root/config/test/test_config.py +++ b/config_root/config/test/test_config.py @@ -1864,7 +1864,6 @@ def test1(self) -> None: key2: key3: key4: - """ should_be_pickleable_before = True force_values_to_string = True @@ -1886,7 +1885,6 @@ def test2(self) -> None: key2: key3: key4: - """ should_be_pickleable_before = False force_values_to_string = True @@ -2085,7 +2083,6 @@ def test5(self) -> None: def test6(self) -> None: """ Test debug mode with `marked_as_used` == True. - This is a smoketest since the output of stacktrace is unstable. """ # Set multiline string value. @@ -2496,3 +2493,127 @@ def test3(self) -> None: key1 (marked_as_used=False, writer=None, val_type=str): hello.txt """ self.execute_stmt(stmt, exp, mode, globals()) + + +# ############################################################################# +# TestSortConfigString +# ############################################################################# + + +class TestSortConfigString(hunitest.TestCase): + """ + Test sort_config_string function with various config formats. + """ + + def check_sort_test(self, txt: str, expected: str) -> None: + """ + Helper method to test sorted config output. + """ + result = cconfig.sort_config_string(txt) + self.assertEqual(result, expected, fuzzy_match=True) + + def test1(self) -> None: + """ + Test sorting of single-line config entries. + """ + txt = """ + z_config: value3 + a_config: value1 + m_config: value2 + """ + expected = """ + a_config: value1 + m_config: value2 + z_config: value3 + """ + self.check_sort_test(txt, expected) + + def test2(self) -> None: + """ + Test sorting of multi-level config entries. + """ + txt = """ + build_model: + activation: sigmoid + layers: 3 + build_data: + source: database + type: timeseries + """ + expected = """ + build_data: + source: database + type: timeseries + build_model: + activation: sigmoid + layers: 3 + """ + self.check_sort_test(txt, expected) + + def test3(self) -> None: + """ + Test handling of different indentation patterns and space combinations. + """ + txt = """ + config1: + normal_indent: value1 + wrong_indent: value2 + config2: + extra_indent: value3 + single: value4 + bad_indent: value5 + """ + expected = """ + config1: + normal_indent: value1 + config2: + extra_indent: value3 + single value4 + """ + self.check_sort_test(txt, expected) + + def test4(self) -> None: + """ + Test sorting of complex nested structure with multiple levels. + """ + txt = """ + z_parent: + child2: + key1: val1 + key2: val2 + child1: value + a_parent: + nested: + deep: + key: value + simple: test + """ + expected = """ + a_parent: + nested: + deep: + key: value + simple: test + z_parent: + child1: value + child2: + key1: val1 + key2: val2 + """ + self.check_sort_test(txt, expected) + + def test5(self) -> None: + """ + Test sorting with special characters in config names. + """ + txt = """ + _special: value + #comment: note + @config: test + """ + expected = """ + #comment: note + @config: test + _special: value + """ + self.check_sort_test(txt, expected)