Blame tests/test_match_counter.py

Packit Service 21c75c
# -*- coding: utf-8 -*-
Packit Service 21c75c
Packit Service 21c75c
# Copyright (C) 2012-2018 Red Hat, Inc.
Packit Service 21c75c
#
Packit Service 21c75c
# This copyrighted material is made available to anyone wishing to use,
Packit Service 21c75c
# modify, copy, or redistribute it subject to the terms and conditions of
Packit Service 21c75c
# the GNU General Public License v.2, or (at your option) any later version.
Packit Service 21c75c
# This program is distributed in the hope that it will be useful, but WITHOUT
Packit Service 21c75c
# ANY WARRANTY expressed or implied, including the implied warranties of
Packit Service 21c75c
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Packit Service 21c75c
# Public License for more details.  You should have received a copy of the
Packit Service 21c75c
# GNU General Public License along with this program; if not, write to the
Packit Service 21c75c
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 21c75c
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
Packit Service 21c75c
# source code or documentation are not subject to the GNU General Public
Packit Service 21c75c
# License and may only be used or replicated with the express permission of
Packit Service 21c75c
# Red Hat, Inc.
Packit Service 21c75c
#
Packit Service 21c75c
Packit Service 21c75c
from __future__ import absolute_import
Packit Service 21c75c
from __future__ import unicode_literals
Packit Service 21c75c
Packit Service 21c75c
import dnf.match_counter
Packit Service 21c75c
Packit Service 21c75c
import tests.support
Packit Service 21c75c
from tests.support import mock
Packit Service 21c75c
Packit Service 21c75c
Packit Service 21c75c
class PackageStub(tests.support.MockPackage):
Packit Service 21c75c
    @classmethod
Packit Service 21c75c
    def several(cls, count):
Packit Service 21c75c
        for _ in range(count):
Packit Service 21c75c
            yield cls()
Packit Service 21c75c
Packit Service 21c75c
    def __init__(self, nevra='nevra-1-1.noarch', summary='summary'):
Packit Service 21c75c
        super(PackageStub, self).__init__(nevra)
Packit Service 21c75c
        self.summary = summary
Packit Service 21c75c
        self.url = ''
Packit Service 21c75c
        self.description = ''
Packit Service 21c75c
Packit Service 21c75c
Packit Service 21c75c
class MatchCounterTest(tests.support.TestCase):
Packit Service 21c75c
    def test_canonize_string_set(self):
Packit Service 21c75c
        a = ['f', 'p']
Packit Service 21c75c
        b = ['p']
Packit Service 21c75c
        self.assertLess(dnf.match_counter._canonize_string_set(b, 2),
Packit Service 21c75c
                        dnf.match_counter._canonize_string_set(a, 2))
Packit Service 21c75c
Packit Service 21c75c
    def test_matched(self):
Packit Service 21c75c
        pkg = tests.support.MockPackage("humbert-1-1.noarch")
Packit Service 21c75c
        pkg.url = url = "http://humbert.com"
Packit Service 21c75c
        pkg.summary = summary = "Glimpses of an incomparably more poignant bliss."
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        counter.add(pkg, 'summary', 'poignant')
Packit Service 21c75c
        counter.add(pkg, 'url', 'humbert')
Packit Service 21c75c
        counter.add(pkg, 'summary', 'humbert')
Packit Service 21c75c
        self.assertCountEqual(counter.matched_needles(pkg),
Packit Service 21c75c
                              ['humbert', 'poignant'])
Packit Service 21c75c
        self.assertCountEqual(counter.matched_keys(pkg), ['url', 'summary'])
Packit Service 21c75c
        self.assertCountEqual(counter.matched_haystacks(pkg), [url, summary])
Packit Service 21c75c
Packit Service 21c75c
    def test_sorted(self):
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        self.assertEqual(counter.sorted(), [])
Packit Service 21c75c
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        pkg1, pkg2, pkg3 = PackageStub().several(3)
Packit Service 21c75c
        counter.add(pkg1, 'name', '')
Packit Service 21c75c
        counter.add(pkg2, 'summary', '')
Packit Service 21c75c
        self.assertEqual(counter.sorted(), [pkg1, pkg2])
