本文整理汇总了Python中unittest.mock.MagicMock.raise_for_status方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.raise_for_status方法的具体用法?Python MagicMock.raise_for_status怎么用?Python MagicMock.raise_for_status使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.raise_for_status方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_sse
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_sse(self):
raw_sse = strip_margin("""|data:
|
|event:My Test Event
|data:My Test Data
|
|data:
|
|""")
raw_sse_iterators = [iter(list(raw_sse))]
iter_content_mock = MagicMock(side_effect=raw_sse_iterators)
raise_for_status_mock = MagicMock()
response_mock = MagicMock()
response_mock.raise_for_status = raise_for_status_mock
response_mock.iter_content = iter_content_mock
request_get_mock = MagicMock(return_value=response_mock)
result = []
with patch('requests.get', request_get_mock):
events = sse_client.get_events(False, '127.0.0.1', 'http://host.com')
for event in events:
result.append(event)
self.assertEqual([
sse_client.Event(event=None, data=''),
sse_client.Event(event='My Test Event', data='My Test Data'),
sse_client.Event(event=None, data='')
], result)
request_get_mock.assert_called_with('http://host.com', stream=True, **sse_client.SSE_REQUEST_INPUT)
raise_for_status_mock.assert_called_with()
iter_content_mock.assert_called_with(decode_unicode=True)
示例2: test_invalid_url_for_login
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_invalid_url_for_login(monkeypatch, tmpdir):
runner = CliRunner()
response = MagicMock()
monkeypatch.setattr('stups_cli.config.load_config', lambda x: {})
monkeypatch.setattr('pierone.api.get_token', MagicMock(return_value='tok123'))
monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
# Missing Pier One header
response.text = 'Not valid API'
monkeypatch.setattr('requests.get', lambda *args, **kw: response)
with runner.isolated_filesystem():
result = runner.invoke(cli, ['login'], catch_exceptions=False, input='pieroneurl\n')
assert 'ERROR: Did not find a valid Pier One registry at https://pieroneurl' in result.output
assert result.exit_code == 1
assert not os.path.exists(os.path.join(str(tmpdir), '.docker/config.json'))
# Not a valid header
response.raise_for_status = MagicMock(side_effect=RequestException)
monkeypatch.setattr('requests.get', lambda *args, **kw: response)
with runner.isolated_filesystem():
result = runner.invoke(cli, ['login'], catch_exceptions=False, input='pieroneurl\n')
assert 'ERROR: Could not reach https://pieroneurl' in result.output
assert result.exit_code == 1
assert not os.path.exists(os.path.join(str(tmpdir), '.docker/config.json'))
示例3: test_HTTPError
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_HTTPError(self, mock_get):
url = "http://gd.mlb.com/inning/inning_all.xml"
response = MagicMock()
response.raise_for_status = MagicMock(side_effect=HTTPError)
mock_get.return_value = response
scrape.download([url])
示例4: test_HTTPError
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_HTTPError(self, mock_get):
url = "http://gd.mlb.com/inning/inning_all.xml"
response = MagicMock()
response.raise_for_status = MagicMock(side_effect=HTTPError)
mock_get.return_value = response
actual_count, actual_fails = scrape.download([url], "test")
self.assertEqual(actual_count, 0)
self.assertEqual(actual_fails, [url])
示例5: test_directory
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_directory(self, mock_makedirs, mock_get):
url = "http://gd.mlb.com/inning/"
response = MagicMock()
response.raise_for_status = MagicMock()
mock_get.return_value = response
mo = mock_open()
# Need to patch `open` within the gd.scrape namespace.
with patch("%s.open" % scrape.__name__, mo, create=True):
scrape.download([url])
示例6: test_directory
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_directory(self, mock_makedirs, mock_get):
url = "http://gd.mlb.com/inning/"
response = MagicMock()
response.raise_for_status = MagicMock()
mock_get.return_value = response
mo = mock_open()
# Need to patch `open` within the gd.scrape namespace.
with patch("%s.open" % scrape.__name__, mo, create=True):
actual_count, actual_fails = scrape.download([url], "test")
# We bail out early when directories come up.
self.assertEqual(actual_count, 0)
self.assertEqual(actual_fails, [])
示例7: test_file
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import raise_for_status [as 别名]
def test_file(self, mock_makedirs, mock_get):
urls = ["http://gd.mlb.com/test1.xml", "http://gd.mlb.com/test2.xml"]
targets = ["gd.mlb.com/test1.xml", "gd.mlb.com/test1.xml"]
content = "content"
response = MagicMock()
response.raise_for_status = MagicMock()
response.content.side_effect = content
mock_get.return_value = response
mo = mock_open()
# Need to patch `open` within the gd.scrape namespace.
with patch("%s.open" % scrape.__name__, mo, create=True):
scrape.download(urls)
mo.assert_any_call(targets[0], "w")
mo.assert_any_call(targets[1], "w")