当前位置: 首页>>代码示例>>Python>>正文


Python MagicMock.raise_for_status方法代码示例

本文整理汇总了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)
开发者ID:fsat,项目名称:conductr-cli,代码行数:37,代码来源:test_sse_client.py

示例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'))
开发者ID:zalando-stups,项目名称:pierone-cli,代码行数:28,代码来源:test_cli.py

示例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])
开发者ID:mattdodge,项目名称:gd,代码行数:10,代码来源:test_scrape.py

示例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])
开发者ID:onelson,项目名称:gd,代码行数:13,代码来源:test_scrape.py

示例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])
开发者ID:mattdodge,项目名称:gd,代码行数:13,代码来源:test_scrape.py

示例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, [])
开发者ID:onelson,项目名称:gd,代码行数:17,代码来源:test_scrape.py

示例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")
开发者ID:mattdodge,项目名称:gd,代码行数:19,代码来源:test_scrape.py


注:本文中的unittest.mock.MagicMock.raise_for_status方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。