fix context mocking

This commit is contained in:
Mike McLean 2024-07-11 14:31:32 -04:00 committed by Tomas Kopecek
parent 1c3b430e02
commit 8d8274a35e
11 changed files with 67 additions and 49 deletions

View file

@ -10,50 +10,49 @@ IP = kojihub.InsertProcessor
class TestAddBType(unittest.TestCase):
@mock.patch('kojihub.kojihub.verify_name_internal')
@mock.patch('kojihub.kojihub.list_btypes')
@mock.patch('kojihub.kojihub.InsertProcessor')
def test_add_btype(self, InsertProcessor, list_btypes, verify_name_internal):
# Not sure why mock can't patch kojihub.context, so we do this
session = kojihub.kojihub.context.session = mock.MagicMock()
mocks = [InsertProcessor, list_btypes, session]
def setUp(self):
self.verify_name_internal = mock.patch('kojihub.kojihub.verify_name_internal').start()
self.list_btypes = mock.patch('kojihub.kojihub.list_btypes').start()
self.InsertProcessor = mock.patch('kojihub.kojihub.InsertProcessor').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.session = self.context.session
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
session.assertPerm = mock.MagicMock()
verify_name_internal.return_value = None
self.session.assertPerm = mock.MagicMock()
self.verify_name_internal.return_value = None
def tearDown(self):
mock.patch.stopall()
def test_add_btype(self):
# expected case
list_btypes.return_value = None
insert = InsertProcessor.return_value
self.list_btypes.return_value = None
insert = self.InsertProcessor.return_value
kojihub.add_btype('new_btype')
InsertProcessor.assert_called_once()
self.InsertProcessor.assert_called_once()
insert.execute.assert_called_once()
args, kwargs = InsertProcessor.call_args
args, kwargs = self.InsertProcessor.call_args
ip = IP(*args, **kwargs)
self.assertEqual(ip.table, 'btype')
self.assertEqual(ip.data, {'name': 'new_btype'})
self.assertEqual(ip.rawdata, {})
session.assertPerm.assert_called_with('admin')
for m in mocks:
m.reset_mock()
session.assertPerm = mock.MagicMock()
self.session.assertPerm.assert_called_with('admin')
def test_btype_exists(self):
# already exists
list_btypes.return_value = True
self.list_btypes.return_value = True
with self.assertRaises(koji.GenericError):
kojihub.add_btype('new_btype')
InsertProcessor.assert_not_called()
session.assertPerm.assert_called_with('admin')
self.InsertProcessor.assert_not_called()
self.session.assertPerm.assert_called_with('admin')
def test_btype_badname(self):
# name is longer as expected
new_btype = 'new-btype+'
verify_name_internal.side_effect = koji.GenericError
self.verify_name_internal.side_effect = koji.GenericError
with self.assertRaises(koji.GenericError):
kojihub.add_btype(new_btype)
# not except regex rules
verify_name_internal.side_effect = koji.GenericError
with self.assertRaises(koji.GenericError):
kojihub.add_btype(new_btype)
# the end

View file

@ -17,6 +17,8 @@ class TestAddExternalRepoToTag(unittest.TestCase):
self.get_external_repo = mock.patch('kojihub.kojihub.get_external_repo').start()
self.get_tag_external_repos = mock.patch('kojihub.kojihub.get_tag_external_repos').start()
self.parse_arches = mock.patch('koji.parse_arches').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.tag_info = {'id': 1, 'name': self.tag_name}
self.external_repo_info = {'id': 123, 'name': 'test-repo'}
self.priority = 11

View file

@ -15,6 +15,8 @@ class TestAddGroupMember(unittest.TestCase):
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.get_user = mock.patch('kojihub.kojihub.get_user').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()

View file

@ -15,6 +15,8 @@ class TestAddUserKrbPrincipal(unittest.TestCase):
self.get_user = mock.patch('kojihub.kojihub.get_user').start()
self.verify_name_user = mock.patch('kojihub.kojihub.verify_name_user').start()
self.get_user_by_krb_principal = mock.patch('kojihub.kojihub.get_user_by_krb_principal').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.username = 'testuser'
self.krbprincipal = '%s@TEST.COM' % self.username
self.userinfo = {'id': 1, 'name': self.username}

View file

@ -12,6 +12,8 @@ class TestCreateImageBuild(unittest.TestCase):
def setUp(self):
self.get_build = mock.patch('kojihub.kojihub.get_build').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.exports = kojihub.RootExports()
self.context = mock.patch('kojihub.kojihub.context').start()
# It seems MagicMock will not automatically handle attributes that

View file

