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


Python Mock.content方法代码示例

本文整理汇总了Python中unittest.mock.Mock.content方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.content方法的具体用法?Python Mock.content怎么用?Python Mock.content使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在unittest.mock.Mock的用法示例。


在下文中一共展示了Mock.content方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mock_response

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
def mock_response(
        status=200,
        content="CONTENT",
        json_data=None,
        raise_for_status=None,
        url=None):
    """
    since we typically test a bunch of different
    requests calls for a service, we are going to do
    a lot of mock responses, so its usually a good idea
    to have a helper function that builds these things
    """
    mock_resp = Mock()
    # mock raise_for_status call w/optional error
    mock_resp.raise_for_status = Mock()
    if raise_for_status:
        mock_resp.raise_for_status.side_effect = raise_for_status
    # set status code and content
    mock_resp.status_code = status
    mock_resp.content = content
    mock_resp.url = url
    # add json data if provided
    if json_data:
        mock_resp.json = Mock(
            return_value=json_data
        )
    return mock_resp
开发者ID:fedspendingtransparency,项目名称:data-act-broker-backend,代码行数:29,代码来源:mock_helpers.py

示例2: setUp

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
 def setUp(self):
     response = Mock()
     response.content = json.dumps({'status': 'ok'})
     response.headers = {'Content-Type': 'application/json'}
     response.ok = True
     response.status_code = requests.codes.ok
     self.response = response
开发者ID:maxcountryman,项目名称:rauth,代码行数:9,代码来源:base.py

示例3: test_async_iterator

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
async def test_async_iterator():
    response = Mock()

    class TestAsyncIterator(AsyncIterator):
        def __init__(self):
            self.i = 0

        async def __anext__(self):
            self.i += 1

            if self.i > 5:
                raise StopAsyncIteration
            else:
                return '{}\r\n'.format(self.i).encode('utf-8')

    response.content = TestAsyncIterator()
    ait = _AsyncLineIterator(response, True)
    ait.get_encoding = lambda: 'utf-8'

    lines = list()
    async for line in ait:
        lines.append(line)

    assert lines == ['1', '2', '3', '4', '5']

    response = Mock()
    response.content.iter_chunked.return_value = TestAsyncIterator()
    ait = _AsyncChunkIterator(response, True)
    ait.get_encoding = lambda: 'utf-8'

    chunks = list()
    async for chunk in ait:
        chunks.append(chunk)

    assert chunks == ['1\n', '2\n', '3\n', '4\n', '5\n']
开发者ID:pombredanne,项目名称:spacetrack,代码行数:37,代码来源:test_aio.py

示例4: test_fetch_and_validate_source_when_source_does_not_contain_target

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_fetch_and_validate_source_when_source_does_not_contain_target(self, mock_get):
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.content = 'foo'
        mock_get.return_value = mock_response

        self.assertRaises(TargetNotFoundError, fetch_and_validate_source, self.source, self.target)
开发者ID:easy-as-python,项目名称:django-webmention,代码行数:9,代码来源:test_resolution.py

示例5: test_fetch_and_validate_source_happy_path

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_fetch_and_validate_source_happy_path(self, mock_get):
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.content = '<a href="{href}">{href}</a>'.format(href=self.target)
        mock_get.return_value = mock_response

        self.assertEqual(mock_response.content, fetch_and_validate_source(self.source, self.target))
开发者ID:easy-as-python,项目名称:django-webmention,代码行数:9,代码来源:test_resolution.py

示例6: test_sucess

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
 def test_sucess(self, requests_mock):
     js = json.dumps(dct)
     response = Mock()
     response.content = js.encode('utf8')
     get_mock = Mock(return_value=response)
     requests_mock.get = get_mock
     resultado = usuario.acessar_dados('renzon')
     get_mock.assert_called_once_with('https://api.github.com/users/renzon')
     self.assertDictEqual(dct, resultado)
开发者ID:pythonprobr,项目名称:pypraticot04,代码行数:11,代码来源:usuario_tests.py

示例7: test_complete_request

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_complete_request(self):
        transport = t.APITransport(Mock())
        transport._http = Mock()

        http_result = Mock()
        http_result.content = b'{"stat":"ok","result":"bar"}'
        transport._http.post.return_value = http_result

        self.assertEqual(
            "bar", transport(t.APITransport.NO_ENCRYPT[0], foo="bar"))
开发者ID:mcrute,项目名称:pydora,代码行数:12,代码来源:test_transport.py

示例8: assert_mock_search_results

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
 def assert_mock_search_results(self, search_parameter, page_num):
     # url to stub out
     url = SearchSong.URL_FORMAT % (search_parameter, page_num)
     # mock out hymnal.net response
     mock_response = Mock()
     mock_response.content = open('test_data/search_song_html_{}_{}.txt'.format(search_parameter, page_num), 'r')
     
     patcher = patch('requests.get', Mock(side_effect = lambda k: {url: mock_response}.get(k, 'unhandled request %s' % k)))
     patcher.start()
     self.assert_search_results(search_parameter, page_num)
     patcher.stop()
