From 03bfeeddd28be26805691ec68cf9f97439bc8d78 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Thu, 19 Dec 2024 23:16:29 +0100 Subject: [PATCH] Add test for name sorting issue #127 --- test/test_khard.py | 164 ++++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 83 deletions(-) diff --git a/test/test_khard.py b/test/test_khard.py index 5895cf6..ca8b377 100644 --- a/test/test_khard.py +++ b/test/test_khard.py @@ -1,18 +1,17 @@ """Unittests for the khard module""" +import unittest from argparse import Namespace from email.headerregistry import Address -import unittest from unittest import mock -from khard import khard, query, config +from khard import config, khard, query from khard.khard import find_email_addresses -from .helpers import TmpAbook, load_contact +from .helpers import TestCarddavObject, TmpAbook, load_contact class TestSearchQueryPreparation(unittest.TestCase): - foo = query.TermQuery("foo") bar = query.TermQuery("bar") @@ -29,28 +28,34 @@ def _make_abook(name): @classmethod def _run(cls, **kwargs): - with mock.patch("khard.khard.config.abooks", - [cls._make_abook(name) - for name in ["foo", "bar", "baz"]]): + with mock.patch( + "khard.khard.config.abooks", + [cls._make_abook(name) for name in ["foo", "bar", "baz"]], + ): return khard.prepare_search_queries(Namespace(**kwargs)) def test_queries_for_the_same_address_book_are_joind_by_disjunction(self): expected = self.foo | self.bar - prepared = self._run(addressbook=["foo"], target_addressbook=["foo"], - source_search_terms=self.foo, - target_contact=self.bar) + prepared = self._run( + addressbook=["foo"], + target_addressbook=["foo"], + source_search_terms=self.foo, + target_contact=self.bar, + ) self.assertEqual(expected, prepared["foo"]) def test_no_search_terms_result_in_any_queries(self): expected = query.AnyQuery() - prepared = self._run(addressbook=["foo"], target_addressbook=["foo"], - source_search_terms=query.AnyQuery(), - target_contact=query.AnyQuery()) + prepared = self._run( + addressbook=["foo"], + target_addressbook=["foo"], + source_search_terms=query.AnyQuery(), + target_contact=query.AnyQuery(), + ) self.assertEqual(expected, prepared["foo"]) class TestFindEmailAddress(unittest.TestCase): - def test_empty_text_finds_none(self): text = "" addrs = find_email_addresses(text, ["from"]) @@ -59,8 +64,9 @@ def test_empty_text_finds_none(self): def test_single_header_finds_one_address(self): text = """From: John Doe """ addrs = find_email_addresses(text, ["from"]) - expected = [Address(display_name="John Doe", - username="jdoe", domain="machine.example")] + expected = [ + Address(display_name="John Doe", username="jdoe", domain="machine.example") + ] self.assertEqual(expected, addrs) def test_single_header_finds_multiple_addresses(self): @@ -68,102 +74,88 @@ def test_single_header_finds_multiple_addresses(self): Mary Smith """ addrs = find_email_addresses(text, ["from"]) expected = [ - Address( - display_name="John Doe", - username="jdoe", - domain="machine.example"), - Address( - display_name="Mary Smith", - username="mary", - domain="example.net")] + Address(display_name="John Doe", username="jdoe", domain="machine.example"), + Address(display_name="Mary Smith", username="mary", domain="example.net"), + ] self.assertEqual(expected, addrs) def test_non_address_header_finds_none(self): - text = "From: John Doe , " \ + text = ( + "From: John Doe , " "Mary Smith \nOther: test" + ) addrs = find_email_addresses(text, ["other"]) expected = [] self.assertEqual(expected, addrs) def test_multiple_headers_finds_some(self): - text = "From: John Doe , " \ + text = ( + "From: John Doe , " "Mary Smith \nOther: test" + ) addrs = find_email_addresses(text, ["other", "from"]) expected = [ - Address( - display_name="John Doe", - username="jdoe", - domain="machine.example"), - Address( - display_name="Mary Smith", - username="mary", - domain="example.net")] + Address(display_name="John Doe", username="jdoe", domain="machine.example"), + Address(display_name="Mary Smith", username="mary", domain="example.net"), + ] self.assertEqual(expected, addrs) def test_multiple_headers_finds_all(self): - text = "From: John Doe , " \ - "Mary Smith \n" \ + text = ( + "From: John Doe , " + "Mary Smith \n" "To: Michael Jones " + ) addrs = find_email_addresses(text, ["to", "FrOm"]) expected = [ Address( display_name="Michael Jones", username="mjones", - domain="machine.example"), - Address( - display_name="John Doe", - username="jdoe", - domain="machine.example"), - Address( - display_name="Mary Smith", - username="mary", - domain="example.net")] + domain="machine.example", + ), + Address(display_name="John Doe", username="jdoe", domain="machine.example"), + Address(display_name="Mary Smith", username="mary", domain="example.net"), + ] self.assertEqual(expected, addrs) def test_finds_all_emails(self): - text = "From: John Doe , " \ - "Mary Smith \n" \ + text = ( + "From: John Doe , " + "Mary Smith \n" "To: Michael Jones " + ) addrs = find_email_addresses(text, ["all"]) expected = [ - Address( - display_name="John Doe", - username="jdoe", - domain="machine.example"), - Address( - display_name="Mary Smith", - username="mary", - domain="example.net"), + Address(display_name="John Doe", username="jdoe", domain="machine.example"), + Address(display_name="Mary Smith", username="mary", domain="example.net"), Address( display_name="Michael Jones", username="mjones", - domain="machine.example")] + domain="machine.example", + ), + ] self.assertEqual(expected, addrs) - def test_finds_all_emails_with_other_headers_too( - self): - text = "From: John Doe , " \ - "Mary Smith \n" \ + def test_finds_all_emails_with_other_headers_too(self): + text = ( + "From: John Doe , " + "Mary Smith \n" "To: Michael Jones " + ) addrs = find_email_addresses(text, ["other", "all", "from"]) expected = [ - Address( - display_name="John Doe", - username="jdoe", - domain="machine.example"), - Address( - display_name="Mary Smith", - username="mary", - domain="example.net"), + Address(display_name="John Doe", username="jdoe", domain="machine.example"), + Address(display_name="Mary Smith", username="mary", domain="example.net"), Address( display_name="Michael Jones", username="mjones", - domain="machine.example")] + domain="machine.example", + ), + ] self.assertEqual(expected, addrs) class TestGetContactListByUserSelection(unittest.TestCase): - def setUp(self): """initialize the global config object with a mock""" khard.config = mock.Mock(spec=config.Config) @@ -179,7 +171,7 @@ def test_uid_query_without_strict_search(self): with TmpAbook(["contact1.vcf", "contact2.vcf"]) as abook: l = khard.get_contact_list(abook, q) self.assertEqual(len(l), 1) - self.assertEqual(l[0].uid, 'testuid1') + self.assertEqual(l[0].uid, "testuid1") def test_name_query_with_uid_text_and_strict_search(self): q = query.NameQuery("testuid1") @@ -198,18 +190,17 @@ def test_term_query_without_strict_search(self): with TmpAbook(["contact1.vcf", "contact2.vcf"]) as abook: l = khard.get_contact_list(abook, q) self.assertEqual(len(l), 1) - self.assertEqual(l[0].uid, 'testuid1') + self.assertEqual(l[0].uid, "testuid1") def test_term_query_with_strict_search_matching(self): q = query.TermQuery("second contact") with TmpAbook(["contact1.vcf", "contact2.vcf"]) as abook: l = khard.get_contact_list(abook, q) self.assertEqual(len(l), 1) - self.assertEqual(l[0].uid, 'testuid1') + self.assertEqual(l[0].uid, "testuid1") class TestSortContacts(unittest.TestCase): - contact1 = load_contact("contact1.vcf") contact2 = load_contact("contact2.vcf") nickname = load_contact("nickname.vcf") @@ -219,7 +210,7 @@ def _test(self, first, second, **kwargs): """Run the sort_contacts function and assert the result The two contacts first and second are expected to come out in that - order and are deliberatly put into the function in the reverse order. + order and are deliberately put into the function in the reverse order. """ actual = khard.sort_contacts([second, first], **kwargs) self.assertListEqual(actual, [first, second]) @@ -238,15 +229,22 @@ def test_can_sort_by_formatted_name(self): def test_group_by_addressbook(self): with TmpAbook(["contact1.vcf", "category.vcf"], name="one") as abook1: - with TmpAbook(["contact2.vcf", "labels.vcf"], - name="two") as abook2: - contact1 = next(abook1.search(query.FieldQuery("uid", - "testuid1"))) + with TmpAbook(["contact2.vcf", "labels.vcf"], name="two") as abook2: + contact1 = next(abook1.search(query.FieldQuery("uid", "testuid1"))) category = next(abook1.search(query.NameQuery("category"))) - contact2 = next(abook2.search(query.FieldQuery("uid", - "testuid2"))) + contact2 = next(abook2.search(query.FieldQuery("uid", "testuid2"))) labels = next(abook2.search(query.NameQuery("labeled guy"))) expected = [category, contact1, labels, contact2] - actual = khard.sort_contacts([contact1, contact2, category, labels], - group=True) + actual = khard.sort_contacts([contact1, contact2, category, labels], group=True) self.assertListEqual(actual, expected) + + def test_sort_order_for_accentuated_names(self): + # reported in issue #127 + albert = TestCarddavObject(fn="Albert") + eleanor = TestCarddavObject(fn="Eleanor") + eugene = TestCarddavObject(fn="Eugene") + zakari = TestCarddavObject(fn="Zakari") + eric = TestCarddavObject(fn="Éric") + unsorted = [albert, eleanor, eugene, zakari, eric] + sorted = khard.sort_contacts(unsorted, sort="formatted_name") + self.assertEqual(sorted, [albert, eleanor, eric, eugene, zakari])