Exemplo n.º 1
0
class TestSearchQueryPreparation(unittest.TestCase):

    foo = query.TermQuery("foo")
    bar = query.TermQuery("bar")

    def setUp(self):
        # Set the uninitialized global variable in the khard module to make it
        # mockable. See https://stackoverflow.com/questions/61193676
        khard.config = mock.Mock()

    @staticmethod
    def _make_abook(name):
        abook = mock.Mock()
        abook.name = name
        return abook

    @classmethod
    def _run(cls, **kwargs):
        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)
        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())
        self.assertEqual(expected, prepared["foo"])
Exemplo n.º 2
0
 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_by_user_selection(abook, q)
     self.assertEqual(len(l), 1)
     self.assertEqual(l[0].uid, 'testuid1')
Exemplo n.º 3
0
 def test_issue_159_uid_search_doesnt_return_items_twice(self):
     # This was the first half of bug report #159.
     abook = address_book.VdirAddressBook('test', 'test/fixture/test.abook')
     c = abook.search(query.TermQuery('testuid1'))
     self.assertEqual(len(list(c)), 1)
Exemplo n.º 4
0
 def test_search_in_source_files_only_loads_matching_cards(self):
     abook = address_book.VdirAddressBook('test', 'test/fixture/test.abook')
     abook.load(query=query.TermQuery('second'),
                search_in_source_files=True)
     self.assertEqual(len(abook.contacts), 1)
Exemplo n.º 5
0
 def test_copied_from_merge_test_2(self):
     q = query.TermQuery("third")
     l = self._search(q)
     self.assertEqual(len(l), 1)
     self.assertEqual(l[0].uid, 'testuid2')
Exemplo n.º 6
0
 def test_term_query_failing(self):
     q = query.TermQuery("this does not match")
     l = self._search(q)
     self.assertEqual(len(l), 0)
Exemplo n.º 7
0
 def test_term_query_matching(self):
     q = query.TermQuery("second contact")
     l = self._search(q)
     self.assertEqual(len(l), 1)
     self.assertEqual(l[0].uid, 'testuid1')
Exemplo n.º 8
0
 def test_term_query(self):
     q = query.TermQuery("testuid1")
     l = self._search(q)
     self.assertEqual(len(l), 1)
     self.assertEqual(l[0].uid, 'testuid1')
Exemplo n.º 9
0
class TestParseArgs(unittest.TestCase):

    foo = query.TermQuery("foo")
    bar = query.TermQuery("bar")
    baz = query.TermQuery("baz")
    uid = query.FieldQuery("uid", "foo")

    def test_normal_search_terms_create_term_queries(self):
        expected = self.foo
        args, _config = cli.parse_args(['list', 'foo'])
        actual = args.search_terms
        self.assertEqual(expected, actual)

    def test_uid_options_create_uid_queries(self):
        expected = self.uid
        args, _config = cli.parse_args(['list', '--uid=foo'])
        actual = args.search_terms
        self.assertEqual(expected, actual)

    def test_multible_search_terms_generate_and_queries(self):
        expected = query.AndQuery(self.foo, self.bar)
        args, _config = cli.parse_args(['list', 'foo', 'bar'])
        actual = args.search_terms
        self.assertEqual(expected, actual)

    def test_no_search_terms_create_an_any_query(self):
        expected = query.AnyQuery()
        args, _config = cli.parse_args(['list'])
        actual = args.search_terms
        self.assertEqual(expected, actual)

    def test_target_search_terms_are_typed(self):
        args, _config = cli.parse_args(['merge', '--target=foo', 'bar'])
        self.assertEqual(self.foo, args.target_contact)
        self.assertEqual(self.bar, args.source_search_terms)

    def test_second_target_search_term_overrides_first(self):
        args, _config = cli.parse_args(
            ['merge', '--target=foo', '--target=bar', 'baz'])
        self.assertEqual(self.bar, args.target_contact)
        self.assertEqual(self.baz, args.source_search_terms)

    def test_target_uid_option_creates_uid_queries(self):
        args, _config = cli.parse_args(['merge', '--target-uid=foo', 'bar'])
        self.assertEqual(self.uid, args.target_contact)
        self.assertEqual(self.bar, args.source_search_terms)

    def test_uid_option_is_combined_with_search_terms_for_merge_command(self):
        args, _config = cli.parse_args(['merge', '--uid=foo', '--target=bar'])
        self.assertEqual(self.uid, args.source_search_terms)
        self.assertEqual(self.bar, args.target_contact)

    def test_uid_and_free_search_terms_produce_a_conflict(self):
        with self.assertRaises(SystemExit):
            with mock_stream("stderr"):  # just silence stderr
                cli.parse_args(['list', '--uid=foo', 'bar'])

    def test_target_uid_and_free_target_search_terms_produce_a_conflict(self):
        with self.assertRaises(SystemExit):
            with mock_stream("stderr"):  # just silence stderr
                cli.parse_args(['merge', '--target-uid=foo', '--target=bar'])

    def test_no_target_specification_results_in_an_any_query(self):
        expected = query.AnyQuery()
        args, _config = cli.parse_args(['merge'])
        actual = args.target_contact
        self.assertEqual(expected, actual)

    def test_add_email_defaults_to_from_lowercase(self):
        args, _config = cli.parse_args(["add-email"])
        actual = args.fields
        self.assertEqual(["from"], actual)

    def test_add_email_from_field(self):
        args, _config = cli.parse_args(["add-email", "-H", "from"])
        actual = args.fields
        self.assertEqual(["from"], actual)

    def test_add_email_another_field(self):
        args, _config = cli.parse_args(["add-email", "-H", "OtHer"])
        actual = args.fields
        self.assertEqual(["other"], actual)

    def test_add_email_multiple_headers_separate_args_takes_last(self):
        args, _config = cli.parse_args(
            ["add-email", "-H", "OtHer", "-H", "myfield"])
        actual = args.fields
        self.assertEqual(["myfield"], actual)

    def test_add_email_multiple_headers_comma_separated(self):
        args, _config = cli.parse_args(
            ["add-email", "-H", "OtHer,myfield,from"])
        actual = args.fields
        self.assertEqual(["other", "myfield", "from"], actual)