Packit Service 21c75c
Packit Service 21c75c
        counter.add(pkg3, 'url', '')
Packit Service 21c75c
        self.assertEqual(counter.sorted(), [pkg1, pkg2, pkg3])
Packit Service 21c75c
        self.assertEqual(counter.sorted(reverse=True), [pkg1, pkg2, pkg3])
Packit Service 21c75c
Packit Service 21c75c
    def test_sorted_with_needles(self):
Packit Service 21c75c
        # the same needles should be listed together:
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        pkg1, pkg2, pkg3, pkg4 = PackageStub().several(4)
Packit Service 21c75c
        counter.add(pkg1, 'summary', 'grin')
Packit Service 21c75c
        counter.add(pkg2, 'summary', 'foolish')
Packit Service 21c75c
        counter.add(pkg3, 'summary', 'grin')
Packit Service 21c75c
        counter.add(pkg4, 'summary', 'grin')
Packit Service 21c75c
Packit Service 21c75c
        srt = counter.sorted()
Packit Service 21c75c
        self.assertEqual(srt.index(pkg2), 1)
Packit Service 21c75c
Packit Service 21c75c
        # more unique needles is more than less unique needles:
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        counter.add(pkg1, 'summary', 'a')
Packit Service 21c75c
        counter.add(pkg1, 'summary', 'b')
Packit Service 21c75c
        counter.add(pkg2, 'summary', 'b')
Packit Service 21c75c
        counter.add(pkg2, 'summary', 'b')
Packit Service 21c75c
Packit Service 21c75c
        self.assertSequenceEqual(counter.sorted(), (pkg1, pkg2))
Packit Service 21c75c
Packit Service 21c75c
    def test_sorted_limit(self):
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        pkg1, pkg2, pkg3 = PackageStub().several(3)
Packit Service 21c75c
        counter.add(pkg1, 'name', '')
Packit Service 21c75c
        counter.add(pkg2, 'url', '')
Packit Service 21c75c
        counter.add(pkg3, 'description', '')
Packit Service 21c75c
        self.assertSequenceEqual(counter.sorted(limit_to=[pkg1, pkg2]),
Packit Service 21c75c
                                 (pkg1, pkg2))
Packit Service 21c75c
Packit Service 21c75c
    def test_sorted_exact_match(self):
Packit Service 21c75c
        """Exactly matching the name beats name and summary non-exact match."""
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        pkg1 = PackageStub('wednesday-1-1.noarch', 'morning')
Packit Service 21c75c
        pkg2 = PackageStub('wednesdaymorning-1-1.noarch', "5 o'clock")
Packit Service 21c75c
        counter.add(pkg1, 'name', 'wednesday')
Packit Service 21c75c
        counter.add(pkg2, 'name', 'wednesday')
Packit Service 21c75c
        counter.add(pkg2, 'summary', 'clock')
Packit Service 21c75c
        self.assertSequenceEqual(counter.sorted(), (pkg1, pkg2))
Packit Service 21c75c
Packit Service 21c75c
    def test_total(self):
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        counter.add(3, 'summary', 'humbert')
Packit Service 21c75c
        counter.add(3, 'url', 'humbert')
Packit Service 21c75c
        counter.add(20, 'summary', 'humbert')
Packit Service 21c75c
        self.assertEqual(len(counter), 2)
Packit Service 21c75c
        self.assertEqual(counter.total(), 3)
Packit Service 21c75c
Packit Service 21c75c
    def test_distance(self):
Packit Service 21c75c
        pkg2 = tests.support.MockPackage('rust-and-stardust-1-2.x86_64')
Packit Service 21c75c
        pkg1 = tests.support.MockPackage('rust-1-3.x86_64')
Packit Service 21c75c
        counter = dnf.match_counter.MatchCounter()
Packit Service 21c75c
        counter.add(pkg1, 'name', 'rust')
Packit Service 21c75c
        counter.add(pkg2, 'name', 'rust')
Packit Service 21c75c
        # 'rust-and-stardust' is a worse match for 'rust' than 'rust' itself
Packit Service 21c75c
        self.assertSequenceEqual([x.name for x in counter.sorted()],
Packit Service 21c75c
                                 ['rust', 'rust-and-stardust'])