Skip to content

Commit

Permalink
add more unit tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwoer committed Dec 20, 2024
1 parent e257e13 commit 42b4798
Showing 1 changed file with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,138 @@ def test_get_modifications_table():
target = pd.read_csv(StringIO(target_tsv), sep="\t", index_col=0)

pd.testing.assert_frame_equal(table, target)


generic_default_config = """
simple_value_int: 1
simple_value_float: 2.0
simple_value_str: three
nested_values:
nested_value_1: 1
nested_value_2: 2
simple_list:
- 1
- 2
- 3
nested_list:
- name: nested_list_value_1
key1: value11
key2: value21
key3:
- 311
- 312
- 313
- name: nested_list_value_2
key1: value12
key2: value22
key3:
- 312
- 322
- 323
"""

expected_generic_default_config_dict = {
"simple_value_int": 1,
"simple_value_float": 2.0,
"simple_value_str": "three",
"nested_values": {"nested_value_1": 1, "nested_value_2": 2},
"simple_list": [1, 2, 3],
"nested_list": [
{
"name": "nested_list_value_1",
"key1": "value11",
"key2": "value21",
"key3": [311, 312, 313],
},
{
"name": "nested_list_value_2",
"key1": "value12",
"key2": "value22",
"key3": [312, 322, 323],
},
],
}


def test_config_update_empty_list():
"""Test updating a config with an empty list."""
config_1 = Config("default")
config_1.from_dict(yaml.safe_load(StringIO(generic_default_config)))

# when
config_1.update([])

assert config_1.to_dict() == expected_generic_default_config_dict


def test_config_update_simple_two_files():
"""Test updating a config with simple values from two files."""
config_1 = Config("default")
config_1.from_dict(yaml.safe_load(StringIO(generic_default_config)))

config_2 = Config("first")
config_2.from_dict({"simple_value_int": 2, "simple_value_float": 4.0})

config_3 = Config("second")
config_3.from_dict(
{
"simple_value_float": 5.0, # overwrites first
"simple_value_str": "six", # overwrites default
}
)

# when
config_1.update([config_2, config_3], print_modifications=True)

assert config_1.to_dict() == expected_generic_default_config_dict | {
"simple_value_int": 2,
"simple_value_float": 5.0,
"simple_value_str": "six",
}


def test_config_update_advanced():
"""Test updating a config with nested values and lists"""
config_1 = Config("default")
config_1.from_dict(yaml.safe_load(StringIO(generic_default_config)))

config_2 = Config("first")
config_2.from_dict(
{
"nested_values": {"nested_value_2": 42},
"simple_list": [43, 44, 45, 999],
"nested_list": [
{
# "name": ""
"key1": "46",
# "key2": ""
"key3": [47, 48, 49],
},
],
}
)

# when
config_1.update([config_2], print_modifications=True)

assert config_1.to_dict() == expected_generic_default_config_dict | {
"nested_values": {
"nested_value_1": 1, # original value
"nested_value_2": 42,
},
"simple_list": [43, 44, 45], # 4th element is ignored
"nested_list": [
{
"name": "nested_list_value_1", # original value
"key1": "46",
"key2": "value21", # original value
"key3": [47, 48, 49],
},
{ # second list item is not changed
"name": "nested_list_value_2",
"key1": "value12",
"key2": "value22",
"key3": [312, 322, 323],
},
],
}

0 comments on commit 42b4798

Please sign in to comment.