@ -27,6 +27,8 @@ class TestDeleteBuildTarget(unittest.TestCase):
self.UpdateProcessor = mock.patch('kojihub.kojihub.UpdateProcessor',
side_effect=self.getUpdate).start()
self.updates = []
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.context_db = mock.patch('kojihub.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"

View file

@ -23,6 +23,8 @@ class TestDisableChannel(unittest.TestCase):
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.get_channel = mock.patch('kojihub.kojihub.get_channel').start()
self.context = mock.patch('kojihub.kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.UpdateProcessor = mock.patch('kojihub.kojihub.UpdateProcessor',
side_effect=self.getUpdate).start()
self.updates = []

View file

@ -105,18 +105,21 @@ class TestDistRepoInit(unittest.TestCase):
class TestDistRepo(unittest.TestCase):
@mock.patch('kojihub.kojihub.assert_policy')
@mock.patch('kojihub.kojihub.dist_repo_init')
@mock.patch('kojihub.kojihub.make_task')
def test_DistRepo(self, make_task, dist_repo_init, assert_policy):
session = kojihub.context.session = mock.MagicMock()
def setUp(self):
self.assert_policy = mock.patch('kojihub.kojihub.assert_policy').start()
self.dist_repo_init = mock.patch('kojihub.kojihub.dist_repo_init').start()
self.make_task = mock.patch('kojihub.kojihub.make_task').start()
self.context = mock.patch('kojihub.kojihub.context').start()
def tearDown(self):
mock.patch.stopall()
def test_DistRepo(self):
session = self.context.session
session.user_id = 123
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
session.hasPerm = mock.MagicMock()
session.hasPerm.return_value = False
dist_repo_init.return_value = ('repo_id', 'event_id')
make_task.return_value = 'task_id'
self.dist_repo_init.return_value = ('repo_id', 'event_id')
self.make_task.return_value = 'task_id'
exports = kojihub.RootExports()
exports.getBuildConfig = mock.MagicMock()
exports.getBuildConfig.return_value = {'extra': {}}
@ -124,10 +127,10 @@ class TestDistRepo(unittest.TestCase):
ret = exports.distRepo('tag', 'keys')
session.hasPerm.assert_has_calls([mock.call('dist-repo'), mock.call('admin')])
assert_policy.assert_called_once_with('dist_repo', {'tag': 'tag'})
dist_repo_init.assert_called_once()
make_task.assert_called_once()
self.assertEqual(ret, make_task.return_value)
self.assert_policy.assert_called_once_with('dist_repo', {'tag': 'tag'})
self.dist_repo_init.assert_called_once()
self.make_task.assert_called_once()
self.assertEqual(ret, self.make_task.return_value)
exports.getBuildConfig.assert_called_once_with('tag')
@ -216,6 +219,7 @@ class TestDistRepoMove(unittest.TestCase):
self.get_build = mock.patch('kojihub.kojihub.get_build').start()
self.get_rpm.side_effect = self.our_get_rpm
self.get_build.side_effect = self.our_get_build
self.context = mock.patch('kojihub.kojihub.context').start()
def tearDown(self):
mock.patch.stopall()
@ -228,7 +232,7 @@ class TestDistRepoMove(unittest.TestCase):
return self.builds[buildInfo]
def test_distRepoMove(self):
session = kojihub.context.session = mock.MagicMock()
session = self.context.session
session.user_id = 123
exports = kojihub.HostExports()
exports.distRepoMove(self.rinfo['id'], self.uploadpath, self.arch)

View file

@ -30,7 +30,8 @@ class TestEditBuildTarget(unittest.TestCase):
self.target_info = {'id': 123, 'name': self.target_name}
self.build_tag_info = {'id': 111, 'name': self.build_tag}
self.dest_tag_info = {'id': 112, 'name': self.dest_tag}
self.session = kojihub.context.session = mock.MagicMock()
self.context = mock.patch('kojihub.kojihub.context').start()
self.session = self.context.session
self.session.assertPerm = mock.MagicMock()
self.QueryProcessor = mock.patch('kojihub.kojihub.QueryProcessor',
side_effect=self.getQuery).start()

View file

@ -7,7 +7,7 @@ import kojihub
class TestDeleteEventId(unittest.TestCase):
@mock.patch('kojihub.kojihub.context')
def test_delete_event_id(self, context):
kojihub.context.event_id = 123
context.event_id = 123
kojihub._delete_event_id()
self.assertFalse(hasattr(context, 'event_id'))

View file

@ -15,17 +15,19 @@ class DummyExports(object):
class TestMulticall(unittest.TestCase):
def setUp(self):
self.context = mock.patch('kojihub.kojixmlrpc.context').start()
self.context_db = mock.patch('kojihub.db.context').start()
self.kojihub = mock.patch('kojihub.kojixmlrpc.kojihub').start()
self.registry = HandlerRegistry()
self.exports = DummyExports()
self.registry.register_instance(self.exports)
def tearDown(self):
mock.patch.stopall()
def test_multicall(self):
self.context_db = mock.patch('kojihub.db.context').start()
kojixmlrpc.kojihub = mock.MagicMock()
kojixmlrpc.context.opts = mock.MagicMock()
kojixmlrpc.context.session = mock.MagicMock()
self.registry = HandlerRegistry()
self.exports = DummyExports()
self.registry.register_instance(self.exports)
calls = [{'methodName': 'foo', 'params': [1]},
{'methodName': 'non', 'params': [mock.ANY]},
{'methodName': 'foo', 'params': [2, Exception('with int arg', 1)]},