Blame tests/test_match_counter.py

Packit 6f3914
# -*- coding: utf-8 -*-
Packit 6f3914
Packit 6f3914
# Copyright (C) 2012-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
from __future__ import unicode_literals
Packit 6f3914
Packit 6f3914
import dnf.match_counter
Packit 6f3914
Packit 6f3914
import tests.support
Packit 6f3914
from tests.support import mock
Packit 6f3914
Packit 6f3914
Packit 6f3914
class PackageStub(tests.support.MockPackage):
Packit 6f3914
    @classmethod
Packit 6f3914
    def several(cls, count):
Packit 6f3914
        for _ in range(count):
Packit 6f3914
            yield cls()
Packit 6f3914
Packit 6f3914
    def __init__(self, nevra='nevra-1-1.noarch', summary='summary'):
Packit 6f3914
        super(PackageStub, self).__init__(nevra)
Packit 6f3914
        self.summary = summary
Packit 6f3914
        self.url = ''
Packit 6f3914
        self.description = ''
Packit 6f3914
Packit 6f3914
Packit 6f3914
class MatchCounterTest(tests.support.TestCase):
Packit 6f3914
    def test_canonize_string_set(self):
Packit 6f3914
        a = ['f', 'p']
Packit 6f3914
        b = ['p']
Packit 6f3914
        self.assertLess(dnf.match_counter._canonize_string_set(b, 2),
Packit 6f3914
                        dnf.match_counter._canonize_string_set(a, 2))
Packit 6f3914
Packit 6f3914
    def test_matched(self):
Packit 6f3914
        pkg = tests.support.MockPackage("humbert-1-1.noarch")
Packit 6f3914
        pkg.url = url = "http://humbert.com"
Packit 6f3914
        pkg.summary = summary = "Glimpses of an incomparably more poignant bliss."
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        counter.add(pkg, 'summary', 'poignant')
Packit 6f3914
        counter.add(pkg, 'url', 'humbert')
Packit 6f3914
        counter.add(pkg, 'summary', 'humbert')
Packit 6f3914
        self.assertCountEqual(counter.matched_needles(pkg),
Packit 6f3914
                              ['humbert', 'poignant'])
Packit 6f3914
        self.assertCountEqual(counter.matched_keys(pkg), ['url', 'summary'])
Packit 6f3914
        self.assertCountEqual(counter.matched_haystacks(pkg), [url, summary])
Packit 6f3914
Packit 6f3914
    @mock.patch('dnf.match_counter.MatchCounter._eval_distance', return_value=0)
Packit 6f3914
    def test_sorted(self, _):
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        self.assertEqual(counter.sorted(), [])
Packit 6f3914
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        pkg1, pkg2, pkg3 = PackageStub().several(3)
Packit 6f3914
        counter.add(pkg1, 'name', '')
Packit 6f3914
        counter.add(pkg2, 'summary', '')
Packit 6f3914
        self.assertEqual(counter.sorted(), [pkg2, pkg1])
Packit 6f3914
Packit 6f3914
        counter.add(pkg3, 'url', '')
Packit 6f3914
        self.assertEqual(counter.sorted(), [pkg3, pkg2, pkg1])
Packit 6f3914
        self.assertEqual(counter.sorted(reverse=True), [pkg1, pkg2, pkg3])
Packit 6f3914
Packit 6f3914
    @mock.patch('dnf.match_counter.MatchCounter._eval_distance', return_value=0)
Packit 6f3914
    def test_sorted_with_needles(self, _):
Packit 6f3914
        # the same needles should be listed together:
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        pkg1, pkg2, pkg3, pkg4 = PackageStub().several(4)
Packit 6f3914
        counter.add(pkg1, 'summary', 'grin')
Packit 6f3914
        counter.add(pkg2, 'summary', 'foolish')
Packit 6f3914
        counter.add(pkg3, 'summary', 'grin')
Packit 6f3914
        counter.add(pkg4, 'summary', 'grin')
Packit 6f3914
Packit 6f3914
        srt = counter.sorted()
Packit 6f3914
        self.assertIn(srt.index(pkg2), (0, 3))
Packit 6f3914
Packit 6f3914
        # more unique needles is more than less unique needles:
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        counter.add(pkg1, 'summary', 'a')
Packit 6f3914
        counter.add(pkg1, 'summary', 'b')
Packit 6f3914
        counter.add(pkg2, 'summary', 'b')
Packit 6f3914
        counter.add(pkg2, 'summary', 'b')
Packit 6f3914
Packit 6f3914
        self.assertSequenceEqual(counter.sorted(), (pkg2, pkg1))
Packit 6f3914
Packit 6f3914
    @mock.patch('dnf.match_counter.MatchCounter._eval_distance', return_value=0)
Packit 6f3914
    def test_sorted_limit(self, _):
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        pkg1, pkg2, pkg3 = PackageStub().several(3)
Packit 6f3914
        counter.add(pkg1, 'name', '')
Packit 6f3914
        counter.add(pkg2, 'url', '')
Packit 6f3914
        counter.add(pkg3, 'description', '')
Packit 6f3914
        self.assertSequenceEqual(counter.sorted(limit_to=[pkg1, pkg2]),
Packit 6f3914
                                 (pkg2, pkg1))
Packit 6f3914
Packit 6f3914
    @mock.patch('dnf.match_counter.MatchCounter._eval_distance', return_value=0)
Packit 6f3914
    def test_sorted_exact_match(self, _):
Packit 6f3914
        """Exactly matching the name beats name and summary non-exact match."""
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        pkg1 = PackageStub('wednesday-1-1.noarch', 'morning')
Packit 6f3914
        pkg2 = PackageStub('wednesdaymorning-1-1.noarch', "5 o'clock")
Packit 6f3914
        counter.add(pkg1, 'name', 'wednesday')
Packit 6f3914
        counter.add(pkg2, 'name', 'wednesday')
Packit 6f3914
        counter.add(pkg2, 'summary', 'clock')
Packit 6f3914
        self.assertSequenceEqual(counter.sorted(), (pkg2, pkg1))
Packit 6f3914
Packit 6f3914
    def test_total(self):
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        counter.add(3, 'summary', 'humbert')
Packit 6f3914
        counter.add(3, 'url', 'humbert')
Packit 6f3914
        counter.add(20, 'summary', 'humbert')
Packit 6f3914
        self.assertEqual(len(counter), 2)
Packit 6f3914
        self.assertEqual(counter.total(), 3)
Packit 6f3914
Packit 6f3914
    def test_distance(self):
Packit 6f3914
        pkg2 = tests.support.MockPackage('rust-and-stardust-1-2.x86_64')
Packit 6f3914
        pkg1 = tests.support.MockPackage('rust-1-3.x86_64')
Packit 6f3914
        counter = dnf.match_counter.MatchCounter()
Packit 6f3914
        counter.add(pkg1, 'name', 'rust')
Packit 6f3914
        counter.add(pkg2, 'name', 'rust')
Packit 6f3914
        # 'rust-and-stardust' is a worse match for 'rust' than 'rust' itself
Packit 6f3914
        self.assertSequenceEqual([x.name for x in counter.sorted()],
Packit 6f3914
                                 ['rust-and-stardust', 'rust'])