本文整理汇总了Python中mock.Mock.text方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.text方法的具体用法?Python Mock.text怎么用?Python Mock.text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mock.Mock
的用法示例。
在下文中一共展示了Mock.text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_mock
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def get_mock():
ysc = youtube_status_code
yu = youtube_url
yc = youtube_cookies
yt = youtube_text
yh = youtube_headers
yosc = youtube_oembed_status_code
you = youtube_oembed_url
yoh = youtube_oembed_headers
yot = youtube_oembed_text
mock_obj_1 = Mock()
mock_obj_1.status_code = ysc
mock_obj_1.url = yu
mock_obj_1.headers = yh
mock_obj_1.cookies = yc
mock_obj_1.text = yt
mock_obj_1.json = lambda: json.loads(mock_obj_1.text)
mock_obj_2 = Mock()
mock_obj_2.status_code = 200
mock_obj_2.url = you
mock_obj_2.headers = yoh
mock_obj_2.text = yot
mock_obj_2.json = lambda: json.loads(mock_obj_2.text)
return [mock_obj_1, mock_obj_1, mock_obj_2]
示例2: test_resource_in_discovery_container_after_get_patch_delete
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_resource_in_discovery_container_after_get_patch_delete(self):
with mock.patch('requests.get') as requests_get_mock:
resource = {"@odata.id": "odata.id", "something": "irrelevant"}
get_response = Mock()
get_response.status_code = ReturnCodes.OK
get_response.headers = {}
get_response.text = json.dumps(resource)
requests_get_mock.return_value = get_response
discovery_container = DiscoveryContainer()
self.api_caller.get_resource("/resource", discovery_container)
self.assertEqual(discovery_container["http://{API_ENDPOINT}/resource".format(
API_ENDPOINT=API_ENDPOINT)].body,
resource)
patched_resource = {"@odata.id": "odata.id", "something": "relevant"}
get_response.text = json.dumps(patched_resource)
with mock.patch('requests.patch') as requests_patch_mock:
patch_response = Mock()
patch_response.status_code = ReturnCodes.OK
patch_response.headers = {}
patch_response.text = "{}"
requests_patch_mock.return_value = patch_response
_, _, _, _ = self.api_caller.patch_resource("/resource", discovery_container)
self.assertEqual(discovery_container["http://{API_ENDPOINT}/resource".format(
API_ENDPOINT=API_ENDPOINT)].body, patched_resource)
with mock.patch('requests.delete') as requests_delete_mock:
delete_response = Mock()
delete_response.status_code = ReturnCodes.NO_CONTENT
delete_response.headers = {}
delete_response.text = ""
requests_delete_mock.return_value = delete_response
_, _, _, _ = self.api_caller.delete_resource("/resource", discovery_container)
self.assertNotIn("/resource", discovery_container)
示例3: test_check_status
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_check_status():
r = Mock()
r.status_code = 429
r.text = ''
with pytest.raises(exceptions.TooManyRequests):
utils.check_status(r)
r.text = 'exceeded QUOTA dude'
with pytest.raises(exceptions.OverQuota):
utils.check_status(r)
示例4: mock_response
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def mock_response(fixture=None, status=200):
response = Mock()
if fixture is None:
response.text = ''
elif isinstance(fixture, dict):
response.text = str(fixture)
else:
response.text = load_fixture(fixture)
response.status_code = status
return response
示例5: test_parse_response_invalid_json
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_parse_response_invalid_json(self):
""" Deal gracefully with an invaldi JSON response """
resp = Mock(name="Mock Response")
resp.text = 'A Server error occurred. Please contact the administrator.'
resp.status_code = 200
with self.assertRaises(exceptions.IndecipherableResponseError):
self.c._parse_resp("FOO", resp)
示例6: test_parse_response_500
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_parse_response_500(self):
""" Deal gracefully with 500 errors """
resp = Mock(name="Mock Response")
resp.text = 'A Server error occurred. Please contact the administrator.'
resp.status_code = 500
with self.assertRaises(exceptions.RemoteError):
self.c._parse_resp("FOO", resp)
示例7: _verify
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def _verify(self, requests, response_text="", assertion=EXPIRED_ASSERTION, status_code=200):
response = Mock()
response.text = response_text
response.status_code = status_code
requests.request.return_value = response
return self.verifier.verify(assertion)
示例8: test_package_version_for_hash
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_package_version_for_hash(self, m_get):
mock_resp = Mock()
mock_resp.ok = True
mock_resp.text = "the_version"
m_get.return_value = mock_resp
result = util.package_version_for_hash("hash")
assert result == "the_version"
示例9: test_body_assertion_var_failed
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_body_assertion_var_failed(self):
# Given
response_body = 'Unexpected value'
expected = 'Expected var value'
config = Config.from_dict(
{'host': 'host', 'vars': {'some_var': expected}},
__file__,
)
spec = {
'type': 'body',
'value': {'type': 'ref', 'var': 'some_var'},
}
assertion = BodyAssertion.from_dict(spec)
case = MockTestCase()
response = Mock()
response.text = response_body
# When
assertion.run(config, 'url', case, response)
# Then
self.assertEqual(case.assertEqual.call_count, 1)
call = case.assertEqual.call_args
args, kwargs = call
self.assertEqual(args, (response_body, expected))
self.assertIn('msg', kwargs)
示例10: test_version_strip_v
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_version_strip_v(self):
resp = Mock()
resp.ok = True
resp.text = "v0.90.0"
self.m_get_response.return_value = resp
super(TestGitbuilderProject, self)\
.test_get_package_version_found()
示例11: test_unexpected_response
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_unexpected_response(self):
response = Mock()
response.status_code = 422
response.text = '{"message": "Unprocessable Entity"}'
exception = UnexpectedResponse(response)
self.assertEqual("%s" % exception, "Unexpected response from server (Error code: 422, Message: Unprocessable Entity)")
示例12: build_response_mock
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def build_response_mock(status_code, json_body=None, headers=None,
add_content_length=True, **kwargs):
real_response = requests.Response()
real_response.status_code = status_code
text = None
if json_body is not None:
text = json.dumps(json_body)
if add_content_length and headers is not {}:
real_response.headers['content-length'] = len(text)
if headers is not None:
for k, v in headers.items():
real_response.headers[k] = v
for k, v in kwargs.items():
setattr(real_response, k, v)
response = Mock(wraps=real_response, autospec=True)
if text:
response.text = text
# for some reason, wraps cannot handle attributes which are dicts
# and accessed by key
response.headers = real_response.headers
response.content = text
return response
示例13: test_get_api_pod_parse_error
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_get_api_pod_parse_error(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
# Set up mock objects
self.driver.auth_token = 'TOKEN'
m_json_load.side_effect = TypeError
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
assert_raises(ApplyProfileError, self.driver._get_api_pod)
示例14: test_get_api_pod_with_client_certs
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_get_api_pod_with_client_certs(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
self.driver.client_certificate = "cert.pem"
self.driver.client_key = "key.pem"
self.driver.certificate_authority = "ca.pem"
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
self.driver._get_api_pod()
# Assert correct data in calls.
m_session_obj.get.assert_called_once_with(
api_root + 'namespaces/a/pods/pod-1',
verify="ca.pem", cert=("cert.pem", "key.pem"))
m_json_load.assert_called_once_with(pod1)
示例15: test_get_api_pod
# 需要导入模块: from mock import Mock [as 别名]
# 或者: from mock.Mock import text [as 别名]
def test_get_api_pod(self, m_json_load, m_session):
# Set up driver.
self.driver.pod_name = 'pod-1'
self.driver.namespace = 'a'
pod1 = '{"metadata": {"namespace": "a", "name": "pod-1"}}'
api_root = "http://kubernetesapi:8080/api/v1/"
self.driver.api_root = api_root
# Set up mock objects
self.driver.auth_token = 'TOKEN'
get_obj = Mock()
get_obj.status_code = 200
get_obj.text = pod1
m_session_obj = Mock()
m_session_obj.headers = Mock()
m_session_obj.get.return_value = get_obj
m_session.return_value = m_session_obj
m_session_obj.__enter__ = Mock(return_value=m_session_obj)
m_session_obj.__exit__ = Mock(return_value=False)
# Call method under test
self.driver._get_api_pod()
# Assert correct data in calls.
m_session_obj.headers.update.assert_called_once_with(
{'Authorization': 'Bearer ' + 'TOKEN'})
m_session_obj.get.assert_called_once_with(
api_root + 'namespaces/a/pods/pod-1',
verify=False)
m_json_load.assert_called_once_with(pod1)