Blame tests/cli/commands/test_search.py

Packit 6f3914
# -*- coding: utf-8 -*-
Packit 6f3914
Packit 6f3914
# Copyright (C) 2014-2018 Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
# This copyrighted material is made available to anyone wishing to use,
Packit 6f3914
# modify, copy, or redistribute it subject to the terms and conditions of
Packit 6f3914
# the GNU General Public License v.2, or (at your option) any later version.
Packit 6f3914
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit 6f3914
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit 6f3914
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit 6f3914
# Public License for more details.  You should have received a copy of the
Packit 6f3914
# GNU General Public License along with this program; if not, write to the
Packit 6f3914
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 6f3914
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit 6f3914
# source code or documentation are not subject to the GNU General Public
Packit 6f3914
# License and may only be used or replicated with the express permission of
Packit 6f3914
# Red Hat, Inc.
Packit 6f3914
#
Packit 6f3914
Packit 6f3914
from __future__ import absolute_import
Packit 6f3914
Packit 6f3914
import dnf.cli.commands.search as search
Packit 6f3914
import dnf.match_counter
Packit 6f3914
import dnf.pycomp
Packit 6f3914
Packit 6f3914
import tests.support
Packit 6f3914
from tests import mock
Packit 6f3914
Packit 6f3914
Packit 6f3914
class SearchCountedTest(tests.support.DnfBaseTestCase):
Packit 6f3914
Packit 6f3914
    REPOS = ["main"]
Packit 6f3914
    CLI = "mock"
Packit 6f3914
Packit 6f3914
    def setUp(self):
Packit 6f3914
        super(SearchCountedTest, self).setUp()
Packit 6f3914
        self.cmd = search.SearchCommand(self.cli)
Packit 6f3914
Packit 6f3914
    def test_search_counted(self):
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        self.cmd._search_counted(counter, 'summary', 'ation')
Packit 6f3914
        self.assertEqual(len(counter), 2)
Packit 6f3914
        haystacks = set()
Packit 6f3914
        for pkg in counter:
Packit 6f3914
            haystacks.update(counter.matched_haystacks(pkg))
Packit 6f3914
        self.assertCountEqual(haystacks, ["It's an invitation.",
Packit 6f3914
                                          "Make a reservation."])
Packit 6f3914
Packit 6f3914
    def test_search_counted_glob(self):
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        self.cmd._search_counted(counter, 'summary', '*invit*')
Packit 6f3914
        self.assertEqual(len(counter), 1)
Packit 6f3914
Packit 6f3914
Packit 6f3914
class SearchTest(tests.support.DnfBaseTestCase):
Packit 6f3914
Packit 6f3914
    REPOS = ["search"]
Packit 6f3914
    CLI = "mock"
Packit 6f3914
Packit 6f3914
    def setUp(self):
Packit 6f3914
        super(SearchTest, self).setUp()
Packit 6f3914
        self.base.output = mock.MagicMock()
Packit 6f3914
        self.base.output.fmtSection = lambda str: str
Packit 6f3914
        self.cmd = search.SearchCommand(self.cli)
Packit 6f3914
Packit 6f3914
    def patched_search(self, *args):
Packit 6f3914
        with tests.support.patch_std_streams() as (stdout, _):
Packit 6f3914
            tests.support.command_run(self.cmd, *args)
Packit 6f3914
            call_args = self.base.output.matchcallback.call_args_list
Packit 6f3914
            pkgs = [c[0][0] for c in call_args]
Packit 6f3914
            return (stdout.getvalue(), pkgs)
Packit 6f3914
Packit 6f3914
    def test_search(self):
Packit 6f3914
        (_, pkgs) = self.patched_search(['lotus'])
Packit 6f3914
        pkg_names = list(map(str, pkgs))
Packit 6f3914
        self.assertIn('lotus-3-16.i686', pkg_names)
Packit 6f3914
        self.assertIn('lotus-3-16.x86_64', pkg_names)
Packit 6f3914
Packit 6f3914
    @mock.patch('dnf.cli.commands.search._',
Packit 6f3914
                dnf.pycomp.NullTranslations().ugettext)
Packit 6f3914
    def test_search_caseness(self):
Packit 6f3914
        (stdout, pkgs) = self.patched_search(['LOTUS'])
Packit 6f3914
        self.assertEqual(stdout, 'Name Matched: LOTUS\n')
Packit 6f3914
        pkg_names = map(str, pkgs)
Packit 6f3914
        self.assertIn('lotus-3-16.i686', pkg_names)
Packit 6f3914
        self.assertIn('lotus-3-16.x86_64', pkg_names)