1# -*- coding: utf-8 -*-
2
3# Copyright (C) 2015-2018 Red Hat, Inc.
4#
5# This copyrighted material is made available to anyone wishing to use,
6# modify, copy, or redistribute it subject to the terms and conditions of
7# the GNU General Public License v.2, or (at your option) any later version.
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY expressed or implied, including the implied warranties of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11# Public License for more details.  You should have received a copy of the
12# GNU General Public License along with this program; if not, write to the
13# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
14# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
15# source code or documentation are not subject to the GNU General Public
16# License and may only be used or replicated with the express permission of
17# Red Hat, Inc.
18#
19
20from __future__ import absolute_import
21from __future__ import unicode_literals
22
23import dnf.exceptions
24
25import tests.support
26
27
28class SubjectTest(tests.support.DnfBaseTestCase):
29
30    REPOS = ['main', 'third_party']
31
32    def setUp(self):
33        super(SubjectTest, self).setUp()
34        pkg = self.base.sack.query().filter(name='lotus', arch='x86_64')[0]
35        self.base.sack.add_excludes([pkg])
36
37    def test_best_selectors_no_glob(self):
38        subj = dnf.subject.Subject('pepper')
39        sltrs = subj._get_best_selectors(self.base)
40        self.assertEqual(len(sltrs), 1)
41
42    def test_best_selectors_glob(self):
43        subj = dnf.subject.Subject('l*')
44        sltrs = subj._get_best_selectors(self.base)
45        q = self.base.sack.query().filter(name__glob='l*')
46        self.assertEqual(len(sltrs), len(set(map(lambda p: p.name, q))))
47
48    def test_best_selectors_arch(self):
49        subj = dnf.subject.Subject('l*.x86_64')
50        sltrs = subj._get_best_selectors(self.base)
51        q = self.base.sack.query().filter(name__glob='l*', arch__eq='x86_64')
52        self.assertEqual(len(sltrs), len(set(map(lambda p: p.name, q))))
53        for sltr in sltrs:
54            for pkg in sltr.matches():
55                self.assertEqual(pkg.arch, 'x86_64')
56
57    def test_best_selectors_ver(self):
58        subj = dnf.subject.Subject('*-1-1')
59        sltrs = subj._get_best_selectors(self.base)
60        for sltr in sltrs:
61            for pkg in sltr.matches():
62                self.assertEqual(pkg.evr, '1-1')
63