From 6439627ce49f5c47ac61ed76900bd69225613a22 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Thu, 14 Mar 2024 19:19:36 -0700 Subject: [PATCH] test_apt: migrate from unittest to pytest (#82666) Signed-off-by: Abhijeet Kasurde --- test/units/modules/test_apt.py | 80 +++++++++++++++++----------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/test/units/modules/test_apt.py b/test/units/modules/test_apt.py index 16cbee32deb..d207320c82a 100644 --- a/test/units/modules/test_apt.py +++ b/test/units/modules/test_apt.py @@ -1,45 +1,45 @@ +# Copyright: Contributors to the Ansible project +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) from __future__ import annotations import collections -from unittest.mock import Mock -import unittest - -from ansible.modules.apt import ( - expand_pkgspec_from_fnmatches, +from ansible.modules.apt import expand_pkgspec_from_fnmatches +import pytest + +FakePackage = collections.namedtuple("Package", ("name",)) +fake_cache = [ + FakePackage("apt"), + FakePackage("apt-utils"), + FakePackage("not-selected"), +] + + +@pytest.mark.parametrize( + ("test_input", "expected"), + [ + pytest.param( + ["apt"], + ["apt"], + id="trivial", + ), + pytest.param( + ["apt=1.0*"], + ["apt=1.0*"], + id="version-wildcard", + ), + pytest.param( + ["apt*=1.0*"], + ["apt", "apt-utils"], + id="pkgname-wildcard-version", + ), + pytest.param( + ["apt*"], + ["apt", "apt-utils"], + id="pkgname-expands", + ), + ], ) - - -class AptExpandPkgspecTestCase(unittest.TestCase): - - def setUp(self): - FakePackage = collections.namedtuple("Package", ("name",)) - self.fake_cache = [ - FakePackage("apt"), - FakePackage("apt-utils"), - FakePackage("not-selected"), - ] - - def test_trivial(self): - pkg = ["apt"] - self.assertEqual( - expand_pkgspec_from_fnmatches(None, pkg, self.fake_cache), pkg) - - def test_version_wildcard(self): - pkg = ["apt=1.0*"] - self.assertEqual( - expand_pkgspec_from_fnmatches(None, pkg, self.fake_cache), pkg) - - def test_pkgname_wildcard_version_wildcard(self): - pkg = ["apt*=1.0*"] - m_mock = Mock() - self.assertEqual( - expand_pkgspec_from_fnmatches(m_mock, pkg, self.fake_cache), - ['apt', 'apt-utils']) - - def test_pkgname_expands(self): - pkg = ["apt*"] - m_mock = Mock() - self.assertEqual( - expand_pkgspec_from_fnmatches(m_mock, pkg, self.fake_cache), - ["apt", "apt-utils"]) +def test_expand_pkgspec_from_fnmatches(test_input, expected): + """Test positive cases of ``expand_pkgspec_from_fnmatches``.""" + assert expand_pkgspec_from_fnmatches(None, test_input, fake_cache) == expected