Using real objects is considered polite

Previously, ToddlerBase and derived classes were used directly, but none
of the methods is marked as a class or static method. Amazingly enough,
it worked regardless. It's useful to have a constructor, though, so do
things conventionally.

As we can't just monkey-patch the classes then, use the shared `toddler`
fixture which creates the object for the test methods that need it.

Signed-off-by: Nils Philippsen <nils@redhat.com>
This commit is contained in:
Nils Philippsen 2020-07-16 13:16:38 +02:00
parent 3f081a66ac
commit 7fe0937190
14 changed files with 167 additions and 225 deletions

View file

@ -2,17 +2,15 @@ from unittest.mock import Mock, patch
import pytest
import toddlers.plugins.packager_bugzilla_sync
from toddlers.plugins.packager_bugzilla_sync import main, PackagerBugzillaSync
class TestPackagerBugzillaSyncToddler:
def test_accepts_topic_invalid(self):
assert (
toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.accepts_topic(
"foo.bar"
)
is False
)
toddler_cls = PackagerBugzillaSync
def test_accepts_topic_invalid(self, toddler):
assert toddler.accepts_topic("foo.bar") is False
@pytest.mark.parametrize(
"topic",
@ -22,26 +20,22 @@ class TestPackagerBugzillaSyncToddler:
"org.fedoraproject.stg.toddlers.trigger.packager_bugzilla_sync",
],
)
def test_accepts_topic_valid(self, topic):
assert toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.accepts_topic(
topic
)
def test_accepts_topic_valid(self, toddler, topic):
assert toddler.accepts_topic(topic)
def test_process_no_email_override(self, capsys):
def test_process_no_email_override(self, toddler, capsys):
with pytest.raises(KeyError, match=r"'email_overrides_file'"):
toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.process(
config={}, message=None, username=False, dry_run=True
)
toddler.process(config={}, message=None, username=False, dry_run=True)
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
def test_process_no_email_override_file(self, capsys):
def test_process_no_email_override_file(self, toddler, capsys):
with pytest.raises(
FileNotFoundError, match=r"No such file or directory: 'test'"
):
toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.process(
toddler.process(
config={"email_overrides_file": "test"},
message=None,
username=False,
@ -60,14 +54,20 @@ class TestPackagerBugzillaSyncToddler:
@patch("toddlers.utils.bugzilla_system.add_user_to_group")
@patch("toml.load")
def test_process(
self, toml_load, bz_user_grp, get_bz_grp_mbr, get_bz_email, get_fas_grp_mbr
self,
toml_load,
bz_user_grp,
get_bz_grp_mbr,
get_bz_email,
get_fas_grp_mbr,
toddler,
):
toml_load.return_value = {}
get_fas_grp_mbr.return_value = ["pingou", "nils"]
get_bz_email.side_effect = ["pingou@fp.o", "nils@fp.o"]
get_bz_grp_mbr.return_value = ["pingou@fp.o", "nphilipp@fp.o"]
toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.process(
toddler.process(
config={"email_overrides_file": "test", "bugzilla_group": "fedora_contrib"},
message=None,
username=False,
@ -92,13 +92,13 @@ class TestPackagerBugzillaSyncToddler:
@patch("toddlers.utils.bugzilla_system.add_user_to_group")
@patch("toml.load")
def test_process_username(
self, toml_load, bz_user_grp, get_bz_grp_mbr, get_bz_email
self, toml_load, bz_user_grp, get_bz_grp_mbr, get_bz_email, toddler
):
toml_load.return_value = {}
get_bz_email.side_effect = ["nils@fp.o"]
get_bz_grp_mbr.return_value = ["pingou@fp.o"]
toddlers.plugins.packager_bugzilla_sync.PackagerBugzillaSync.process(
toddler.process(
config={"email_overrides_file": "test", "bugzilla_group": "fedora_contrib"},
message=None,
username="nils",
@ -117,7 +117,7 @@ class TestPackagerBugzillaSyncToddler:
def test_main_no_args(self, capsys):
with pytest.raises(SystemExit):
toddlers.plugins.packager_bugzilla_sync.main([])
main([])
out, err = capsys.readouterr()
assert out == ""
@ -131,7 +131,7 @@ class TestPackagerBugzillaSyncToddler:
@patch("toml.load", new=Mock(return_value={}))
def test_main_debug(self, capsys):
with pytest.raises(KeyError, match=r"'email_overrides_file'"):
toddlers.plugins.packager_bugzilla_sync.main(["test.cfg", "--debug"])
main(["test.cfg", "--debug"])
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""
@ -139,7 +139,7 @@ class TestPackagerBugzillaSyncToddler:
@patch("toml.load", new=Mock(return_value={}))
def test_main(self, capsys):
with pytest.raises(KeyError, match=r"'email_overrides_file'"):
toddlers.plugins.packager_bugzilla_sync.main(["test.cfg"])
main(["test.cfg"])
out, err = capsys.readouterr()
assert out == "Failed to load the file containing the email-overrides\n"
assert err == ""