本文整理汇总了Python中unittest.mock.patch.dict方法的典型用法代码示例。如果您正苦于以下问题:Python patch.dict方法的具体用法?Python patch.dict怎么用?Python patch.dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.patch
的用法示例。
在下文中一共展示了patch.dict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_succeeds_if_source_is_master
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_succeeds_if_source_is_master(self, mocks_factory):
mocklab, api, job = mocks_factory(
extra_mocklab_opts=dict(merge_request_options={
'source_branch': 'master',
'target_branch': 'production',
}),
)
api.add_transition(
GET(
'/projects/1234/repository/branches/{source}'.format(
source=mocklab.merge_request_info['source_branch'],
),
),
Ok({'commit': {'id': mocklab.rewritten_sha}}),
from_state='initial', to_state='pushed',
)
job.execute()
assert api.state == 'merged'
assert api.notes == []
示例2: test_handles_races_for_merging
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_handles_races_for_merging(self, mocks):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
api.add_transition(
PUT(
'/projects/1234/merge_requests/{iid}/merge'.format(iid=mocklab.merge_request_info['iid']),
dict(sha=rewritten_sha, should_remove_source_branch=True, merge_when_pipeline_succeeds=True),
),
Error(marge.gitlab.NotFound(404, {'message': '404 Branch Not Found'})),
from_state='passed', to_state='someone_else_merged',
)
api.add_merge_request(
dict(mocklab.merge_request_info, state='merged'),
from_state='someone_else_merged',
)
job.execute()
assert api.state == 'someone_else_merged'
assert api.notes == []
示例3: test_calculates_merge_when_pipeline_succeeds_correctly
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_calculates_merge_when_pipeline_succeeds_correctly(
self, mocks, only_allow_merge_if_pipeline_succeeds
):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
project_info = dict(TEST_PROJECT_INFO)
project_info["only_allow_merge_if_pipeline_succeeds"] = only_allow_merge_if_pipeline_succeeds
api.add_project(project_info)
api.add_transition(
PUT(
'/projects/{pid}/merge_requests/{iid}/merge'.format(
iid=mocklab.merge_request_info['iid'],
pid=project_info["id"]
),
dict(
sha=rewritten_sha,
should_remove_source_branch=True,
merge_when_pipeline_succeeds=only_allow_merge_if_pipeline_succeeds
),
),
Ok(True),
to_state='merged',
)
job.execute()
assert api.state == 'merged'
示例4: test_handles_request_becoming_wip_after_push
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_handles_request_becoming_wip_after_push(self, mocks):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
api.add_transition(
PUT(
'/projects/1234/merge_requests/{iid}/merge'.format(iid=mocklab.merge_request_info['iid']),
dict(sha=rewritten_sha, should_remove_source_branch=True, merge_when_pipeline_succeeds=True),
),
Error(marge.gitlab.MethodNotAllowed(405, {'message': '405 Method Not Allowed'})),
from_state='passed', to_state='now_is_wip',
)
api.add_merge_request(
dict(mocklab.merge_request_info, work_in_progress=True),
from_state='now_is_wip',
)
message = 'The request was marked as WIP as I was processing it (maybe a WIP commit?)'
with mocklab.expected_failure(message):
job.execute()
assert api.state == 'now_is_wip'
assert api.notes == ["I couldn't merge this branch: %s" % message]
示例5: test_guesses_git_hook_error_on_merge_refusal
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_guesses_git_hook_error_on_merge_refusal(self, mocks):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
api.add_transition(
PUT(
'/projects/1234/merge_requests/{iid}/merge'.format(iid=mocklab.merge_request_info['iid']),
dict(sha=rewritten_sha, should_remove_source_branch=True, merge_when_pipeline_succeeds=True),
),
Error(marge.gitlab.MethodNotAllowed(405, {'message': '405 Method Not Allowed'})),
from_state='passed', to_state='rejected_by_git_hook',
)
api.add_merge_request(
dict(mocklab.merge_request_info, state='reopened'),
from_state='rejected_by_git_hook',
)
message = (
'GitLab refused to merge this branch. I suspect that a Push Rule or a git-hook '
'is rejecting my commits; maybe my email needs to be white-listed?'
)
with mocklab.expected_failure(message):
job.execute()
assert api.state == 'rejected_by_git_hook'
assert api.notes == ["I couldn't merge this branch: %s" % message]
示例6: test_discovers_if_someone_closed_the_merge_request
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_discovers_if_someone_closed_the_merge_request(self, mocks):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
api.add_transition(
PUT(
'/projects/1234/merge_requests/{iid}/merge'.format(iid=mocklab.merge_request_info['iid']),
dict(sha=rewritten_sha, should_remove_source_branch=True, merge_when_pipeline_succeeds=True),
),
Error(marge.gitlab.MethodNotAllowed(405, {'message': '405 Method Not Allowed'})),
from_state='passed', to_state='oops_someone_closed_it',
)
api.add_merge_request(
dict(mocklab.merge_request_info, state='closed'),
from_state='oops_someone_closed_it',
)
message = 'Someone closed the merge request while I was attempting to merge it.'
with mocklab.expected_failure(message):
job.execute()
assert api.state == 'oops_someone_closed_it'
assert api.notes == ["I couldn't merge this branch: %s" % message]
示例7: test_tells_explicitly_that_gitlab_refused_to_merge
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_tells_explicitly_that_gitlab_refused_to_merge(self, mocks):
mocklab, api, job = mocks
rewritten_sha = mocklab.rewritten_sha
api.add_transition(
PUT(
'/projects/1234/merge_requests/{iid}/merge'.format(iid=mocklab.merge_request_info['iid']),
dict(sha=rewritten_sha, should_remove_source_branch=True, merge_when_pipeline_succeeds=True),
),
Error(marge.gitlab.MethodNotAllowed(405, {'message': '405 Method Not Allowed'})),
from_state='passed', to_state='rejected_for_mysterious_reasons',
)
message = "Gitlab refused to merge this request and I don't know why!"
with mocklab.expected_failure(message):
job.execute()
assert api.state == 'rejected_for_mysterious_reasons'
assert api.notes == ["I couldn't merge this branch: %s" % message]
示例8: test_wont_merge_branches_with_autosquash_if_rewriting
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_wont_merge_branches_with_autosquash_if_rewriting(self, mocks):
mocklab, api, job = mocks
autosquash_merge_request = dict(mocklab.merge_request_info, squash=True)
api.add_merge_request(autosquash_merge_request, from_state='initial')
admin_user = dict(mocklab.user_info, is_admin=True)
api.add_user(admin_user, is_current=True)
if job.opts.requests_commit_tagging:
message = "Sorry, merging requests marked as auto-squash would ruin my commit tagging!"
with mocklab.expected_failure(message):
job.execute()
assert api.state == 'initial'
else:
job.execute()
assert api.state == 'merged'
示例9: test_adaptive_length
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_adaptive_length(self):
with patch.dict('os.environ', {'COLUMNS': '80'}):
out = StringIO()
bar_width = 20
prog_bar = mmcv.ProgressBar(10, bar_width=bar_width, file=out)
time.sleep(1)
reset_string_io(out)
prog_bar.update()
assert len(out.getvalue()) == 66
os.environ['COLUMNS'] = '30'
reset_string_io(out)
prog_bar.update()
assert len(out.getvalue()) == 48
os.environ['COLUMNS'] = '60'
reset_string_io(out)
prog_bar.update()
assert len(out.getvalue()) == 60
示例10: test__get_version_info_loggers_enabled
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test__get_version_info_loggers_enabled(self):
mock_loggers = {
'versionfinder': Mock(),
'pip': Mock(),
'git': Mock()
}
def se_getLogger(lname):
return mock_loggers[lname]
with patch('awslimitchecker.version.logging.getLogger') as mock_logger:
mock_logger.side_effect = se_getLogger
with patch.dict(
'awslimitchecker.version.os.environ',
{'VERSIONCHECK_DEBUG': 'true'}, clear=True
):
with patch('awslimitchecker.version.find_version'):
version._get_version_info()
assert mock_logger.mock_calls == []
assert mock_loggers['versionfinder'].mock_calls == []
assert mock_loggers['pip'].mock_calls == []
assert mock_loggers['git'].mock_calls == []
示例11: test_no_service_key_warn
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_no_service_key_warn(self):
self.cls._service_key_crit = 'cKey'
self.cls._account_alias = 'myAcct'
with patch('%s._event_dict' % pb, autospec=True) as m_ed:
m_ed.return_value = {'event': 'dict', 'details': {}}
with patch('%s._send_event' % pb, autospec=True) as m_send:
self.cls.on_success(duration=12.3)
assert m_ed.mock_calls == [call(self.cls)]
assert m_send.mock_calls == [
call(self.cls, 'cKey', {
'event': 'dict',
'details': {
'duration_seconds': 12.3
},
'event_type': 'resolve',
'description': 'awslimitchecker in myAcct rname found '
'no problems; run completed in '
'12.30 seconds'
})
]
示例12: test__ma_field_to_fr_field_converts_metadata_param_to_description_if_present
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test__ma_field_to_fr_field_converts_metadata_param_to_description_if_present():
@dataclass
class FakeFieldWithDescription(ma.Field):
metadata: dict
expected_description = "test"
fr_field_dict = utils._ma_field_to_fr_field(
FakeFieldWithDescription(metadata={"description": expected_description})
)
assert fr_field_dict["description"] == expected_description
@dataclass
class FakeFieldNoMetaData(ma.Field):
pass
fr_field_dict = utils._ma_field_to_fr_field(FakeFieldNoMetaData())
assert "description" not in fr_field_dict
@dataclass
class FakeFieldNoDescription(ma.Field):
metadata: dict
fr_field_dict = utils._ma_field_to_fr_field(FakeFieldNoDescription(metadata={}))
assert "description" not in fr_field_dict
示例13: test_map_type_calls_type_map_dict_function_for_schema_instance
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_map_type_calls_type_map_dict_function_for_schema_instance():
class MarshmallowSchema(Schema):
test_field: ma.Float
expected_ma_field = MarshmallowSchema()
expected_model_name, expected_operation, expected_namespace = _get_type_mapper_default_params()
schema_type_mapper_mock = Mock()
type_map_mock = dict(utils.type_map)
type_map_mock[Schema] = schema_type_mapper_mock
type_map_patch = patch.object(utils, "type_map", new=type_map_mock)
with type_map_patch:
utils.map_type(expected_ma_field, expected_namespace, expected_model_name, expected_operation)
schema_type_mapper_mock.assert_called_with(
expected_ma_field, expected_namespace, expected_model_name, expected_operation
)
示例14: test_map_type_calls_type_map_dict_function_for_schema_class
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_map_type_calls_type_map_dict_function_for_schema_class():
class InheritedMeta(SchemaMeta):
pass
class MarshmallowSchema(Schema, metaclass=InheritedMeta):
test_field: ma.Float
expected_ma_field = MarshmallowSchema
expected_model_name, expected_operation, expected_namespace = _get_type_mapper_default_params()
schema_type_mapper_mock = Mock()
type_map_mock = dict(utils.type_map)
type_map_mock[Schema] = schema_type_mapper_mock
type_map_patch = patch.object(utils, "type_map", new=type_map_mock)
with type_map_patch:
utils.map_type(expected_ma_field, expected_namespace, expected_model_name, expected_operation)
schema_type_mapper_mock.assert_called_with(
expected_ma_field, expected_namespace, expected_model_name, expected_operation
)
示例15: test_ignore_core
# 需要导入模块: from unittest.mock import patch [as 别名]
# 或者: from unittest.mock.patch import dict [as 别名]
def test_ignore_core():
bot = MagicMock()
event = MagicMock()
_hook = MagicMock()
_hook.plugin.title = "core.optout"
_hook.function_name = "optout"
event.chan = "#test"
conn = event.conn
conn.name = "test"
opt = optout.OptOut("#test", "*", False)
with patch.dict(optout.optout_cache, clear=True, test=[opt]):
res = optout.optout_sieve(bot, event, _hook)
assert res is event