Skip to content

Commit b5816b6

Browse files
Eduard Čubaj-mracek
authored andcommitted
[tests]: reintroduce dropped unit tests
1 parent 6c82346 commit b5816b6

File tree

6 files changed

+318
-5
lines changed

6 files changed

+318
-5
lines changed

tests/README

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ What will not be tested:
66
* rpm transactions themselves (tested in RPM)
77
* depsolving (the core depsolving algorithms are tested in libsolv, correctly
88
setting up libsolv is hawkey's job)
9-
* Swdb functions - tested in libdnf
10-
11-
Missing
12-
* SwdbInterface tests
9+
* SWDB functions (tested in libdnf)
1310

1411
== The repos/ directory ==
1512

tests/support.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from __future__ import absolute_import
1919
from __future__ import unicode_literals
2020
from functools import reduce
21+
from dnf.db.types import SwdbReason, SwdbPkgData
2122
import contextlib
2223
import dnf
2324
import dnf.conf
@@ -151,6 +152,17 @@ def command_run(cmd, args):
151152
command_configure(cmd, args)
152153
return cmd.run()
153154

155+
156+
def mockSwdbPkg(history, pkg, state="Installed", repo="unknown", reason=SwdbReason.USER):
157+
""" Add DnfPackage into database """
158+
hpkg = history.ipkg_to_pkg(pkg)
159+
pid = history.add_package(hpkg)
160+
pkg_data = SwdbPkgData()
161+
history.swdb.trans_data_beg(0, pid, reason, state, False)
162+
history.update_package_data(pid, 0, pkg_data)
163+
history.set_repo(hpkg, repo)
164+
165+
154166
class Base(dnf.Base):
155167
def __init__(self, *args, **kwargs):
156168
with mock.patch('dnf.rpm.detect_releasever', return_value=69):

tests/test_commands.py

Lines changed: 265 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from __future__ import absolute_import
1919
from __future__ import unicode_literals
2020
from tests import support
21-
from tests.support import mock
21+
from tests.support import mock, mockSwdbPkg
2222

2323
import dnf.cli.commands
2424
import dnf.cli.commands.group
@@ -192,6 +192,26 @@ def test_run_notinstalled(self):
192192
self.assertResult(self._cmd.cli.base,
193193
self._cmd.cli.base.sack.query().installed())
194194

195+
@mock.patch('dnf.cli.commands.reinstall._', dnf.pycomp.NullTranslations().ugettext)
196+
def test_run_notavailable(self):
197+
""" Test whether it fails if the package is not available. """
198+
base = self._cmd.cli.base
199+
holes_query = dnf.subject.Subject('hole').get_best_query(base.sack)
200+
history = self._cmd.base.history
201+
for pkg in holes_query.installed():
202+
mockSwdbPkg(history, pkg)
203+
204+
stdout = dnf.pycomp.StringIO()
205+
206+
with support.wiretap_logs('dnf', logging.INFO, stdout):
207+
self.assertRaises(dnf.exceptions.Error, support.command_run, self._cmd, ['hole'])
208+
209+
self.assertEqual(
210+
stdout.getvalue(),
211+
'Installed package hole-1-1.x86_64 (from unknown) not available.\n')
212+
self.assertResult(base, base.sack.query().installed())
213+
214+
195215
class RepoPkgsCommandTest(unittest.TestCase):
196216

197217
"""Tests of ``dnf.cli.commands.RepoPkgsCommand`` class."""
@@ -214,6 +234,34 @@ class RepoPkgsCheckUpdateSubCommandTest(unittest.TestCase):
214234

215235
"""Tests of ``dnf.cli.commands.RepoPkgsCommand.CheckUpdateSubCommand`` class."""
216236

237+
def test(self):
238+
""" Test whether only upgrades in the repository are listed. """
239+
history = self.cli.base.history
240+
for pkg in self.cli.base.sack.query().installed().filter(name='tour'):
241+
mockSwdbPkg(history, pkg, repo='updates')
242+
243+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
244+
with support.patch_std_streams() as (stdout, _):
245+
support.command_run(cmd, ['updates', 'check-update'])
246+
247+
self.assertEqual(
248+
stdout.getvalue(),
249+
u'\n'
250+
u'hole.x86_64 2-1'
251+
u' updates \n'
252+
u'pepper.x86_64 20-1'
253+
u' updates \n'
254+
u'Obsoleting Packages\n'
255+
u'hole.i686 2-1'
256+
u' updates \n'
257+
u' tour.noarch 5-0'
258+
u' @updates\n'
259+
u'hole.x86_64 2-1'
260+
u' updates \n'
261+
u' tour.noarch 5-0'
262+
u' @updates\n')
263+
self.assertEqual(self.cli.demands.success_exit_status, 100)
264+
217265
def setUp(self):
218266
"""Prepare the test fixture."""
219267
super(RepoPkgsCheckUpdateSubCommandTest, self).setUp()
@@ -287,6 +335,45 @@ def setUp(self):
287335
base.conf.recent = 7
288336
self.cli = base.mock_cli()
289337

338+
def test_info_all(self):
339+
"""Test whether only packages related to the repository are listed."""
340+
history = self.cli.base.history
341+
for pkg in self.cli.base.sack.query().installed().filter(name='pepper'):
342+
mockSwdbPkg(history, pkg, repo='main')
343+
344+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
345+
with support.patch_std_streams() as (stdout, _):
346+
support.command_run(cmd, ['main', 'info', 'all', '*p*'])
347+
348+
self.assertEqual(
349+
stdout.getvalue(),
350+
''.join((
351+
self.INSTALLED_TITLE,
352+
self.PEPPER_SYSTEM_INFO,
353+
self.AVAILABLE_TITLE,
354+
u'Name : pepper\n'
355+
u'Version : 20\n'
356+
u'Release : 0\n'
357+
u'Arch : src\n'
358+
u'Size : 0.0 \n'
359+
u'Source : None\n'
360+
u'Repo : main\n'
361+
u'Summary : \n'
362+
u'License : \n'
363+
u'Description : \n'
364+
u'\n',
365+
u'Name : trampoline\n'
366+
u'Version : 2.1\n'
367+
u'Release : 1\n'
368+
u'Arch : noarch\n'
369+
u'Size : 0.0 \n'
370+
u'Source : None\n'
371+
u'Repo : main\n'
372+
u'Summary : \n'
373+
u'License : \n'
374+
u'Description : \n'
375+
u'\n')))
376+
290377
def test_info_available(self):
291378
"""Test whether only packages in the repository are listed."""
292379
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
@@ -301,6 +388,45 @@ def test_info_available(self):
301388
self.HOLE_X86_64_INFO,
302389
self.PEPPER_UPDATES_INFO)))
303390

