本文整理汇总了Python中unittest.mock.Mock.returncode方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.returncode方法的具体用法?Python Mock.returncode怎么用?Python Mock.returncode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.returncode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: popen_side_effect
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def popen_side_effect(*args, **kwargs):
if args[0] == 'ssh-keyscan -t rsa host_a':
mock_popen = Mock()
mock_popen.communicate.return_value = [b"a_host ssh-rsa the_value_a", None]
mock_popen.returncode = 0
return mock_popen
elif args[0] == 'ssh-keyscan -t rsa host_b':
mock_popen = Mock()
mock_popen.communicate.return_value = [b"a_host ssh-rsa the_other_value_b", None]
mock_popen.returncode = 0
return mock_popen
else:
return None
示例2: test_migrate_no_migrations
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_migrate_no_migrations(self, connect_db_mock, backup_mock,
run_mig_mock, any_failed_mock):
'''
The "migrate" CLI command should exit gracefully if there are no
migrations to run.
'''
runner = CliRunner()
cursor = self.make_cursor(connect_db_mock)
cursor.fetchall.return_value = [
('01/bar', 'bootstrapped', datetime.now(), datetime.now()),
('01/foo', 'bootstrapped', datetime.now(), datetime.now()),
('02_foobar', 'succeeded', datetime.now(), datetime.now()),
('03_bazbat', 'succeeded', datetime.now(), datetime.now()),
('04/bat', 'succeeded', datetime.now(), datetime.now()),
('04/baz', 'succeeded', datetime.now(), datetime.now()),
]
process = Mock()
process.returncode = 0
backup_mock.return_value = process
any_failed_mock.return_value = False
with runner.isolated_filesystem():
self.make_sample_files()
cli_args = self.cli_args() + ['migrate']
result = runner.invoke(agnostic.cli, cli_args)
self.assertNotEqual(0, result.exit_code)
self.assertIsInstance(result.exception, SystemExit)
示例3: test_migrate_no_backup
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_migrate_no_backup(self, connect_db_mock, backup_mock, run_mig_mock,
any_failed_mock):
''' The "migrate" CLI command with no --backup option. '''
runner = CliRunner()
cursor = self.make_cursor(connect_db_mock)
cursor.fetchall.return_value = [
('01/bar', 'bootstrapped', datetime.now(), datetime.now()),
('01/foo', 'bootstrapped', datetime.now(), datetime.now()),
('02_foobar', 'succeeded', datetime.now(), datetime.now()),
('04/bat', 'succeeded', datetime.now(), datetime.now()),
]
process = Mock()
process.returncode = 0
backup_mock.return_value = process
any_failed_mock.return_value = False
with runner.isolated_filesystem():
self.make_sample_files()
cli_args = self.cli_args() + ['migrate', '--no-backup']
result = runner.invoke(agnostic.cli, cli_args)
self.assertEqual(0, result.exit_code)
self.assertTrue(cursor.execute.called)
self.assertFalse(backup_mock.called)
self.assertTrue(run_mig_mock.called)
self.assertRegex(result.output, r'run 2 migrations')
示例4: test_csr
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_csr(self, popen):
CSR_DUMP = textwrap.dedent("""
Certificate Request:
Data:
Version: 0 (0x0)
Subject:
Subject Public Key Info:
Public Key Algorithm: id-ecPublicKey
Public-Key: (256 bit)
pub:
[skipped]
ASN1 OID: prime256v1
NIST CURVE: P-256
Attributes:
Requested Extensions:
X509v3 Subject Alternative Name:
DNS:example.org, DNS:www.example.org
Signature Algorithm: ecdsa-with-SHA256
[skipped]
""").strip().encode("ascii")
def mock_openssl(*whatever):
return (CSR_DUMP, "wat")
process = Mock()
process.communicate = Mock(return_value=mock_openssl())
process.returncode = 0
popen.return_value = process
csr = CertificateRequest("mock.csr")
domains = csr.get_domains()
self.assertEqual(domains, {"example.org", "www.example.org"})
示例5: test_ec_key
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_ec_key(self, popen):
KEY_DUMP = textwrap.dedent("""
Private-Key: (256 bit)
priv:
04:00:00:00:00:00:00:00:00:00:00:00:00:00:00:
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:
00:01
pub:
04:00:00:00:00:00:00:00:00:00:00:00:00:00:00:
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:
00:00:01:00:00:00:00:00:00:00:00:00:00:00:00:
00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:
00:00:00:00:02
ASN1 OID: prime256v1
NIST CURVE: P-256
""").strip().encode("ascii")
def mock_openssl(*whatever):
return (KEY_DUMP, "wat")
process = Mock()
process.communicate = Mock(return_value=mock_openssl())
process.returncode = 0
popen.return_value = process
key = ECKeyFile("mock.pem")
jwk = key.as_jwk()
self.assertEqual(jwk, {
"kty": "EC",
"crv": "P-256",
"x": "A" * 42 + "E",
"y": "A" * 42 + "I",
})
示例6: exec_command
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def exec_command(*args, **kwargs):
nonlocal command_response_dict
if args[0] in command_response_dict:
response_mock = Mock()
response_mock.raw_output = command_response_dict[args[0]].encode('utf-8')
response_mock.raw_error = None
response_mock.returncode = 0
return response_mock
示例7: test_open_git_status__fail
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_open_git_status__fail(self, pipe_open_cls_mock, sys_mock):
popen_mock = Mock()
popen_mock.returncode = 1
popen_mock.communicate.return_value = (" ", None)
pipe_open_cls_mock.return_value = popen_mock
open_git_status()
self.assertTrue(sys_mock.exit.called)
示例8: test_wait_for_process_failed
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_wait_for_process_failed(self):
''' Raise exception if process doesn't exit cleanly. '''
process = Mock()
process.args = MagicMock()
process.returncode = 1
with self.assertRaises(click.ClickException):
agnostic._wait_for(process)
示例9: test_wait_for_process
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_wait_for_process (self):
''' Wait for process to finish. '''
process = Mock()
process.returncode = 0
agnostic._wait_for(process)
self.assertTrue(process.wait.called)
示例10: test_open_git_status
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_open_git_status(self, pipe_open_cls_mock):
status = "## branch\n M zsh/test.py\n"
popen_mock = Mock()
popen_mock.returncode = 0
popen_mock.communicate.return_value = (status, None)
pipe_open_cls_mock.return_value = popen_mock
results = open_git_status()
self.assertListEqual(
[u"## branch", u" M zsh/test.py"],
results.splitlines(),
)
示例11: mock_process_creator
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def mock_process_creator(error: bool = False) -> asyncio.coroutine:
"""Mock a coroutine that creates a process when yielded."""
@asyncio.coroutine
def communicate() -> Tuple[bytes, bytes]:
"""Mock a coroutine that runs a process when yielded.
Returns a tuple of (stdout, stderr).
"""
return b"I am stdout", b"I am stderr"
mock_process = Mock()
mock_process.communicate = communicate
mock_process.returncode = int(error)
return mock_process
示例12: test_snapshot
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_snapshot(self, insert_sql_mock, make_snapshot_mock):
''' The "snapshot" CLI command. '''
runner = CliRunner()
process = Mock()
process.returncode = 0
make_snapshot_mock.return_value = process
with runner.isolated_filesystem():
self.make_sample_files()
cli_args = self.cli_args() + ['snapshot', 'out.sql']
result = runner.invoke(agnostic.cli, cli_args)
self.assertEqual(0, result.exit_code)
self.assertTrue(make_snapshot_mock.called)
示例13: test_main_with_svn_command
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_main_with_svn_command(self):
os.environ['SSH_ORIGINAL_COMMAND'] = self._svn_cmd * 1
process_mock = Mock()
process_mock.returncode = self._side_effect
with IOCapture(stdout=False, stderr=True, module='vcs_ssh') as ioc:
with patch('vcs_ssh.subprocess.Popen') as popen_mock:
popen_mock.return_value = process_mock
res = main()
self.assertEqual(
ioc.get_stderr(),
'remote: Warning: using Subversion: no access control enforced!\n')
popen_mock.assert_called_once_with(
self._svn_cmd.split(),
shell=False)
process_mock.communicate.assert_called_once_with(*())
self.assertEqual(res, self._side_effect)
示例14: test_migrate_fail
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_migrate_fail(self, connect_db_mock, backup_mock, run_mig_mock,
restore_mock, any_failed_mock):
'''
If a migration fails, the "migrate" CLI command should handle it
gracefully.
'''
runner = CliRunner()
cursor = self.make_cursor(connect_db_mock)
cursor.fetchall.return_value = [
('01/bar', 'bootstrapped', datetime.now(), datetime.now()),
('01/foo', 'bootstrapped', datetime.now(), datetime.now()),
('02_foobar', 'succeeded', datetime.now(), datetime.now()),
('04/bat', 'succeeded', datetime.now(), datetime.now()),
]
process = Mock()
process.returncode = 0
backup_mock.return_value = process
restore_mock.return_value = process
run_mig_mock.side_effect = ValueError
any_failed_mock.return_value = False
# In non-debug mode, should exit with non-zero code.
with runner.isolated_filesystem():
self.make_sample_files()
cli_args1 = self.cli_args() + ['migrate']
result1 = runner.invoke(agnostic.cli, cli_args1)
self.assertNotEqual(0, result1.exit_code)
self.assertIsInstance(result1.exception, SystemExit)
# In debug mode, should raise an exception.
with runner.isolated_filesystem():
self.make_sample_files()
cli_args2 = self.cli_args() + ['--debug', 'migrate']
result2 = runner.invoke(agnostic.cli, cli_args2)
self.assertNotEqual(0, result2.exit_code)
self.assertIsInstance(result2.exception, ValueError)
示例15: test_test_y
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import returncode [as 别名]
def test_test_y(self, insert_sql_mock, connect_db_mock, clear_schema_mock,
load_snap_mock, make_snap_mock, pending_mig_mock,
run_mig_mock, diff_mock):
'''
The "test" CLI command.
This test is awwwwwwwful.
'''
runner = CliRunner()
cursor = self.make_cursor(connect_db_mock)
process = Mock()
process.returncode = 0
load_snap_mock.return_value = process
make_snap_mock.return_value = process
diff_mock.return_value = []
# Should succeed with empty diff.
with runner.isolated_filesystem():
self.make_sample_files()
with open('current.sql', 'w') as current:
current.write('current')
with open('target.sql', 'w') as target:
target.write('target')
cli_args2 = self.cli_args() + ['test', 'current.sql', 'target.sql']
result1 = runner.invoke(agnostic.cli, cli_args2, input='y')
self.assertEqual(0, result1.exit_code)
self.assertTrue(clear_schema_mock.called)
self.assertTrue(load_snap_mock.called)
self.assertTrue(pending_mig_mock.called)
self.assertTrue(run_mig_mock.called)
self.assertTrue(make_snap_mock.called)
# Should abort if user types 'n'.
with runner.isolated_filesystem():
self.make_sample_files()
with open('current.sql', 'w') as current:
current.write('current')
with open('target.sql', 'w') as target:
target.write('target')
cli_args2 = self.cli_args() + ['test', 'current.sql', 'target.sql']
result2 = runner.invoke(agnostic.cli, cli_args2, input='n')
self.assertNotEqual(0, result2.exit_code)
# Should fail with with non-empty diff.
diff_mock.return_value = ['foo']
with runner.isolated_filesystem():
self.make_sample_files()
with open('current.sql', 'w') as current:
current.write('current')
with open('target.sql', 'w') as target:
target.write('target')
cli_args3 = self.cli_args() + ['test', 'current.sql', 'target.sql']
result3 = runner.invoke(agnostic.cli, cli_args3, input='y')
self.assertNotEqual(0, result3.exit_code)