本文整理汇总了Python中mock.mock_open函数的典型用法代码示例。如果您正苦于以下问题:Python mock_open函数的具体用法?Python mock_open怎么用?Python mock_open使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mock_open函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_fetch
def test_fetch(self):
"""
test for the "fetch" method
we verify that:
* Input sanitation is performed properly
* The request is made to the proper link
* The file extension is appended if not in the link
(jpg is hardcoded)
* The request object returns a valid iterable
* Weird image titles are handled properly
"""
imgobject = imgurpython.helpers.GalleryImage(link=None,
title="Neat mountains",
description="or not",
width=10000, height=10000)
# verify that we can only send imgur objects here
with self.assertRaises(ValueError):
self.fetcher.fetch(None, "filename")
with self.assertRaises(ValueError):
self.fetcher.fetch("badtype", "filename")
# verify that we send a proper filename here
with self.assertRaises(ValueError):
self.fetcher.fetch(imgobject, None)
with self.assertRaises(ValueError):
self.fetcher.fetch(imgobject, 10)
# check that the request is properly formatted
with patch("bg_daemon.fetchers.imgurfetcher.requests.get") as \
mock_method:
mock_method.return_value = None
imgobject.link = None
# we check that the link is properly checked when building
# the request
with self.assertRaises(ValueError):
self.fetcher.fetch(imgobject, "filename.jpg")
mock_method.return_value = self.fake_response
open_mock = mock_open()
with patch("bg_daemon.fetchers.imgurfetcher.open", open_mock,
create=True):
# Assert that we actually try to write the file
self.fetcher.fetch(imgobject, "filename.jpg")
open_mock.assert_called_once_with("filename.jpg", "wb")
open_mock = mock_open()
with patch("bg_daemon.fetchers.imgurfetcher.open", open_mock,
create=True):
# Assert that it tries to infer a different extension if
# not provided
imgobject.link = "filename.gif"
self.fetcher.fetch(imgobject, "filename")
open_mock.assert_called_once_with("filename.gif", "wb")
示例2: test_upload_certificate_md5
def test_upload_certificate_md5(self, mock_makedir, mock_chmod,
mock_exists):
# wrong file name
rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/certificates/test.bla',
data='TestTest')
self.assertEqual(400, rv.status_code)
mock_exists.return_value = True
m = mock.mock_open()
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/certificates/test.pem',
data='TestTest')
self.assertEqual(200, rv.status_code)
self.assertEqual(OK, json.loads(rv.data.decode('utf-8')))
handle = m()
handle.write.assert_called_once_with(six.b('TestTest'))
mock_chmod.assert_called_once_with(handle.fileno(), 0o600)
mock_exists.return_value = False
m = mock.mock_open()
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION +
'/listeners/123/certificates/test.pem',
data='TestTest')
self.assertEqual(200, rv.status_code)
self.assertEqual(OK, json.loads(rv.data.decode('utf-8')))
handle = m()
handle.write.assert_called_once_with(six.b('TestTest'))
mock_makedir.assert_called_once_with('/var/lib/octavia/certs/123')
示例3: eq_ne_test
def eq_ne_test(self,_,tfile1,path1,tfile2,path2,eq_flag):
with patch("__builtin__.open", mock_open(read_data=tfile1)) as m:
ltor1 = LocalTorrent(path1)
with patch("__builtin__.open", mock_open(read_data=tfile2)) as m:
ltor2 = LocalTorrent(path2)
self.assertEqual(ltor1.__eq__(ltor2), eq_flag)
self.assertEqual(ltor1.__ne__(ltor2), not eq_flag)
示例4: test_ignore_list
def test_ignore_list():
"""Test that `ignore`d errors are not reported in the API."""
function_to_check = textwrap.dedent('''
def function_with_bad_docstring(foo):
""" does spacinwithout a period in the end
no blank line after one-liner is bad. Also this - """
return foo
''')
expected_error_codes = {'D100', 'D400', 'D401', 'D205', 'D209', 'D210',
'D403'}
mock_open = mock.mock_open(read_data=function_to_check)
from pydocstyle import checker
with mock.patch.object(
checker.tk, 'open', mock_open, create=True):
errors = tuple(checker.check(['filepath']))
error_codes = {error.code for error in errors}
assert error_codes == expected_error_codes
# We need to recreate the mock, otherwise the read file is empty
mock_open = mock.mock_open(read_data=function_to_check)
with mock.patch.object(
checker.tk, 'open', mock_open, create=True):
ignored = {'D100', 'D202', 'D213'}
errors = tuple(checker.check(['filepath'], ignore=ignored))
error_codes = {error.code for error in errors}
assert error_codes == expected_error_codes - ignored
示例5: test_write_queued_seeds
def test_write_queued_seeds(self):
mock_file = mock_open()
seeds = set(['123_456', '234_567'])
with patch.object(builtins, 'open', mock_open()) as mock_file:
qsub_store_corsika_data.write_queued_seeds(seeds)
mock_file.assert_called_once_with(qsub_store_corsika_data.QUEUED_SEEDS, 'w')
mock_file().write.assert_called_once_with('\n'.join(seeds))
示例6: testCalculateDeviceMd5Sums_singlePath_linkerWarning
def testCalculateDeviceMd5Sums_singlePath_linkerWarning(self):
# See crbug/479966
test_path = '/storage/emulated/legacy/test/file.dat'
device = mock.NonCallableMock()
device.adb = mock.NonCallableMock()
device.adb.Push = mock.Mock()
device_md5sum_output = [
'WARNING: linker: /data/local/tmp/md5sum/md5sum_bin: '
'unused DT entry: type 0x1d arg 0x15db',
'0123456789abcdeffedcba9876543210 '
'/storage/emulated/legacy/test/file.dat',
]
device.RunShellCommand = mock.Mock(return_value=device_md5sum_output)
mock_temp_file = mock.mock_open()
mock_temp_file.return_value.name = '/tmp/test/script/file.sh'
mock_device_temp_file = mock.mock_open()
mock_device_temp_file.return_value.name = (
'/data/local/tmp/test/script/file.sh')
with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), (
mock.patch('pylib.utils.device_temp_file.DeviceTempFile',
new=mock_device_temp_file)):
out = md5sum.CalculateDeviceMd5Sums(test_path, device)
self.assertEquals(1, len(out))
self.assertTrue('/storage/emulated/legacy/test/file.dat' in out)
self.assertEquals('0123456789abcdeffedcba9876543210',
out['/storage/emulated/legacy/test/file.dat'])
device.adb.Push.assert_called_once_with(
'/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh')
device.RunShellCommand.assert_called_once_with(
['sh', '/data/local/tmp/test/script/file.sh'])
示例7: test_tags_are_shown_in_post
def test_tags_are_shown_in_post(self, _, __):
"""
Test that tags are actually get to the output.
"""
# since we're interested in rendered page, let's register
# a fake converter for that purpose
self.app.add_converter(FakeConverter())
data = textwrap.dedent('''\
---
tags: [tag1, tag2]
---
some text''')
open_fn = 'holocron.content.open'
with mock.patch(open_fn, mock.mock_open(read_data=data), create=True):
post = Post('2015/05/23/filename.fake', self.app)
self._get_content([post])
with mock.patch(open_fn, mock.mock_open(), create=True) as mopen:
post.build()
content = mopen().write.call_args[0][0]
err = 'Could not find link for #tag1.'
self.assertIn('<a href="/mypath/tags/tag1/">#tag1</a>', content, err)
err = 'Could not find link for #tag2.'
self.assertIn('<a href="/mypath/tags/tag2/">#tag2</a>', content, err)
示例8: testCalculateDeviceMd5Sums_singlePath
def testCalculateDeviceMd5Sums_singlePath(self):
test_path = '/storage/emulated/legacy/test/file.dat'
device = mock.NonCallableMock()
device.adb = mock.NonCallableMock()
device.adb.Push = mock.Mock()
device_md5sum_output = [
'0123456789abcdeffedcba9876543210 '
'/storage/emulated/legacy/test/file.dat',
]
device.RunShellCommand = mock.Mock(return_value=device_md5sum_output)
mock_temp_file = mock.mock_open()
mock_temp_file.return_value.name = '/tmp/test/script/file.sh'
mock_device_temp_file = mock.mock_open()
mock_device_temp_file.return_value.name = (
'/data/local/tmp/test/script/file.sh')
with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), (
mock.patch('pylib.utils.device_temp_file.DeviceTempFile',
new=mock_device_temp_file)):
out = md5sum.CalculateDeviceMd5Sums(test_path, device)
self.assertEquals(1, len(out))
self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash)
self.assertEquals('/storage/emulated/legacy/test/file.dat', out[0].path)
device.adb.Push.assert_called_once_with(
'/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh')
device.RunShellCommand.assert_called_once_with(
['sh', '/data/local/tmp/test/script/file.sh'])
示例9: test_upload_keepalived_config
def test_upload_keepalived_config(self, mock_remove, mock_subprocess,
mock_rename, mock_makedirs, mock_exists):
mock_exists.return_value = True
m = mock.mock_open()
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + '/vrrp/upload',
data='test')
self.assertEqual(200, rv.status_code)
mock_exists.return_value = False
m = mock.mock_open()
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + '/vrrp/upload',
data='test')
self.assertEqual(200, rv.status_code)
mock_subprocess.side_effect = subprocess.CalledProcessError(1,
'blah!')
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + '/vrrp/upload',
data='test')
self.assertEqual(500, rv.status_code)
mock_subprocess.side_effect = [True,
subprocess.CalledProcessError(1,
'blah!')]
with mock.patch.object(builtins, 'open', m):
rv = self.app.put('/' + api_server.VERSION + '/vrrp/upload',
data='test')
self.assertEqual(500, rv.status_code)
示例10: open_se
def open_se(path, mode='r', create=True):
if path.endswith('cpuacct/mesos/%s/cpuacct.usage' % task_id):
fixture = self.getFixture('cpuacct.usage')
m = mock_open(read_data=fixture.getvalue())
m.__enter__.return_value = fixture
return m
elif path.endswith('cpuacct/mesos/%s/cpuacct.stat' % task_id):
fixture = self.getFixture('cpuacct.stat')
m = mock_open(read_data=fixture.getvalue())
m.__enter__.return_value = fixture
return m
elif path.endswith('cpu/mesos/%s/cpu.stat' % task_id):
fixture = self.getFixture('cpu.stat')
m = mock_open(read_data=fixture.getvalue())
m.__enter__.return_value = fixture
return m
elif path.endswith('memory/mesos/%s/memory.stat' % task_id):
fixture = self.getFixture('memory.stat')
m = mock_open(read_data=fixture.getvalue())
m.__enter__.return_value = fixture
return m
else:
patch_open.stop()
o = open(path, mode, create)
patch_open.start()
return o
示例11: test_should_upgrade_assessment_items
def test_should_upgrade_assessment_items(self):
# if assessmentitems.version doesn't exist, then return
# true
with patch("os.path.exists") as exists_method:
exists_method.return_value = False
self.assertTrue(
mod.should_upgrade_assessment_items(),
"We told our user not to download assessment items even if they don't have it! Madness!"
)
# if the version in assessmentitems.version is less
# than our current version, then we should upgrade (return True)
assessment_items_mock_version = "0.9.0"
with patch('%s.open' % mod.__name__, mock_open(read_data=assessment_items_mock_version), create=True) as mopen:
self.assertTrue(
mod.should_upgrade_assessment_items(),
"We should've told our users to upgrade assessment items, as they have an old version!"
)
# we should've also opened the file at least
mopen.assert_called_once_with(contentload_settings.KHAN_ASSESSMENT_ITEM_VERSION_PATH)
# if the version in assessment items is equal to our current
# version, then don't upgrade
assessment_items_mock_version = version.SHORTVERSION
with patch('%s.open' % mod.__name__, mock_open(read_data=assessment_items_mock_version), create=True) as mopen:
self.assertFalse(
mod.should_upgrade_assessment_items(),
"We should not tell the user to upgrade when we have the same version as assessment items!"
)
# we should've also opened the file atleast
mopen.assert_called_once_with(contentload_settings.KHAN_ASSESSMENT_ITEM_VERSION_PATH)
示例12: test_load_task
def test_load_task(self, mock_open):
input_task = "{'ab': {{test}}}"
input_args = "{'test': 2}"
# NOTE(boris-42): Such order of files is because we are reading
# file with args before file with template.
mock_open.side_effect = [
mock.mock_open(read_data="{'test': 1}").return_value,
mock.mock_open(read_data=input_task).return_value
]
result = self.task._load_task("in_task", task_args_file="in_args_path")
self.assertEqual(result, {"ab": 1})
mock_open.side_effect = [
mock.mock_open(read_data=input_task).return_value
]
result = self.task._load_task("in_task", task_args=input_args)
self.assertEqual(result, {"ab": 2})
mock_open.side_effect = [
mock.mock_open(read_data="{'test': 1}").return_value,
mock.mock_open(read_data=input_task).return_value
]
result = self.task._load_task("in_task", task_args=input_args,
task_args_file="any_file")
self.assertEqual(result, {"ab": 2})
示例13: runErnwin
def runErnwin(self, command):
self.exitCode = None
open_stats = mock_open()
open_main = mock_open()
try:
with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
with patch('sys.stderr', new_callable=StringIO) as mock_stderr:
with patch('fess.builder.samplingStatisticsNew2.open', open_stats, create=True):
with patch('fess.scripts.ernwin_new.open', open_main, create=True):
try:
ernwin.main(self.parser.parse_args(command.split()[2:]))
except SystemExit as e:
self.exitCode = e.code
if self.exitCode:
print("ERNWIN exited with non-zero exit code {}".format(self.exitCode), file=sys.stderr)
print("STDERR WAS:", file=sys.stderr)
print(mock_stderr.getvalue(), file=sys.stderr)
return open_main, open_stats, mock_stdout, mock_stderr
except BaseException as e:
print('Exception {}:"{}" caught'.format(type(e), e), file=sys.stderr)
print("STDERR WAS:", file=sys.stderr)
print(mock_stderr.getvalue(), file=sys.stderr)
print("STDOUT WAS:", file=sys.stderr)
print(mock_stdout.getvalue(), file=sys.stderr)
raise
示例14: test_setup_challenge_cert
def test_setup_challenge_cert(self):
# This is a helper function that can be used for handling
# open context managers more elegantly. It avoids dealing with
# __enter__ and __exit__ calls.
# http://www.voidspace.org.uk/python/mock/helpers.html#mock.mock_open
mock_open, mock_safe_open = mock.mock_open(), mock.mock_open()
response = challenges.TLSSNI01Response()
achall = mock.MagicMock()
key = test_util.load_pyopenssl_private_key("rsa512_key.pem")
achall.response_and_validation.return_value = (
response, (test_util.load_cert("cert.pem"), key))
with mock.patch("certbot.plugins.common.open",
mock_open, create=True):
with mock.patch("certbot.plugins.common.util.safe_open",
mock_safe_open):
# pylint: disable=protected-access
self.assertEqual(response, self.sni._setup_challenge_cert(
achall, "randomS1"))
# pylint: disable=no-member
mock_open.assert_called_once_with(self.sni.get_cert_path(achall), "wb")
mock_open.return_value.write.assert_called_once_with(
test_util.load_vector("cert.pem"))
mock_safe_open.assert_called_once_with(
self.sni.get_key_path(achall), "wb", chmod=0o400)
mock_safe_open.return_value.write.assert_called_once_with(
OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key))
示例15: test_directoryLayout
def test_directoryLayout(self):
m = mock_open()
dl = self.zp.destdir
with patch('__builtin__.open', mock_open(read_data='fake data'), create=True) as m:
dl.write()
# Write File Handle
wfh = m.return_value.__enter__.return_value
self.results = wfh.write.call_args_list
# Get the specific file calls that make up our writes
top = defaults.get('prefix', os.getcwd())
name = DEFAULT_NAME
file_calls = [x
for x in m.mock_calls
if repr(x)[6:].startswith("%s/%s" % (top, name))]
self.assertEqual(
self.results, [
call(
"__import__('pkg_resources').declare_namespace(__name__)\n"),
call(
"__import__('pkg_resources').declare_namespace(__name__)\n"),
call(
"__import__('pkg_resources').declare_namespace(__name__)\n"),
call('graft ZenPacks\n')])
self.assertEqual(
file_calls, [
call('%s/%s/a/__init__.py' % (top, name), 'w'),
call('%s/%s/a/b/__init__.py' % (top, name), 'w'),
call('%s/%s/a/b/c/__init__.py' % (top, name), 'w'),
call('%s/%s/MANIFEST.in' % (top, name), 'w')])