本文整理汇总了Python中mock.Mock.__enter__方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.__enter__方法的具体用法?Python Mock.__enter__怎么用?Python Mock.__enter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.__enter__方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_api_pod_with_client_certs
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_get_api_pod_with_client_certs(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
self.driver.client_certificate = "cert.pem"
self.driver.client_key = "key.pem"
self.driver.certificate_authority = "ca.pem"
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
self.driver._get_api_pod()
# Assert correct data in calls.
m_session_obj.get.assert_called_once_with(
api_root + 'namespaces/a/pods/pod-1',
verify="ca.pem", cert=("cert.pem", "key.pem"))
m_json_load.assert_called_once_with(pod1)
示例2: test_inject_parent_settings
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_inject_parent_settings(self, _pulp_conf, _read_config, _open):
host = 'test-host'
cert_path = '/path/cert'
_pulp_conf.get.return_value = host
_read_config.return_value.main = Mock(node_certificate=cert_path)
fp = Mock()
fp.__enter__ = Mock(return_value=fp)
fp.__exit__ = Mock()
_open.return_value = fp
# test
options = {}
NodeProfiler._inject_parent_settings(options)
# validation
_pulp_conf.get.assert_called_once_with('server', 'server_name')
_read_config.assert_called_once_with()
_open.assert_called_once_with(cert_path)
settings = options[constants.PARENT_SETTINGS]
self.assertEqual(settings[constants.HOST], host)
self.assertEqual(settings[constants.PORT], 443)
self.assertEqual(
settings[constants.NODE_CERTIFICATE],
_open.return_value.read.return_value)
示例3: test_get_api_pod
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_get_api_pod(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
# Set up mock objects
self.driver.auth_token = 'TOKEN'
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
self.driver._get_api_pod()
# Assert correct data in calls.
m_session_obj.headers.update.assert_called_once_with(
{'Authorization': 'Bearer ' + 'TOKEN'})
m_session_obj.get.assert_called_once_with(
api_root + 'namespaces/a/pods/pod-1',
verify=False)
m_json_load.assert_called_once_with(pod1)
示例4: test_catter
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_catter():
fs = Filesystem(DB(':memory:'))
fs.root().add_file('zeroes', [test_asset_ids], 130)
asset = Mock()
asset.__enter__ = Mock(return_value=asset)
asset.__exit__ = Mock(return_value=False)
asset.status = Mock(return_value=proto.AssetStatus(size=1024 * 1024, status=proto.SUCCESS))
asset.__iter__ = Mock(return_value=('\x00' * (128 * 1024) for _ in range(8)))
bithorde = Mock()
bithorde.open = Mock(return_value=asset)
cat = Cat(fs, bithorde)
sha = sha1()
for chunk in cat(P('zeroes')):
sha.update(chunk)
assert_equals(sha.hexdigest(), '3b71f43ff30f4b15b5cd85dd9e95ebc7e84eb5a3')
with assert_raises(FNotFound):
for chunk in cat(P('NON-EXISTING')):
pass
asset.status = Mock(return_value=proto.AssetStatus(status=proto.NOTFOUND))
with assert_raises(NotFoundError):
for chunk in cat(P('zeroes')):
pass
示例5: test_DBExporter
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_DBExporter():
path = u'apa/movie'
xt = u'tree:tiger:ASDASDSADASDASDSADASDASDSADASDASDSADASD'
db = DB(':memory:')
db.update(Object(u"dir:apa", {
u'directory': ValueSet(u"dir:/apa"),
}))
db.update(Object('some_file', {
u'directory': ValueSet(u"dir:apa/movie"),
u'xt': ValueSet(xt),
}))
asset = Mock()
asset.__enter__ = Mock(return_value=asset)
asset.__exit__ = Mock(return_value=False)
asset.status = Mock(
return_value=proto.AssetStatus(status=proto.SUCCESS))
bithorde = Mock()
bithorde.open = Mock(return_value=asset)
bithorde.pool = Mock(return_value=itertools)
writer = Mock()
magnet = u'magnet:?xt=urn:' + xt
exp = links.DBExporter(db, bithorde, writer)
exp.export(False)
writer.assert_called_once_with(path, magnet)
writer.reset_mock()
exp.export(False)
writer.assert_not_called()
exp.export(True)
writer.assert_called_once_with(path, magnet)
示例6: test_run_interactive
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_run_interactive(capfd):
"""Ensure the calls to run a command on a list of hosts interactively."""
runner = Bladerunner({"threads": 14})
fake_result = Mock()
fake_result.result = Mock(return_value="fake results")
mock_con = Mock()
runner.interactive_hosts = {"fake host": mock_con}
fake_thread = Mock()
fake_thread.__enter__ = fake_context
fake_thread.__exit__ = fake_context
fake_thread.submit = Mock(return_value=fake_result)
threadpool_mock = patch.object(
base,
"ThreadPoolExecutor",
return_value=fake_thread,
)
with patch.object(runner, "setup_interactive") as mock_setup:
with threadpool_mock as mock_threadpool:
runner.run_interactive("fake cmd", "fake host")
mock_setup.assert_called_once_with("fake host")
mock_threadpool.assert_called_once_with(max_workers=14)
stdout, stderr = capfd.readouterr()
assert stdout == "fake host: fake results\n"
assert stderr == ""
示例7: test_run_interactive_returns
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_run_interactive_returns():
"""Confirm the dict return when print_results is False."""
runner = Bladerunner({"threads": 17})
fake_result = Mock()
fake_result.result = Mock(return_value="some result")
mock_con = Mock()
runner.interactive_hosts = {"host": mock_con}
fake_thread = Mock()
fake_thread.__enter__ = fake_context
fake_thread.__exit__ = fake_context
fake_thread.submit = Mock(return_value=fake_result)
threadpool_mock = patch.object(
base,
"ThreadPoolExecutor",
return_value=fake_thread,
)
with patch.object(runner, "setup_interactive") as mock_setup:
with threadpool_mock as mock_threadpool:
results = runner.run_interactive("ok", "host", print_results=False)
mock_setup.assert_called_once_with("host")
mock_threadpool.assert_called_once_with(max_workers=17)
assert results == {"host": "some result"}
示例8: test_get_api_pod_parse_error
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_get_api_pod_parse_error(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
# Set up mock objects
self.driver.auth_token = 'TOKEN'
m_json_load.side_effect = TypeError
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
assert_raises(ApplyProfileError, self.driver._get_api_pod)
示例9: create_file_context_manager
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def create_file_context_manager(*args, **kwargs):
path = args[0]
file_mock = Mock()
file_mock.read = Mock(return_value=file_map[path])
cm = Mock()
cm.__enter__ = Mock(return_value=file_mock)
cm.__exit__ = Mock()
return cm
示例10: test_write_data
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_write_data(self, m_open):
mock_fp = Mock()
mock_fp.__enter__ = lambda x: mock_fp
mock_fp.__exit__ = lambda w, x, y, z: None
m_open.return_value = mock_fp
c.write_to_file('data', 'outfile')
m_open.assert_called_once_with('outfile', 'w')
mock_fp.write.assert_called_once_with('data')
示例11: global_reader_bis
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def global_reader_bis(path=''):
reader = Mock(spec=RasterReader)
reader.shape = (361, 720)
reader.affine = Affine(-180., 0.5, 0.0, 90., 0.0, -0.5)
context = Mock()
context.__enter__ = Mock(return_value=reader)
context.__exit__ = Mock(return_value=False)
return context
示例12: testContextManagerMocking
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def testContextManagerMocking(self):
mock = Mock()
mock.__enter__ = Mock()
mock.__exit__ = Mock()
mock.__exit__.return_value = False
with mock as m:
self.assertEqual(m, mock.__enter__.return_value)
mock.__enter__.assert_called_with()
mock.__exit__.assert_called_with(None, None, None)
示例13: test_update_unlocked
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def test_update_unlocked(self):
''' til; How to mock a context generator '''
job = {'app_id': 6380}
self.master.apphandlerlocks = {6380: None}
nblc = Mock()
nblc.__enter__ = Mock(return_value=False)
nblc.__exit__ = Mock()
nblc_mock = Mock(return_value=nblc)
with patch('master.NBLockContext', nblc_mock, create=True):
self.assertFalse(self.master.api_update(job, Mock()))
示例14: setUp
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def setUp(self):
AddPatch(self, patch.dict('os.environ', {'NACL_SDK_ROOT': '/sdk/root'}))
AddPatch(self, patch('naclports.util.GetPlatform',
Mock(return_value='linux')))
AddPatch(self, patch('naclports.util.GetInstallRoot',
Mock(return_value='/package/install/path')))
AddPatch(self, patch('naclports.util.GetSDKRoot',
Mock(return_value='/sdk/root')))
mock_lock = Mock()
mock_lock.__enter__ = lambda s: s
mock_lock.__exit__ = Mock(return_value=False)
AddPatch(self, patch('naclports.util.InstallLock',
Mock(return_value=mock_lock)))
mock_lock = Mock()
mock_lock.__enter__ = lambda s: s
mock_lock.__exit__ = Mock(return_value=False)
AddPatch(self, patch('naclports.util.BuildLock',
Mock(return_value=mock_lock)))
示例15: setUp
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import __enter__ [as 别名]
def setUp(self):
add_patch(self, patch.dict('os.environ', {'NACL_SDK_ROOT': '/sdk/root'}))
add_patch(self, patch('webports.util.get_platform',
Mock(return_value='linux')))
add_patch(self, patch('webports.util.get_install_root',
Mock(return_value='/package/install/path')))
add_patch(self, patch('webports.util.get_sdk_root',
Mock(return_value='/sdk/root')))
mock_lock = Mock()
mock_lock.__enter__ = lambda s: s
mock_lock.__exit__ = Mock(return_value=False)
add_patch(self, patch('webports.util.InstallLock',
Mock(return_value=mock_lock)))
mock_lock = Mock()
mock_lock.__enter__ = lambda s: s
mock_lock.__exit__ = Mock(return_value=False)
add_patch(self, patch('webports.util.BuildLock',
Mock(return_value=mock_lock)))