391+
def test_info_extras(self):
392+
"""Test whether only extras installed from the repository are listed."""
393+
history = self.cli.base.history
394+
for pkg in self.cli.base.sack.query().installed().filter(name='tour'):
395+
mockSwdbPkg(history, pkg, repo='main')
396+
397+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
398+
with support.patch_std_streams() as (stdout, _):
399+
support.command_run(cmd, ['main', 'info', 'extras'])
400+
401+
self.assertEqual(
402+
stdout.getvalue(),
403+
u'Extra Packages\n'
404+
u'Name : tour\n'
405+
u'Version : 5\n'
406+
u'Release : 0\n'
407+
u'Arch : noarch\n'
408+
u'Size : 0.0 \n'
409+
u'Source : None\n'
410+
u'Repo : @System\n'
411+
u'From repo : main\n'
412+
u'Summary : \n'
413+
u'License : \n'
414+
u'Description : \n\n')
415+
416+
def test_info_installed(self):
417+
"""Test whether only packages installed from the repository are listed."""
418+
history = self.cli.base.history
419+
for pkg in self.cli.base.sack.query().installed().filter(name='pepper'):
420+
mockSwdbPkg(history, pkg, repo='main')
421+
422+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
423+
with support.patch_std_streams() as (stdout, _):
424+
support.command_run(cmd, ['main', 'info', 'installed'])
425+
426+
self.assertEqual(
427+
stdout.getvalue(),
428+
''.join((self.INSTALLED_TITLE, self.PEPPER_SYSTEM_INFO)))
429+
304430
def test_info_obsoletes(self):
305431
"""Test whether only obsoletes in the repository are listed."""
306432
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
@@ -381,6 +507,34 @@ def test_all(self):
381507
dnf.subject.Subject('tour-5-0').get_best_query(self.cli.base.sack)
382508
.available()))
383509

