本文整理汇总了Python中unittest.mock.create_autospec方法的典型用法代码示例。如果您正苦于以下问题:Python mock.create_autospec方法的具体用法?Python mock.create_autospec怎么用?Python mock.create_autospec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock
的用法示例。
在下文中一共展示了mock.create_autospec方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_and_run_keyboard_interrupt_cancels_queries
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_create_and_run_keyboard_interrupt_cancels_queries(validator):
validator._running_queries = [
Query(
query_id="12345",
lookml_ref=None,
query_task_id="abc",
explore_url="https://example.looker.com/x/12345",
)
]
mock_create_queries = create_autospec(validator._create_queries)
mock_create_queries.side_effect = KeyboardInterrupt()
validator._create_queries = mock_create_queries
mock_cancel_queries = create_autospec(validator._cancel_queries)
validator._cancel_queries = mock_cancel_queries
try:
validator._create_and_run(mode="batch")
except SpectaclesException:
mock_cancel_queries.assert_called_once_with(query_task_ids=["abc"])
示例2: test_find_token_valid_match
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_find_token_valid_match(self, token_re, token_cls, is_valid_id, is_valid_timestamp):
"""The first match with a valid user ID and timestamp should be returned as a `Token`."""
matches = [
mock.create_autospec(Match, spec_set=True, instance=True),
mock.create_autospec(Match, spec_set=True, instance=True),
]
tokens = [
mock.create_autospec(Token, spec_set=True, instance=True),
mock.create_autospec(Token, spec_set=True, instance=True),
]
token_re.finditer.return_value = matches
token_cls.side_effect = tokens
is_valid_id.side_effect = (False, True) # The 1st match will be invalid, 2nd one valid.
is_valid_timestamp.return_value = True
return_value = TokenRemover.find_token_in_message(self.msg)
self.assertEqual(tokens[1], return_value)
token_re.finditer.assert_called_once_with(self.msg.content)
示例3: test_write_pandas_data_to_right_libraries
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_write_pandas_data_to_right_libraries():
self = create_autospec(TopLevelTickStore, _arctic_lib=MagicMock(), _collection=MagicMock())
self._collection.find.return_value = [{'library_name': sentinel.libname1, 'start': sentinel.st1, 'end': sentinel.end1},
{'library_name': sentinel.libname2, 'start': sentinel.st2, 'end': sentinel.end2}]
slice1 = range(2)
slice2 = range(4)
when(self._slice).called_with(sentinel.data, sentinel.st1, sentinel.end1).then(slice1)
when(self._slice).called_with(sentinel.data, sentinel.st2, sentinel.end2).then(slice2)
mock_lib1 = Mock()
mock_lib2 = Mock()
when(self._arctic_lib.arctic.__getitem__).called_with(sentinel.libname1).then(mock_lib1)
when(self._arctic_lib.arctic.__getitem__).called_with(sentinel.libname2).then(mock_lib2)
with patch("arctic.tickstore.toplevel.to_dt") as patch_to_dt:
patch_to_dt.side_effect = [sentinel.st1, sentinel.end1, sentinel.st2, sentinel.end2]
TopLevelTickStore.write(self, 'blah', sentinel.data)
mock_lib1.write.assert_called_once_with('blah', slice1)
mock_lib2.write.assert_called_once_with('blah', slice2)
示例4: test_read
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_read():
self = create_autospec(TopLevelTickStore)
tsl = TickStoreLibrary(create_autospec(TickStore), create_autospec(DateRange))
self._get_libraries.return_value = [tsl, tsl]
dr = create_autospec(DateRange)
with patch('pandas.concat') as concat:
res = TopLevelTickStore.read(self, sentinel.symbol, dr,
columns=sentinel.include_columns,
include_images=sentinel.include_images)
assert concat.call_args_list == [call([tsl.library.read.return_value,
tsl.library.read.return_value])]
assert res == concat.return_value
assert tsl.library.read.call_args_list == [call(sentinel.symbol, tsl.date_range.intersection.return_value,
sentinel.include_columns, include_images=sentinel.include_images),
call(sentinel.symbol, tsl.date_range.intersection.return_value,
sentinel.include_columns, include_images=sentinel.include_images)]
示例5: mock_nova
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def mock_nova(self):
# NOTE(ft): create an extra mock for Nova calls with an admin account.
# Also make sure that the admin account is used only for this calls.
# The special mock is needed to validate tested function to retrieve
# appropriate data, as long as only calls with admin account return
# some specific data.
novaclient_spec = novaclient.Client('2')
nova = mock.create_autospec(novaclient_spec)
nova_admin = mock.create_autospec(novaclient_spec)
nova_patcher = mock.patch('novaclient.client.Client')
novaclient_getter = nova_patcher.start()
self.addCleanup(nova_patcher.stop)
novaclient_getter.side_effect = (
lambda *args, **kwargs: (
nova_admin
if (kwargs.get('session') == mock.sentinel.admin_session) else
nova
if (kwargs.get('session') == mock.sentinel.session) else
None))
return nova, nova_admin
示例6: get_batch_merge_job
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def get_batch_merge_job(self, api, mocklab, **batch_merge_kwargs):
project_id = mocklab.project_info['id']
merge_request_iid = mocklab.merge_request_info['iid']
merge_request = MergeRequest.fetch_by_iid(project_id, merge_request_iid, api)
params = {
'api': api,
'user': marge.user.User.myself(api),
'project': marge.project.Project.fetch_by_id(project_id, api),
'repo': create_autospec(marge.git.Repo, spec_set=True),
'options': MergeJobOptions.default(),
'merge_requests': [merge_request]
}
params.update(batch_merge_kwargs)
return BatchMergeJob(**params)
示例7: test_web_subject_context_resolve_webregistry_no_reg_returns_subject_wr
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_web_subject_context_resolve_webregistry_no_reg_returns_subject_wr(
web_subject_context, monkeypatch):
"""
- no self.web_registry exists
- returns subject.web_registry attribute
"""
mock_subject = mock.create_autospec(WebDelegatingSubject)
mock_subject.web_registry = 'mockwebregistry'
monkeypatch.setattr(web_subject_context, 'subject', mock_subject)
monkeypatch.setattr(web_subject_context, 'web_registry', None)
result = web_subject_context.resolve_web_registry()
assert result == 'mockwebregistry'
# ------------------------------------------------------------------------------
# WebDelegatingSubject
# ------------------------------------------------------------------------------
示例8: test_web_yosai_get_subject_returns_subject
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_web_yosai_get_subject_returns_subject(
mock_wsc, web_yosai, monkeypatch, mock_web_registry):
@staticmethod
def mock_cwr():
return mock_web_registry
monkeypatch.setattr(WebYosai, 'get_current_webregistry', mock_cwr)
with mock.patch.object(web_yosai.security_manager, 'create_subject') as mock_cs:
mock_ws = mock.create_autospec(WebDelegatingSubject)
mock_ws.web_registry = 'wr'
mock_cs.return_value = mock_ws
result = web_yosai._get_subject()
mock_wsc.assert_called_once_with(yosai=web_yosai,
security_manager=web_yosai.security_manager,
web_registry=mock_web_registry)
mock_cs.assert_called_once_with(subject_context='wsc')
assert result == mock_ws
示例9: test_requires_permission_succeeds
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_requires_permission_succeeds(monkeypatch):
"""
This test verifies that the decorator works as expected.
"""
mock_wds = mock.create_autospec(WebDelegatingSubject)
@staticmethod
def mock_gcs():
return mock_wds
monkeypatch.setattr(WebYosai, 'get_current_subject', mock_gcs)
@WebYosai.requires_permission(['something:anything'])
def do_this():
return 'dothis'
result = do_this()
assert result == 'dothis'
mock_wds.check_permission.assert_called_once_with(['something:anything'], all)
示例10: test_requires_permission_raises_one
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_requires_permission_raises_one(monkeypatch):
@staticmethod
def mock_gcs():
m = mock.create_autospec(WebDelegatingSubject)
m.check_permission.side_effect = ValueError
@staticmethod
def mock_cwr():
m = mock.MagicMock()
m.raise_unauthorized.return_value = Exception
m.raise_forbidden.return_value = Exception
return m
monkeypatch.setattr(WebYosai, 'get_current_subject', mock_gcs)
monkeypatch.setattr(WebYosai, 'get_current_webregistry', mock_cwr)
@WebYosai.requires_permission(['something_anything'])
def do_this():
return 'dothis'
with pytest.raises(Exception):
do_this()
示例11: test_requires_permission_raises_two
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_requires_permission_raises_two(monkeypatch):
mock_wds = mock.create_autospec(WebDelegatingSubject)
mock_wds.check_permission.side_effect = AuthorizationException
@staticmethod
def mock_gcs():
return mock_wds
@staticmethod
def mock_cwr():
m = mock.MagicMock()
m.raise_unauthorized.return_value = Exception
m.raise_forbidden.return_value = Exception
return m
monkeypatch.setattr(WebYosai, 'get_current_subject', mock_gcs)
monkeypatch.setattr(WebYosai, 'get_current_webregistry', mock_cwr)
@WebYosai.requires_permission(['something_anything'])
def do_this():
return 'dothis'
with pytest.raises(Exception):
do_this()
示例12: test_requires_role_succeeds
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_requires_role_succeeds(monkeypatch):
"""
This test verifies that the decorator works as expected.
"""
mock_wds = mock.create_autospec(WebDelegatingSubject)
@staticmethod
def mock_gcs():
return mock_wds
monkeypatch.setattr(WebYosai, 'get_current_subject', mock_gcs)
@WebYosai.requires_role(['role1'])
def do_this():
return 'dothis'
result = do_this()
assert result == 'dothis'
mock_wds.check_role.assert_called_once_with(['role1'], all)
示例13: test_dsc_resolve_identifiers_none_sessionreturns
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_dsc_resolve_identifiers_none_sessionreturns(
subject_context, monkeypatch, mock_session):
"""
unit tested: resolve_identifiers
test case:
- the dsc doesn't have an identifiers attribute set
- neither account nor subject has identifiers
- resolve_session is called to obtain a session and the session's
identifiers obtained
"""
dsc = subject_context
mock_subject = mock.create_autospec(DelegatingSubject)
mock_subject.identifiers = None
monkeypatch.setattr(dsc, 'identifiers', None)
monkeypatch.setattr(dsc, 'subject', mock_subject)
monkeypatch.setattr(mock_session, 'get_internal_attribute',
lambda x: 'identifiers')
result = dsc.resolve_identifiers(mock_session)
assert result == 'identifiers'
示例14: test_dsc_resolve_host_notexists_token
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_dsc_resolve_host_notexists_token(subject_context, monkeypatch):
"""
unit tested: resolve_host
test case:
no host attribute exists, so token's host is returned
"""
dsc = subject_context
mock_token = mock.create_autospec(UsernamePasswordToken)
mock_token.host = 'token_host'
monkeypatch.setattr(dsc, 'host', None)
monkeypatch.setattr(dsc, 'authentication_token', mock_token)
result = dsc.resolve_host('session')
assert result == 'token_host'
示例15: test_get_session_withoutsessionattribute_createsnew
# 需要导入模块: from unittest import mock [as 别名]
# 或者: from unittest.mock import create_autospec [as 别名]
def test_get_session_withoutsessionattribute_createsnew(
delegating_subject, monkeypatch, mock_session):
"""
unit tested: get_session
test case:
no session attribute, creation enabled, results in creation of a new session
attribute
"""
ds = delegating_subject
monkeypatch.setattr(ds, 'session', None)
monkeypatch.setattr(ds, 'create_session_context', lambda: 'sessioncontext')
mock_sm = mock.create_autospec(NativeSecurityManager)
mock_sm.start.return_value = mock_session
monkeypatch.setattr(ds, 'security_manager', mock_sm)
result = ds.get_session()
mock_sm.start.assert_called_once_with('sessioncontext')
assert result == mock_session
assert mock_session.stop_session_callback == ds.session_stopped