本文整理汇总了Python中mock.mock.patch方法的典型用法代码示例。如果您正苦于以下问题:Python mock.patch方法的具体用法?Python mock.patch怎么用?Python mock.patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.mock
的用法示例。
在下文中一共展示了mock.patch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_normalized_distance
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_normalized_distance(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
self.bot.config.walk_max = 1
self.bot.config.walk_min = 1
sw = StepWalker(self.bot, 0.1, 0.1, precision=0.0)
self.assertGreater(sw.dest_lat, 0)
self.assertGreater(sw.dest_lng, 0)
@mock.patch('random.uniform')
def run_step(mock_random):
mock_random.return_value = 0.0
return sw.step()
stayInPlace = run_step()
self.assertFalse(stayInPlace)
self.assertAlmostEqual(self.bot.position[0], NORMALIZED_LAT_LNG_DISTANCE[0], places=6)
self.assertAlmostEqual(self.bot.position[1], NORMALIZED_LAT_LNG_DISTANCE[1], places=6)
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min
示例2: test_normalized_distance_times_2
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_normalized_distance_times_2(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
self.bot.config.walk_max = 2
self.bot.config.walk_min = 2
sw = StepWalker(self.bot, 0.1, 0.1, precision=0.0)
self.assertTrue(sw.dest_lat > 0)
self.assertTrue(sw.dest_lng > 0)
@mock.patch('random.uniform')
def run_step(mock_random):
mock_random.return_value = 0.0
return sw.step()
stayInPlace = run_step()
self.assertFalse(stayInPlace)
self.assertAlmostEqual(self.bot.position[0], NORMALIZED_LAT_LNG_DISTANCE[0] * 2, places=6)
self.assertAlmostEqual(self.bot.position[1], NORMALIZED_LAT_LNG_DISTANCE[1] * 2, places=6)
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min
示例3: test_small_distance_same_spot
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_small_distance_same_spot(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
self.bot.config.walk_max = 1
self.bot.config.walk_min = 1
sw = StepWalker(self.bot, 0, 0, precision=0.0)
self.assertEqual(sw.dest_lat, 0, 'dest_lat should be 0')
self.assertEqual(sw.dest_lng, 0, 'dest_lng should be 0')
@mock.patch('random.uniform')
def run_step(mock_random):
mock_random.return_value = 0.0
return sw.step()
moveInprecision = run_step()
self.assertTrue(moveInprecision, 'step should return True')
distance = Geodesic.WGS84.Inverse(0.0, 0.0, self.bot.position[0], self.bot.position[1])["s12"]
self.assertTrue(0.0 <= distance <= (sw.precision + sw.epsilon))
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min
示例4: test_stay_put
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_stay_put(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
self.bot.config.walk_max = 4
self.bot.config.walk_min = 2
sw = StepWalker(self.bot, 10, 10, precision=0.0)
self.assertEqual(sw.dest_lat, 10)
self.assertEqual(sw.dest_lng, 10)
@mock.patch('random.uniform')
def run_step(mock_random):
mock_random.return_value = 0.0
return sw.step(speed=0)
finishedWalking = run_step()
self.assertFalse(finishedWalking, 'step should return False')
distance = Geodesic.WGS84.Inverse(0.0, 0.0, self.bot.position[0], self.bot.position[1])["s12"]
self.assertTrue(0.0 <= distance <= (sw.precision + sw.epsilon))
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min
示例5: test_teleport
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_teleport(self):
walk_max = self.bot.config.walk_max
walk_min = self.bot.config.walk_min
self.bot.config.walk_max = 4
self.bot.config.walk_min = 2
sw = StepWalker(self.bot, 10, 10, precision=0.0)
self.assertEqual(sw.dest_lat, 10)
self.assertEqual(sw.dest_lng, 10)
@mock.patch('random.uniform')
def run_step(mock_random):
mock_random.return_value = 0.0
return sw.step(speed=float("inf"))
finishedWalking = run_step()
self.assertTrue(finishedWalking, 'step should return True')
total_distance = Geodesic.WGS84.Inverse(0.0, 0.0, 10, 10)["s12"]
distance = Geodesic.WGS84.Inverse(0.0, 0.0, self.bot.position[0], self.bot.position[1])["s12"]
self.assertTrue(0.0 <= abs(total_distance - distance) <= (sw.precision + sw.epsilon))
self.bot.config.walk_max = walk_max
self.bot.config.walk_min = walk_min
示例6: test_06_execute_task
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_06_execute_task(self):
assertEqual = self.assertEqual
class _TestTask(BaseTask):
identifier = "Test"
description = "foo"
def do(self, params):
assertEqual(params["key"], "value")
return True
with self.assertRaises(ParameterError):
execute_task("Test", {"key": "value"})
with mock.patch.dict(TASK_MODULES, values={"Test": _TestTask}):
ret = execute_task("Test", {"key": "value"})
self.assertTrue(ret)
示例7: test_30_logger_audit_qualname
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_30_logger_audit_qualname(self, capture):
# Check that the default qualname is 'privacyidea.lib.auditmodules.loggeraudit'
current_utc_time = datetime.datetime(2018, 3, 4, 5, 6, 8)
with mock.patch('privacyidea.lib.auditmodules.loggeraudit.datetime') as mock_dt:
mock_dt.utcnow.return_value = current_utc_time
a = LoggerAudit(config={})
a.log({"action": "No PI_AUDIT_LOGGER_QUALNAME given"})
a.finalize_log()
capture.check_present(
('privacyidea.lib.auditmodules.loggeraudit', 'INFO',
'{{"action": "No PI_AUDIT_LOGGER_QUALNAME given", "policies": "", '
'"timestamp": "{timestamp}"}}'.format(timestamp=current_utc_time.isoformat())))
# Now change the qualname to 'pi-audit'
current_utc_time = datetime.datetime(2020, 3, 4, 5, 6, 8)
with mock.patch('privacyidea.lib.auditmodules.loggeraudit.datetime') as mock_dt:
mock_dt.utcnow.return_value = current_utc_time
a = LoggerAudit(config={"PI_AUDIT_LOGGER_QUALNAME": "pi-audit"})
a.log({"action": "PI_AUDIT_LOGGER_QUALNAME given"})
a.finalize_log()
capture.check_present(
('pi-audit', 'INFO',
'{{"action": "PI_AUDIT_LOGGER_QUALNAME given", "policies": "", '
'"timestamp": "{timestamp}"}}'.format(timestamp=current_utc_time.isoformat())))
示例8: test_horovod_rank_compatibility
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_horovod_rank_compatibility(spark_test_ctx):
with mock.patch.dict(os.environ, {'HOROVOD_RANK': '1', 'HOROVOD_SIZE': '3'}, clear=True):
assert (1, 3) == _get_horovod_rank_and_size()
assert _check_rank_and_size_consistent_with_horovod(
petastorm_reader_kwargs={"cur_shard": 1, "shard_count": 3})
assert not _check_rank_and_size_consistent_with_horovod(
petastorm_reader_kwargs={"cur_shard": 1, "shard_count": 2})
assert not _check_rank_and_size_consistent_with_horovod(
petastorm_reader_kwargs={"cur_shard": 0, "shard_count": 3})
with mock.patch.dict(os.environ, {'OMPI_COMM_WORLD_RANK': '1', 'OMPI_COMM_WORLD_SIZE': '3'}, clear=True):
assert (1, 3) == _get_horovod_rank_and_size()
with mock.patch.dict(os.environ, {'PMI_RANK': '1', 'PMI_SIZE': '3'}, clear=True):
assert (1, 3) == _get_horovod_rank_and_size()
with mock.patch.dict(os.environ, {}, clear=True):
assert (None, None) == _get_horovod_rank_and_size()
assert _check_rank_and_size_consistent_with_horovod(
petastorm_reader_kwargs={"cur_shard": 1, "shard_count": 3})
示例9: test_check_parent_cache_dir_url
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_check_parent_cache_dir_url(spark_test_ctx, caplog):
def log_warning_occur():
return 'you should specify a dbfs fuse path' in '\n'.join([r.message for r in caplog.records])
with mock.patch('petastorm.spark.spark_dataset_converter._is_spark_local_mode') as mock_is_local:
mock_is_local.return_value = False
caplog.clear()
_check_parent_cache_dir_url('file:/dbfs/a/b')
assert not log_warning_occur()
caplog.clear()
_check_parent_cache_dir_url('file:/a/b')
assert log_warning_occur()
mock_is_local.return_value = True
caplog.clear()
_check_parent_cache_dir_url('file:/dbfs/a/b')
assert not log_warning_occur()
caplog.clear()
_check_parent_cache_dir_url('file:/a/b')
assert not log_warning_occur()
示例10: _generate_plan_and_rsync_lists
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def _generate_plan_and_rsync_lists(migration, plan_name):
mock_shard_allocation = _get_expected_yml(plan_name, 'mock_shard_allocation_pre_migration.yml')
mock_func = get_shard_allocation_func(mock_shard_allocation)
db_info = [
(allocation['_id'], 10, {'a': 10}, sorted(allocation['by_range']), ShardAllocationDoc.wrap(allocation))
for allocation in mock_shard_allocation
]
with patch('couchdb_cluster_admin.suggest_shard_allocation.get_db_info', return_value=db_info):
generate_shard_plan(migration)
with patch('couchdb_cluster_admin.file_plan.get_shard_allocation', mock_func):
# this also get's called in generate_rsync_lists but we want the result to test against
migration_file_configs = get_migration_file_configs(migration)
generate_rsync_lists(migration)
return migration_file_configs
示例11: test_add_node_stopped_metric
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_add_node_stopped_metric(node_scenario):
node_scenario.schema = {
"actions": [
{
"stop": {}
},
],
}
mock_item = MagicMock()
mock_item.uid = '1'
mock_item.name = 'node1'
items = [mock_item]
with mock.patch('powerfulseal.metriccollectors.StdoutCollector.add_node_stopped_metric') \
as metric_function:
metric_function.assert_not_called()
node_scenario.act(items)
metric_function.assert_called_once_with(mock_item)
示例12: test_add_node_stop_failed_metric
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_add_node_stop_failed_metric(node_scenario):
node_scenario.schema = {
"actions": [
{
"stop": {}
},
],
}
mock_item = MagicMock()
mock_item.uid = '1'
mock_item.name = 'node1'
items = [mock_item]
method = getattr(node_scenario.driver, "stop")
method.side_effect = Exception("something bad")
with mock.patch('powerfulseal.metriccollectors.StdoutCollector.add_node_stop_failed_metric') \
as metric_function:
metric_function.assert_not_called()
node_scenario.act(items)
metric_function.assert_called_once_with(mock_item)
示例13: test_add_probability_filter_passed_no_nodes_metric
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_add_probability_filter_passed_no_nodes_metric(noop_scenario):
"""
Ensures that add_probability_filter_passed_no_nodes_metric is called when
the filter decides to pass no nodes based on a probability
"""
assert noop_scenario.name == "test scenario"
random.seed(6) # make the tests deterministic
candidates = [make_dummy_object()]
with mock.patch('powerfulseal.metriccollectors.StdoutCollector.add_probability_filter_passed_no_nodes_filter') \
as metric_function:
# Ensure metric is not added when nodes are all passed
criterion = {"probabilityPassAll": 1}
noop_scenario.filter_probability(candidates, criterion)
metric_function.assert_not_called()
# Ensure metric is added when nodes are not passed
criterion = {"probabilityPassAll": 0.00000001}
noop_scenario.filter_probability(candidates, criterion)
metric_function.assert_called()
示例14: test_add_node_stopped_metric
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_add_node_stopped_metric(datadog_node_scenario):
datadog_node_scenario.schema = {
"actions": [
{
"stop": {}
},
],
}
mock_item = MagicMock()
mock_item.uid = '1'
mock_item.name = 'node1'
items = [mock_item]
with mock.patch('powerfulseal.metriccollectors.DatadogCollector.add_node_stopped_metric') \
as metric_function:
metric_function.assert_not_called()
datadog_node_scenario.act(items)
metric_function.assert_called_once_with(mock_item)
示例15: test_add_probability_filter_passed_no_nodes_metric
# 需要导入模块: from mock import mock [as 别名]
# 或者: from mock.mock import patch [as 别名]
def test_add_probability_filter_passed_no_nodes_metric(datadog_noop_scenario):
"""
Ensures that add_probability_filter_passed_no_nodes_metric is called when
the filter decides to pass no nodes based on a probability
"""
assert datadog_noop_scenario.name == "test scenario"
random.seed(6) # make the tests deterministic
candidates = [make_dummy_object()]
with mock.patch('powerfulseal.metriccollectors.DatadogCollector.add_probability_filter_passed_no_nodes_filter') \
as metric_function:
# Ensure metric is not added when nodes are all passed
criterion = {"probabilityPassAll": 1}
datadog_noop_scenario.filter_probability(candidates, criterion)
metric_function.assert_not_called()
# Ensure metric is added when nodes are not passed
criterion = {"probabilityPassAll": 0.00000001}
datadog_noop_scenario.filter_probability(candidates, criterion)
metric_function.assert_called()