from fansifter_common.utils.dictutil import compare_seq_of_dicts, exclude, flatten def test_flatten() -> None: actual = flatten( { "a": {"b": {"c": {"d": "e"}}}, "f": 1, "g": [1, 2, 3], "h": {"i": 1, "j": 2}, } ) assert actual == { "a.b.c.d": "e", "f": 1, "g": [1, 2, 3], "h.i": 1, "h.j": 2, } def test_flatten_with_indexes() -> None: actual = flatten( { 0: {"name": "test1"}, 1: {"name": "test2"}, } ) assert actual == { "0.name": "test1", "1.name": "test2", } def test_exclude() -> None: actual = exclude( {"user": "admin", "pass": "secret", "host": "localhost"}, keys=["pass"], ) assert actual == {"user": "admin", "host": "localhost"} def test_compare_seq_of_dicts_empty_lists() -> None: assert compare_seq_of_dicts([], []) def test_compare_seq_of_dicts_one_list_empty() -> None: assert not compare_seq_of_dicts([{"name": "test"}], []) def test_compare_seq_of_dicts_different_size() -> None: assert not compare_seq_of_dicts( [{"name": "test"}], [{"name": "test"}, {"name": "test"}] ) def test_compare_seq_of_dicts() -> None: list1 = [ { "name": "test", "list": ["test1", "test2"], }, { "name": "test2", "list": ["test1", "test2"], }, ] list2 = [ { "name": "test", "list": ["test1", "test2"], }, { "name": "test2", "list": ["test1", "test2"], }, ] assert compare_seq_of_dicts(list1, list2) def test_compare_seq_of_dicts_different_dict_orders_same_content() -> None: list1 = [ { "name": "test", "list": ["test1", "test2"], }, { "name": "test2", "list": ["test1", "test2"], }, ] list2 = [ { "name": "test2", "list": ["test1", "test2"], }, { "name": "test", "list": ["test1", "test2"], }, ] assert compare_seq_of_dicts(list1, list2) def test_compare_seq_of_dicts_same_data_fields_in_different_order() -> None: list1 = [ { "name": "test", "list": ["test1", "test2"], }, { "name": "test2", "list": ["test1", "test2"], }, ] list2 = [ { "name": "test2", "list": ["test1", "test2"], }, { "list": ["test1", "test2"], "name": "test", }, ] assert compare_seq_of_dicts(list1, list2)