开发者ID:skywalkerdude,项目名称:HymnalApi,代码行数:13,代码来源:SearchSongTest.py

示例9: listing_and_sumatra_record

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
def listing_and_sumatra_record(request):
    listing = request.param
    output_data = Mock()
    output_data.content = listing.path_to_file.read_text().encode('utf-8')
    datastore = Mock(spec_set=DataStore)
    datastore.get_data_item.return_value = output_data
    record = Mock(spec=Record)
    record.datastore = datastore
    record.output_data = ['something']
    record.to_sumatra = Mock()
    record.to_sumatra.return_value = record
    return listing, record
开发者ID:philipphanemann,项目名称:forrestgums,代码行数:14,代码来源:test_core.py

示例10: test_make_http_request

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_make_http_request(self):
        # url, data, params
        http = Mock()
        retval = Mock()
        retval.content = "foo"
        http.post.return_value = retval

        self.transport._http = http
        res = self.transport._make_http_request(
            "/url", b"data", {"a": None, "b": "c"})

        http.post.assert_called_with("/url", data=b"data", params={"b": "c"})
        retval.raise_for_status.assert_called_with()

        self.assertEqual("foo", res)
开发者ID:mcrute,项目名称:pydora,代码行数:17,代码来源:test_transport.py

示例11: requests_factory

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
def requests_factory(text='some text',
                     location='some location',
                     content=b'some content'):
    requests = Mock()

    get_response = Mock()
    get_response.text = text
    get_response.content = content

    head_response = Mock()
    head_response.headers = {'Location': location}

    requests.get = Mock(return_value=get_response)
    requests.head = Mock(return_value=head_response)

    return requests
开发者ID:dduong42,项目名称:Booksee,代码行数:18,代码来源:factories.py

示例12: test_deliver_slice_csv_inline

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_deliver_slice_csv_inline(self, send_email_smtp, get):
        response = Mock()
        get.return_value = response
        response.raise_for_status.return_value = None
        response.content = self.CSV

        schedule = db.session.query(SliceEmailSchedule).filter_by(
            id=self.slice_schedule).all()[0]

        schedule.email_format = SliceEmailReportFormat.data
        schedule.delivery_type = EmailDeliveryType.inline

        deliver_slice(schedule)
        send_email_smtp.assert_called_once()

        self.assertIsNone(send_email_smtp.call_args[1]['data'])
        self.assertTrue('<table ' in send_email_smtp.call_args[0][2])
开发者ID:tan31989,项目名称:caravel,代码行数:19,代码来源:schedules_test.py

示例13: test_get_or_create_from_username_create

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_get_or_create_from_username_create(self, mock_get):
        response = Mock()
        response.status_code = 200
        photo_bytes_array = bytearray(self.photo_bytes)
        expected_image = "data:image/jpg;base64,%s" % base64.b64encode(photo_bytes_array).decode('utf-8')
        response.content = photo_bytes_array
        mock_get.return_value = response

        with mock.patch('pmp_shield.employees.ldap_tools.LDAPTool.search_by_username',
                        TestACPEmployee.mock_ldap.search_by_username):
            employee, created = Employee.objects.get_or_create_from_username('lberrocal')
            self.assertEqual('Luis', employee.first_name)
            self.assertEqual('Berrocal Cordoba', employee.last_name)
            self.assertEqual('272-4149', employee.phones.first().phone_number)
            self.assertEqual(1, Employee.objects.count())
            self.assertEqual(expected_image, employee.photo_url)
            self.assertTrue(created)
开发者ID:luiscberrocal,项目名称:pmp_shield,代码行数:19,代码来源:test_models.py

示例14: test_deliver_slice_csv_attachment

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
    def test_deliver_slice_csv_attachment(self, send_email_smtp, get):
        response = Mock()
        get.return_value = response
        response.raise_for_status.return_value = None
        response.content = self.CSV

        schedule = db.session.query(SliceEmailSchedule).filter_by(
            id=self.slice_schedule).all()[0]

        schedule.email_format = SliceEmailReportFormat.data
        schedule.delivery_type = EmailDeliveryType.attachment

        deliver_slice(schedule)
        send_email_smtp.assert_called_once()

        file_name = __('%(name)s.csv', name=schedule.slice.slice_name)

        self.assertEquals(
            send_email_smtp.call_args[1]['data'][file_name],
            self.CSV,
        )
开发者ID:tan31989,项目名称:caravel,代码行数:23,代码来源:schedules_test.py

示例15: message

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import content [as 别名]
 def message(self, last_vote):
     message = Mock()
     message.content = last_vote
     return message
开发者ID:niccokunzmann,项目名称:pypaxos,代码行数:6,代码来源:test_basic_paxos.py


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