Skip to content

Commit

Permalink
Make the tests use the pytest fixtures and assert system
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <aurelien@bompard.org>
  • Loading branch information
abompard committed Dec 11, 2023
1 parent bdf7da9 commit 961b82d
Show file tree
Hide file tree
Showing 14 changed files with 501 additions and 523 deletions.
26 changes: 0 additions & 26 deletions tests/__init__.py
@@ -1,26 +0,0 @@
# This file is part of fedora_messaging.
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os

import crochet


FIXTURES_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures/"))

# Tests use pytest-twisted, which manages the Twisted reactor for us. This stops
# crochet from starting the reactor in its own thread.
crochet.no_setup()
31 changes: 31 additions & 0 deletions tests/conftest.py
@@ -0,0 +1,31 @@
# This file is part of fedora_messaging.
# Copyright (C) 2018 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os

import crochet
import pytest


@pytest.fixture(autouse=True, scope="session")
def crochet_no_setup():
crochet.no_setup()


@pytest.fixture
def fixtures_dir():
return os.path.abspath(os.path.join(os.path.dirname(__file__), "fixtures/"))
2 changes: 1 addition & 1 deletion tests/fixtures/good_conf.toml
@@ -1,4 +1,4 @@
callback = "fedora_messaging.tests.unit.test_cli:echo"
callback = "tests.unit.test_cli:echo"

[[bindings]]
exchange = "e"
Expand Down
11 changes: 6 additions & 5 deletions tests/integration/test_cli.py
Expand Up @@ -25,12 +25,13 @@
from twisted.internet import threads

from fedora_messaging import api, exceptions, message
from fedora_messaging.tests import FIXTURES_DIR

from .utils import sleep


CLI_CONF = os.path.join(FIXTURES_DIR, "cli_integration.toml")
@pytest.fixture
def cli_conf(fixtures_dir):
return os.path.join(fixtures_dir, "cli_integration.toml")


def halt_exit_0(message):
Expand Down Expand Up @@ -64,13 +65,13 @@ def queue(scope="function"):
],
)
@pytest_twisted.inlineCallbacks
def test_consume_halt_with_exitcode(callback, exit_code, msg, queue):
def test_consume_halt_with_exitcode(callback, exit_code, msg, queue, cli_conf):
"""Assert user execution halt with reason and exit_code is reported."""
args = [
"fedora-messaging",
f"--conf={CLI_CONF}",
f"--conf={cli_conf}",
"consume",
f"--callback=fedora_messaging.tests.integration.test_cli:{callback}",
f"--callback=tests.integration.test_cli:{callback}",
f"--queue-name={queue}",
"--exchange=amq.topic",
"--routing-key=#",
Expand Down
63 changes: 21 additions & 42 deletions tests/unit/test_api.py
Expand Up @@ -47,7 +47,7 @@ def callback(self):

callback = api._check_callback(class_instance.callback)

self.assertIs(callback, callback)
assert callback == class_instance.callback

def test_func(self):
"""Assert functions are valid."""
Expand All @@ -57,7 +57,7 @@ def callback(message):

cb = api._check_callback(callback)

self.assertIs(cb, callback)
assert cb is callback

def test_class(self):
"""Assert classes are instantiated."""
Expand All @@ -68,18 +68,16 @@ def __call__(self, message):

callback = api._check_callback(Callback)

self.assertEqual(callback(None), "It worked")
assert callback(None) == "It worked"

def test_class_no_call(self):
"""Assert classes are instantiated."""

class Callback:
pass

try:
with pytest.raises(ValueError):
api._check_callback(Callback)
self.fail("_check_callback failed to raise an exception")
except ValueError:
pass

def test_class_init_args(self):
Expand All @@ -89,19 +87,13 @@ class Callback:
def __init__(self, args):
self.args = args

try:
with pytest.raises(TypeError):
api._check_callback(Callback)
self.fail("_check_callback failed to raise a TypeError")
except TypeError:
pass

def test_not_callable(self):
"""Assert a ValueError is raised for things that can't be called."""
try:
with pytest.raises(ValueError):
api._check_callback("")
self.fail("_check_callback failed to raise an ValueError")
except ValueError:
pass


@mock.patch("fedora_messaging.api._twisted_service")
Expand Down Expand Up @@ -144,9 +136,8 @@ def test_bindings_dict(self, mock_service):

def test_bindings_invalid_type(self, mock_service):
"""Assert bindings are validated and result in a value error if they are invalid."""
self.assertRaises(
ValueError, api.twisted_consume, self.dummy_callback, "test_bindings"
)
with pytest.raises(ValueError):
api.twisted_consume(self.dummy_callback, "test_bindings")

def test_bindings_list_of_dict(self, mock_service):
"""Assert consume is working(bindings type is dict)"""
Expand All @@ -160,13 +151,8 @@ def test_bindings_list_of_dict(self, mock_service):

def test_queues_invalid_type(self, mock_service):
"""Assert queues are validated and result in a value error if they are invalid."""
self.assertRaises(
ValueError,
api.twisted_consume,
self.dummy_callback,
None,
"should be a dict",
)
with pytest.raises(ValueError):
api.twisted_consume(self.dummy_callback, None, "should be a dict")

def test_with_queues(self, mock_service):
"""Assert queues is used over the config if provided."""
Expand Down Expand Up @@ -264,11 +250,9 @@ def test_publish_to_exchange(self, mock_twisted_publish):

mock_twisted_publish.assert_called_once_with(message, exchange)
mock_twisted_publish.return_value.wait.assert_called_once_with(timeout=30)
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
self.assertEqual(
self.publish_failed_signal_data, expected_publish_failed_signal_data
)
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
assert self.publish_signal_data == expected_publish_signal_data
assert self.publish_failed_signal_data == expected_publish_failed_signal_data

@mock.patch.dict(
"fedora_messaging.config.conf", {"publish_exchange": "test_public_exchange"}
Expand All @@ -295,11 +279,9 @@ def test_publish_to_config_exchange(self, mock_twisted_publish):
api.publish(message)

mock_twisted_publish.assert_called_once_with(message, "test_public_exchange")
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
self.assertEqual(
self.publish_failed_signal_data, expected_publish_failed_signal_data
)
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
assert self.publish_signal_data == expected_publish_signal_data
assert self.publish_failed_signal_data == expected_publish_failed_signal_data

def test_publish_failed(self, mock_twisted_publish):
"""Assert an exception is raised when message can't be published."""
Expand All @@ -319,16 +301,13 @@ def test_publish_failed(self, mock_twisted_publish):
}
mock_twisted_publish.return_value.wait.side_effect = expected_exception

self.assertRaises(
type(expected_exception), api.publish, message, exchange=exchange
)
with pytest.raises(type(expected_exception)):
api.publish(message, exchange=exchange)

mock_twisted_publish.assert_called_once_with(message, exchange)
self.assertEqual(self.pre_publish_signal_data, expected_pre_publish_signal_data)
self.assertEqual(self.publish_signal_data, expected_publish_signal_data)
self.assertEqual(
self.publish_failed_signal_data, expected_publish_failed_signal_data
)
assert self.pre_publish_signal_data == expected_pre_publish_signal_data
assert self.publish_signal_data == expected_publish_signal_data
assert self.publish_failed_signal_data == expected_publish_failed_signal_data


@pytest_twisted.inlineCallbacks
Expand Down

0 comments on commit 961b82d

Please sign in to comment.