510+
class RepoPkgsReinstallOldSubCommandTest(support.ResultTestCase):
511+
512+
"""Tests of ``dnf.cli.commands.RepoPkgsCommand.ReinstallOldSubCommand`` class."""
513+
514+
def setUp(self):
515+
"""Prepare the test fixture."""
516+
super(RepoPkgsReinstallOldSubCommandTest, self).setUp()
517+
base = support.BaseCliStub('main')
518+
base.init_sack()
519+
self.cli = base.mock_cli()
520+
521+
def test_all(self):
522+
"""Test whether all packages from the repository are reinstalled."""
523+
history = self.cli.base.history
524+
for pkg in self.cli.base.sack.query().installed():
525+
reponame = 'main' if pkg.name != 'pepper' else 'non-main'
526+
mockSwdbPkg(history, pkg, repo=reponame)
527+
528+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
529+
support.command_run(cmd, ['main', 'reinstall-old'])
530+
531+
self.assertResult(self.cli.base, itertools.chain(
532+
self.cli.base.sack.query().installed().filter(name__neq='librita'),
533+
dnf.subject.Subject('librita.i686').get_best_query(self.cli.base.sack)
534+
.installed(),
535+
dnf.subject.Subject('librita').get_best_query(self.cli.base.sack)
536+
.available()))
537+
384538

385539
class RepoPkgsReinstallSubCommandTest(unittest.TestCase):
386540

@@ -445,6 +599,52 @@ def setUp(self):
445599
self.cli = support.BaseCliStub('distro').mock_cli()
446600
self.cli.base.init_sack()
447601

602+
def test_run_on_repo_spec_sync(self):
603+
"""Test running with a package which can be synchronized."""
604+
history = self.cli.base.history
605+
for pkg in self.cli.base.sack.query().installed():
606+
reponame = 'non-distro' if pkg.name == 'pepper' else 'distro'
607+
mockSwdbPkg(history, pkg, repo=reponame)
608+
609+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
610+
support.command_run(cmd, ['non-distro', 'remove-or-distro-sync', 'pepper'])
611+
612+
self.assertResult(self.cli.base, itertools.chain(
613+
self.cli.base.sack.query().installed().filter(name__neq='pepper'),
614+
dnf.subject.Subject('pepper').get_best_query(self.cli.base.sack)
615+
.available()))
616+
617+
def test_run_on_repo_spec_remove(self):
618+
"""Test running with a package which must be removed."""
619+
history = self.cli.base.history
620+
for pkg in self.cli.base.sack.query().installed():
621+
reponame = 'non-distro' if pkg.name == 'hole' else 'distro'
622+
mockSwdbPkg(history, pkg, repo=reponame)
623+
624+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
625+
support.command_run(cmd, ['non-distro', 'remove-or-distro-sync', 'hole'])
626+
627+
self.assertResult(
628+
self.cli.base,
629+
self.cli.base.sack.query().installed().filter(name__neq='hole'))
630+
631+
def test_run_on_repo_all(self):
632+
"""Test running without a package specification."""
633+
nondist = {'pepper', 'hole'}
634+
history = self.cli.base.history
635+
for pkg in self.cli.base.sack.query().installed():
636+
reponame = 'non-distro' if pkg.name in nondist else 'distro'
637+
mockSwdbPkg(history, pkg, repo=reponame)
638+
639+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
640+
support.command_run(cmd, ['non-distro', 'remove-or-distro-sync'])
641+
642+
self.assertResult(self.cli.base, itertools.chain(
643+
self.cli.base.sack.query().installed().filter(name__neq='pepper')
644+
.filter(name__neq='hole'),
645+
dnf.subject.Subject('pepper').get_best_query(self.cli.base.sack)
646+
.available()))
647+
448648
@mock.patch('dnf.cli.commands._', dnf.pycomp.NullTranslations().ugettext)
449649
def test_run_on_repo_spec_notinstalled(self):
450650
"""Test running with a package which is not installed."""
@@ -484,6 +684,70 @@ def setUp(self):
484684
base.init_sack()
485685
self.cli = base.mock_cli()
486686

