fix tests

This commit is contained in:
Tomas Kopecek 2020-02-25 14:42:30 +01:00
parent f873934473
commit 04f6ba886e
28 changed files with 404 additions and 458 deletions

View file

@ -9,8 +9,9 @@ except ImportError:
import unittest
from koji_cli.commands import handle_remove_host_from_channel
from . import utils
class TestRemoveHostFromChannel(unittest.TestCase):
class TestRemoveHostFromChannel(utils.CliTestCase):
# Show long diffs in error output...
maxDiff = None
@ -45,10 +46,10 @@ class TestRemoveHostFromChannel(unittest.TestCase):
session.removeHostFromChannel.assert_called_once_with(host, channel)
self.assertNotEqual(rv, 1)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('sys.stderr', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_remove_host_from_channel_no_host(
self, activate_session_mock, stdout):
self, activate_session_mock, stderr):
host = 'host'
host_info = None
channel = 'channel'
@ -62,8 +63,10 @@ class TestRemoveHostFromChannel(unittest.TestCase):
# Run it and check immediate output
# args: host, channel
# expected: failed: no such host
rv = handle_remove_host_from_channel(options, session, args)
actual = stdout.getvalue()
with self.assertRaises(SystemExit) as ex:
handle_remove_host_from_channel(options, session, args)
self.assertExitCode(ex, 1)
actual = stderr.getvalue()
expected = 'No such host: host\n'
self.assertMultiLineEqual(actual, expected)
# Finally, assert that things were called as we expected.
@ -71,12 +74,11 @@ class TestRemoveHostFromChannel(unittest.TestCase):
session.getHost.assert_called_once_with(host)
session.listChannels.assert_not_called()
session.removeHostFromChannel.assert_not_called()
self.assertEqual(rv, 1)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('sys.stderr', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_remove_host_from_channel_not_a_member(
self, activate_session_mock, stdout):
self, activate_session_mock, stderr):
host = 'host'
host_info = {'id': 1}
channel = 'channel'
@ -92,9 +94,11 @@ class TestRemoveHostFromChannel(unittest.TestCase):
session.listChannels.return_value = channel_infos
# Run it and check immediate output
# args: host, channel
# expected: success: host isn't belong to channel
rv = handle_remove_host_from_channel(options, session, args)
actual = stdout.getvalue()
# expected: failed: host isn't belong to channel
with self.assertRaises(SystemExit) as ex:
handle_remove_host_from_channel(options, session, args)
self.assertExitCode(ex, 1)
actual = stderr.getvalue()
expected = 'Host host is not a member of channel channel\n'
self.assertMultiLineEqual(actual, expected)
# Finally, assert that things were called as we expected.
@ -102,7 +106,6 @@ class TestRemoveHostFromChannel(unittest.TestCase):
session.getHost.assert_called_once_with(host)
session.listChannels.assert_called_once_with(host_info['id'])
session.removeHostFromChannel.assert_not_called()
self.assertEqual(rv, 1)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('sys.stderr', new_callable=six.StringIO)
@ -119,8 +122,9 @@ class TestRemoveHostFromChannel(unittest.TestCase):
# Run it and check immediate output
# args: _empty_
# expected: failed, help msg shows
with self.assertRaises(SystemExit) as cm:
with self.assertRaises(SystemExit) as ex:
handle_remove_host_from_channel(options, session, args)
self.assertExitCode(ex, 2)
actual_stdout = stdout.getvalue()
actual_stderr = stderr.getvalue()
expected_stdout = ''
@ -137,10 +141,6 @@ class TestRemoveHostFromChannel(unittest.TestCase):
session.getHost.assert_not_called()
session.listChannels.assert_not_called()
session.removeHostFromChannel.assert_not_called()
if isinstance(cm.exception, int):
self.assertEqual(cm.exception, 2)
else:
self.assertEqual(cm.exception.code, 2)
if __name__ == '__main__':