本文整理汇总了Python中mock.patch.dict函数的典型用法代码示例。如果您正苦于以下问题:Python dict函数的具体用法?Python dict怎么用?Python dict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dict函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_issue_6030_deprecated_never_download
def test_issue_6030_deprecated_never_download(self):
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
virtualenv_mod.create(
'/tmp/foo', never_download=True
)
mock.assert_called_once_with(
'virtualenv --never-download /tmp/foo',
runas=None
)
with TestsLoggingHandler() as handler:
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
# Let's fake a higher virtualenv version
virtualenv_mock = MagicMock()
virtualenv_mock.__version__ = '1.10rc1'
with patch.dict(virtualenv_mod.__salt__, {'cmd.run_all': mock}):
with patch.dict('sys.modules',
{'virtualenv': virtualenv_mock}):
virtualenv_mod.create(
'/tmp/foo', never_download=True
)
mock.assert_called_once_with('virtualenv /tmp/foo',
runas=None)
# Are we logging the deprecation information?
self.assertIn(
'INFO:The virtualenv \'--never-download\' option has been '
'deprecated in virtualenv(>=1.10), as such, the '
'\'never_download\' option to `virtualenv.create()` has '
'also been deprecated and it\'s not necessary anymore.',
handler.messages
)
示例2: test_exc_str
def test_exc_str():
try:
raise Exception("my bad")
except Exception as e:
estr = exc_str(e)
assert_re_in("my bad \[test_dochelpers.py:test_exc_str:...\]", estr)
def f():
def f2():
raise Exception("my bad again")
f2()
try:
f()
except Exception as e:
# default one:
estr2 = exc_str(e, 2)
estr1 = exc_str(e, 1)
# and we can control it via environ by default
with patch.dict('os.environ', {'DATALAD_EXC_STR_TBLIMIT': '3'}):
estr3 = exc_str(e)
with patch.dict('os.environ', {}, clear=True):
estr_ = exc_str()
assert_re_in("my bad again \[test_dochelpers.py:test_exc_str:...,test_dochelpers.py:f:...,test_dochelpers.py:f2:...\]", estr3)
assert_re_in("my bad again \[test_dochelpers.py:f:...,test_dochelpers.py:f2:...\]", estr2)
assert_re_in("my bad again \[test_dochelpers.py:f2:...\]", estr1)
assert_equal(estr_, estr1)
try:
raise NotImplementedError
except Exception as e:
assert_re_in("NotImplementedError\(\) \[test_dochelpers.py:test_exc_str:...\]", exc_str(e))
示例3: test_color_enabled
def test_color_enabled():
# In the absence of NO_COLOR, follow ui.color, or ui.is_interactive if 'auto'
with patch.dict(os.environ), \
patch('datalad.support.ansi_colors.ui'):
os.environ.pop('NO_COLOR', None)
for is_interactive in (True, False):
colors.ui.is_interactive = is_interactive
with patch_config({'datalad.ui.color': 'off'}):
assert_equal(colors.color_enabled(), False)
with patch_config({'datalad.ui.color': 'on'}):
assert_equal(colors.color_enabled(), True)
with patch_config({'datalad.ui.color': 'auto'}):
assert_equal(colors.color_enabled(), is_interactive)
# In the presence of NO_COLOR, default to disable, unless ui.color is "on"
# The value of NO_COLOR should have no effect, so try true-ish and false-ish values
for NO_COLOR in ("", "1", "0"):
with patch.dict(os.environ, {'NO_COLOR': NO_COLOR}), \
patch('datalad.support.ansi_colors.ui'):
for is_interactive in (True, False):
colors.ui.is_interactive = is_interactive
with patch_config({'datalad.ui.color': 'on'}):
assert_equal(colors.color_enabled(), True)
for ui_color in ('off', 'auto'):
with patch_config({'datalad.ui.color': ui_color}):
assert_equal(colors.color_enabled(), False)
示例4: test_uninstall_timeout_argument_in_resulting_command
def test_uninstall_timeout_argument_in_resulting_command(self):
# Passing an int
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall('pep8', timeout=10)
mock.assert_called_once_with(
'pip uninstall -y --timeout=10 pep8',
runas=None,
cwd=None
)
# Passing an int as a string
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.uninstall('pep8', timeout='10')
mock.assert_called_once_with(
'pip uninstall -y --timeout=10 pep8',
runas=None,
cwd=None
)
# Passing a non-int to timeout
mock = MagicMock(return_value={'retcode': 0, 'stdout': ''})
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
self.assertRaises(
ValueError,
pip.uninstall,
'pep8',
timeout='a'
)
示例5: test_install_pre_argument_in_resulting_command
def test_install_pre_argument_in_resulting_command(self):
# Lower than 1.4 versions don't end-up with `--pre` in the resulting
# output
mock = MagicMock(side_effect=[
{'retcode': 0, 'stdout': 'pip 1.2.0 /path/to/site-packages/pip'},
{'retcode': 0, 'stdout': ''}
])
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(
'pep8', pre_releases=True
)
mock.assert_called_with(
'pip install pep8',
runas=None,
cwd=None
)
mock = MagicMock(side_effect=[
{'retcode': 0, 'stdout': 'pip 1.4.0 /path/to/site-pacakges/pip'},
{'retcode': 0, 'stdout': ''}
])
with patch.dict(pip.__salt__, {'cmd.run_all': mock}):
pip.install(
'pep8', pre_releases=True
)
mock.assert_called_with(
'pip install --pre pep8',
runas=None,
cwd=None
)
示例6: test_trigger_events
def test_trigger_events(self, mock_from):
mock_after = Mock()
mock_before = Mock()
mock_from.return_value = {'foo': 1}
ctx = Mock()
view = Mock(
request=Mock(action='index'),
_json_params={'bar': 1},
context=ctx,
_silent=False)
view.index._silent = False
view.index._event_action = None
with patch.dict(events.BEFORE_EVENTS, {'index': mock_before}):
with patch.dict(events.AFTER_EVENTS, {'index': mock_after}):
with events.trigger_events(view):
pass
mock_after.assert_called_once_with(
fields={'foo': 1}, model=view.Model, instance=ctx,
view=view)
mock_before.assert_called_once_with(
fields={'foo': 1}, model=view.Model, instance=ctx,
view=view)
view.request.registry.notify.assert_has_calls([
call(mock_before()),
call(mock_after()),
])
mock_from.assert_called_once_with({'bar': 1}, view.Model)
示例7: test_proctoring_js_includes
def test_proctoring_js_includes(self):
"""
Make sure that proctoring JS does not get included on
courseware pages if either the FEATURE flag is turned off
or the course is not proctored enabled
"""
email, password = self.STUDENT_INFO[0]
self.login(email, password)
self.enroll(self.test_course, True)
test_course_id = self.test_course.id.to_deprecated_string()
with patch.dict(settings.FEATURES, {"ENABLE_SPECIAL_EXAMS": False}):
url = reverse("courseware", kwargs={"course_id": test_course_id})
resp = self.client.get(url)
self.assertNotContains(resp, "/static/js/lms-proctoring.js")
with patch.dict(settings.FEATURES, {"ENABLE_SPECIAL_EXAMS": True}):
url = reverse("courseware", kwargs={"course_id": test_course_id})
resp = self.client.get(url)
self.assertNotContains(resp, "/static/js/lms-proctoring.js")
# now set up a course which is proctored enabled
self.test_course.enable_proctored_exams = True
self.test_course.save()
modulestore().update_item(self.test_course, self.user.id)
resp = self.client.get(url)
self.assertContains(resp, "/static/js/lms-proctoring.js")
示例8: test_install_install_options_argument_in_resulting_command
def test_install_install_options_argument_in_resulting_command(self):
install_options = ["--exec-prefix=/foo/bar", "--install-scripts=/foo/bar/bin"]
# Passing options as a list
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install("pep8", install_options=install_options)
mock.assert_called_once_with(
"pip install "
"--install-option='--exec-prefix=/foo/bar' "
"--install-option='--install-scripts=/foo/bar/bin' pep8",
runas=None,
cwd=None,
)
# Passing mirrors as a comma separated list
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install("pep8", install_options=",".join(install_options))
mock.assert_called_once_with(
"pip install "
"--install-option='--exec-prefix=/foo/bar' "
"--install-option='--install-scripts=/foo/bar/bin' pep8",
runas=None,
cwd=None,
)
# Passing mirrors as a single string entry
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install("pep8", install_options=install_options[0])
mock.assert_called_once_with(
"pip install --install-option='--exec-prefix=/foo/bar' pep8", runas=None, cwd=None
)
示例9: test_install_multiple_editable
def test_install_multiple_editable(self):
editables = [
"git+https://github.com/jek/blinker.git#egg=Blinker",
"git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
]
# Passing editables as a list
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install(editable=editables)
mock.assert_called_once_with(
"pip install "
"--editable=git+https://github.com/jek/blinker.git#egg=Blinker "
"--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
runas=None,
cwd=None,
)
# Passing editables as a comma separated list
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
pip.install(editable=",".join(editables))
mock.assert_called_once_with(
"pip install "
"--editable=git+https://github.com/jek/blinker.git#egg=Blinker "
"--editable=git+https://github.com/saltstack/salt-testing.git#egg=SaltTesting",
runas=None,
cwd=None,
)
示例10: test_tar
def test_tar(self):
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.tar(
'zcvf', 'foo.tar',
['/tmp/something-to-compress-1',
'/tmp/something-to-compress-2'],
cwd=None, template=None
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
'/tmp/something-to-compress-2',
cwd=None,
template=None
)
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.tar(
'zcvf', 'foo.tar',
'/tmp/something-to-compress-1,/tmp/something-to-compress-2',
cwd=None, template=None
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'tar -zcvf foo.tar /tmp/something-to-compress-1 '
'/tmp/something-to-compress-2',
cwd=None,
template=None
)
示例11: test_proxy_detection
def test_proxy_detection(self, proxy, bah, build, install):
with patch.dict('os.environ', {'http_proxy': 'something'}, clear=True):
setup_proxy_opener()
proxy.assert_called_with({'http': 'something'})
with patch.dict('os.environ', {'https_proxy': 'somethings'}, clear=True):
setup_proxy_opener()
proxy.assert_called_with({'https': 'somethings'})
示例12: test_unrar
def test_unrar(self):
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.unrar(
'/tmp/rarfile.rar',
'/home/strongbad/',
excludes='file_1,file_2'
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'unrar x -idp /tmp/rarfile.rar '
'-x file_1 -x file_2 /home/strongbad/',
template=None
)
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.unrar(
'/tmp/rarfile.rar',
'/home/strongbad/',
excludes=['file_1', 'file_2']
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'unrar x -idp /tmp/rarfile.rar '
'-x file_1 -x file_2 /home/strongbad/',
template=None
)
示例13: test_rar
def test_rar(self):
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.rar(
'/tmp/rarfile.rar',
'/tmp/sourcefile1,/tmp/sourcefile2'
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'rar a -idp /tmp/rarfile.rar '
'/tmp/sourcefile1 /tmp/sourcefile2',
template=None
)
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.rar(
'/tmp/rarfile.rar',
['/tmp/sourcefile1', '/tmp/sourcefile2']
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'rar a -idp /tmp/rarfile.rar '
'/tmp/sourcefile1 /tmp/sourcefile2',
template=None
)
示例14: test_unzip
def test_unzip(self):
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.unzip(
'/tmp/salt.{{grains.id}}.zip',
'/tmp/dest',
excludes='/tmp/tmpePe8yO,/tmp/tmpLeSw1A',
template='jinja'
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
template='jinja'
)
mock = MagicMock(return_value='salt')
with patch.dict(archive.__salt__, {'cmd.run': mock}):
ret = archive.unzip(
'/tmp/salt.{{grains.id}}.zip',
'/tmp/dest',
excludes=['/tmp/tmpePe8yO', '/tmp/tmpLeSw1A'],
template='jinja'
)
self.assertEqual(['salt'], ret)
mock.assert_called_once_with(
'unzip /tmp/salt.{{grains.id}}.zip -d /tmp/dest '
'-x /tmp/tmpePe8yO /tmp/tmpLeSw1A',
template='jinja'
)
示例15: test_bridge_line
def test_bridge_line(self):
self.assertRaises(onion.UnrecognizedTransport,
onion.bridge_line, 'rot13', '/log.txt')
onion.find_executable = Mock(return_value=False)
self.assertRaises(onion.UninstalledTransport,
onion.bridge_line, 'fte', '/log.txt')
onion.find_executable = Mock(return_value="/fakebin")
for transport, exp_line in sample_transport_lines.iteritems():
self.assertEqual(onion.bridge_line(transport, '/log.txt'),
exp_line)
with patch.dict(onion.obfsproxy_details,
{'version': onion.OBFSProxyVersion('0.1.12')}):
self.assertRaises(onion.OutdatedObfsproxy,
onion.bridge_line, 'obfs2', '/log.txt')
with patch.dict(onion.tor_details,
{'version': onion.TorVersion('0.2.4.20')}):
onion.bridge_line('fte', '/log.txt')
self.assertRaises(onion.OutdatedTor,
onion.bridge_line, 'scramblesuit', '/log.txt')
self.assertRaises(onion.OutdatedTor,
onion.bridge_line, 'obfs4', '/log.txt')
with patch.dict(onion.tor_details,
{'version': onion.TorVersion('0.2.3.20')}):
self.assertRaises(onion.OutdatedTor,
onion.bridge_line, 'fte', '/log.txt')