diff --git a/khard/carddav_object.py b/khard/carddav_object.py index 67cb979..77f9ddb 100644 --- a/khard/carddav_object.py +++ b/khard/carddav_object.py @@ -39,6 +39,8 @@ def multi_property_key(item: str) -> Tuple[Literal[0], str]: ... @overload def multi_property_key(item: Dict[T, Any]) -> Tuple[Literal[1], T]: ... +@overload +def multi_property_key(item: Union[str, Dict[T, Any]]) -> Tuple[Union[Literal[0], Literal[1]], Union[str, T]]: ... def multi_property_key(item: Union[str, Dict[T, Any]] ) -> Tuple[int, Union[T, str]]: """Key function to pass to sorted(), allowing sorting of dicts with lists diff --git a/test/test_carddav_object.py b/test/test_carddav_object.py index 8833272..3e06e8e 100644 --- a/test/test_carddav_object.py +++ b/test/test_carddav_object.py @@ -6,7 +6,7 @@ import unittest from unittest import mock -from khard.carddav_object import CarddavObject +from khard.carddav_object import CarddavObject, multi_property_key class CarddavObjectFormatDateObject(unittest.TestCase): @@ -57,3 +57,28 @@ def test_parsing_base64_ecoded_photo_vcard_v4(self): self.assertEqual(uri_stuff, 'data:image/png;base64') data = base64.decodebytes(data.encode()) self.assertEqual(data[:8], self.PNG_HEADER) + + +class MultiPropertyKey(unittest.TestCase): + """Test for the multi_property_key helper function""" + + def test_strings_are_in_the_first_sort_group(self) -> None: + group, _key = multi_property_key("some string") + self.assertEqual(group, 0) + + def test_dicts_are_in_the_second_sort_group(self) -> None: + group, _key = multi_property_key({"some": "dict"}) + self.assertEqual(group, 1) + + def test_strings_are_their_own_keys(self) -> None: + _group, key = multi_property_key("some string") + self.assertEqual(key, "some string") + + def test_dicts_are_keyed_by_the_first_key(self) -> None: + _group, key = multi_property_key({"some": "dict", "more": "stuff"}) + self.assertEqual(key, "some") + + def test_all_strings_are_sorted_before_dicts(self) -> None: + my_list = ["a", {"c": "d"}, "e", {"f": "g"}] + my_list.sort(key=multi_property_key) + self.assertEqual(my_list, ["a", "e", {"c": "d"}, {"f": "g"}]) diff --git a/test/test_yaml_editable.py b/test/test_yaml_editable.py index 39fc56e..4238aba 100644 --- a/test/test_yaml_editable.py +++ b/test/test_yaml_editable.py @@ -95,3 +95,21 @@ def test_private_objects_can_have_an_ablabel(self) -> None: label.group = "1" result = ye._get_private_objects() self.assertEqual(result, {"foo": [{"baz": "bar"}]}) + + def test_private_objects_with_ablabels_are_sorted_by_ablabel(self) -> None: + ye = TestYAMLEditable() + ye.supported_private_objects = ["foo"] + foo1 = ye.vcard.add("X-FOO") + foo1.value = "AAA" + foo1.group = "1" + label1 = ye.vcard.add("X-ABLABEL") + label1.value = "yyy" + label1.group = "1" + foo2 = ye.vcard.add("X-FOO") + foo2.value = "BBB" + foo2.group = "2" + label2 = ye.vcard.add("X-ABLABEL") + label2.value = "xxx" + label2.group = "2" + result = ye._get_private_objects() + self.assertEqual(result, {"foo": [{"xxx": "BBB"}, {"yyy": "AAA"}]})