From 82b3f0755f5b122e5e4189c5519c89495336f55a Mon Sep 17 00:00:00 2001 From: Neomi Date: Thu, 1 May 2025 11:07:28 -0400 Subject: [PATCH 1/2] HelpersTask579: Added unit tests --- config_root/config/config_utils.py | 1 - config_root/config/test/test_config.py | 187 +++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletion(-) diff --git a/config_root/config/config_utils.py b/config_root/config/config_utils.py index e80753736..22a18b8c8 100644 --- a/config_root/config/config_utils.py +++ b/config_root/config/config_utils.py @@ -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..974c8a3c5 100644 --- a/config_root/config/test/test_config.py +++ b/config_root/config/test/test_config.py @@ -2496,3 +2496,190 @@ def test3(self) -> None: key1 (marked_as_used=False, writer=None, val_type=str): hello.txt """ self.execute_stmt(stmt, exp, mode, globals()) + + +# ############################################################################# +# Test_sort_config_string +# ############################################################################# + +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 handling of empty lines and lines with only spaces. + """ + txt = """ + config3: + value3: test3 + + config2: + value2: test2 + + config1: + value1: test1 + """ + expected = """ + config1: + value1: test1 + config2: + value2: test2 + config3: + value3: test3 + """ + self.check_sort_test(txt, expected) + + def test5(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 test6(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) + + def test7(self) -> None: + """ + Test handling of different key-value formats. + """ + txt = """ + key3 = value3 + key1=value1 + key4:balue4 + key2: value2 + """ + expected = """ + key1=value1 + key2: value2 + key3 = value3 + key4:value4 + """ + self.check_sort_test(txt, expected) + + def test8(self) -> None: + """ + Test handling of comments and various whitespace patterns. + """ + txt = """ + # This is a comment + key1: value1 # Inline comment + + key3: value3 # With trailing spaces + + # Another comment + key2: value2 + """ + expected = """ + # This is a comment + key1: value1 # Inline comment + # Another comment + key2: value2 + key3: value3 # With trailing spaces + """ + self.check_sort_test(txt, expected) \ No newline at end of file From 4e75742638619f64389b6e3b53a2171b9f450469 Mon Sep 17 00:00:00 2001 From: Neomi Date: Thu, 1 May 2025 14:11:07 -0400 Subject: [PATCH 2/2] HelpersTask579: Added unit tests --- config_root/config/config_utils.py | 2 +- config_root/config/test/test_config.py | 72 ++------------------------ 2 files changed, 4 insertions(+), 70 deletions(-) diff --git a/config_root/config/config_utils.py b/config_root/config/config_utils.py index 22a18b8c8..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 diff --git a/config_root/config/test/test_config.py b/config_root/config/test/test_config.py index 974c8a3c5..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. @@ -2499,9 +2496,10 @@ def test3(self) -> None: # ############################################################################# -# Test_sort_config_string +# TestSortConfigString # ############################################################################# + class TestSortConfigString(hunitest.TestCase): """ Test sort_config_string function with various config formats. @@ -2575,30 +2573,6 @@ def test3(self) -> None: self.check_sort_test(txt, expected) def test4(self) -> None: - """ - Test handling of empty lines and lines with only spaces. - """ - txt = """ - config3: - value3: test3 - - config2: - value2: test2 - - config1: - value1: test1 - """ - expected = """ - config1: - value1: test1 - config2: - value2: test2 - config3: - value3: test3 - """ - self.check_sort_test(txt, expected) - - def test5(self) -> None: """ Test sorting of complex nested structure with multiple levels. """ @@ -2628,7 +2602,7 @@ def test5(self) -> None: """ self.check_sort_test(txt, expected) - def test6(self) -> None: + def test5(self) -> None: """ Test sorting with special characters in config names. """ @@ -2643,43 +2617,3 @@ def test6(self) -> None: _special: value """ self.check_sort_test(txt, expected) - - def test7(self) -> None: - """ - Test handling of different key-value formats. - """ - txt = """ - key3 = value3 - key1=value1 - key4:balue4 - key2: value2 - """ - expected = """ - key1=value1 - key2: value2 - key3 = value3 - key4:value4 - """ - self.check_sort_test(txt, expected) - - def test8(self) -> None: - """ - Test handling of comments and various whitespace patterns. - """ - txt = """ - # This is a comment - key1: value1 # Inline comment - - key3: value3 # With trailing spaces - - # Another comment - key2: value2 - """ - expected = """ - # This is a comment - key1: value1 # Inline comment - # Another comment - key2: value2 - key3: value3 # With trailing spaces - """ - self.check_sort_test(txt, expected) \ No newline at end of file