687+
def test_all_not_installed(self):
688+
"""Test whether it fails if no package is installed from the repository."""
689+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
690+
self.assertRaises(dnf.exceptions.Error,
691+
support.command_run, cmd,
692+
['non-distro', 'remove-or-distro-sync'])
693+
694+
self.assertResult(self.cli.base, self.cli.base.sack.query().installed())
695+
696+
def test_all_reinstall(self):
697+
"""Test whether all packages from the repository are reinstalled."""
698+
history = self.cli.base.history
699+
for pkg in self.cli.base.sack.query().installed():
700+
reponame = 'distro' if pkg.name != 'tour' else 'non-distro'
701+
mockSwdbPkg(history, pkg, repo=reponame)
702+
703+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
704+
support.command_run(cmd, ['non-distro', 'remove-or-reinstall'])
705+
706+
self.assertResult(self.cli.base, itertools.chain(
707+
self.cli.base.sack.query().installed().filter(name__neq='tour'),
708+
dnf.subject.Subject('tour').get_best_query(self.cli.base.sack)
709+
.available()))
710+
711+
def test_all_remove(self):
712+
"""Test whether all packages from the repository are removed."""
713+
history = self.cli.base.history
714+
for pkg in self.cli.base.sack.query().installed():
715+
reponame = 'distro' if pkg.name != 'hole' else 'non-distro'
716+
mockSwdbPkg(history, pkg, repo=reponame)
717+
718+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
719+
support.command_run(cmd, ['non-distro', 'remove-or-reinstall'])
720+
721+
self.assertResult(
722+
self.cli.base,
723+
self.cli.base.sack.query().installed().filter(name__neq='hole'))
724+
725+
726+
class RepoPkgsRemoveSubCommandTest(support.ResultTestCase):
727+
728+
"""Tests of ``dnf.cli.commands.RepoPkgsCommand.RemoveSubCommand`` class."""
729+
730+
def setUp(self):
731+
"""Prepare the test fixture."""
732+
super(RepoPkgsRemoveSubCommandTest, self).setUp()
733+
base = support.BaseCliStub('main')
734+
base.init_sack()
735+
self.cli = base.mock_cli()
736+
737+
def test_all(self):
738+
"""Test whether only packages from the repository are removed."""
739+
history = self.cli.base.history
740+
for pkg in self.cli.base.sack.query().installed():
741+
reponame = 'main' if pkg.name == 'pepper' else 'non-main'
742+
mockSwdbPkg(history, pkg, repo=reponame)
743+
744+
cmd = dnf.cli.commands.RepoPkgsCommand(self.cli)
745+
support.command_run(cmd, ['main', 'remove'])
746+
747+
self.assertResult(
748+
self.cli.base,
749+
self.cli.base.sack.query().installed().filter(name__neq='pepper'))
750+
487751

488752
class RepoPkgsUpgradeSubCommandTest(support.ResultTestCase):
489753

tests/test_list.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ def test_list_installed(self):
4040
ypl = base._do_package_lists('installed')
4141
self.assertEqual(len(ypl.installed), support.TOTAL_RPMDB_COUNT)
4242

43+
def test_list_installed_reponame(self):
44+
"""Test whether only packages installed from the repository are listed."""
45+
base = support.MockBase()
46+
expected = base.sack.query().installed().filter(name={'pepper',
47+
'librita'})
48+
history = base.history
49+
for pkg in expected:
50+
support.mockSwdbPkg(history, pkg, repo='main')
51+
52+
lists = base._do_package_lists('installed', reponame='main')
53+
54+
self.assertCountEqual(lists.installed, expected)
55+
4356
def test_list_updates(self):
4457
base = support.MockBase("updates", "main")
4558
ypl = base._do_package_lists('upgrades')

0 commit comments

Comments
 (0)