本文整理汇总了Python中unittest.mock.MagicMock.text方法的典型用法代码示例。如果您正苦于以下问题:Python MagicMock.text方法的具体用法?Python MagicMock.text怎么用?Python MagicMock.text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.MagicMock
的用法示例。
在下文中一共展示了MagicMock.text方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def get(url, **kwargs):
root = os.path.join(os.path.dirname(__file__), 'statics')
load = lambda x: open(os.path.join(root, x), 'rb').read()
response = MagicMock(spec=requests.Response)
if url == 'feed_url':
response.text = load('feed.xml')
elif url == 'entry_url':
response.text = load('entry.html')
elif url == 'thumbnail_url':
response.content = load('thumbnail.png')
return response
示例2: assert_mock_get_hymn
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def assert_mock_get_hymn(self, hymn_type, hymn_number, stored_content_path = None, query_params = tuple()):
stubbed_path = GetSong.HYMN_PATH_FORMAT % (hymn_type, hymn_number)
# url to stub out
url = GetSong.GET_SONG_URL_FORMAT % stubbed_path
stubbed_url = Utils.add_query_to_url(url, query_params)
# mock out hymnal.net response
# https://docs.python.org/3/library/unittest.mock.html
mock_response = Mock()
mock_data_format = Utils.add_query_to_url('test_data/get_song_html_{}_{}', query_params)
mock_data_format += '.txt'
with open(mock_data_format.format(hymn_type, hymn_number), 'r') as m:
mock_response.text = m.read()
# key order doesn't matter for dict equality, so compare query parameter dicts
def get_url(url):
parsed_url = urllib.parse.urlparse(url)
params = urllib.parse.parse_qsl(parsed_url.query)
assert_equal(dict(query_params), dict(params))
return mock_response
# http://stackoverflow.com/questions/15753390/python-mock-requests-and-the-response
with patch('requests.get', Mock(side_effect=get_url)) as n:
self.assert_get_hymn(hymn_type, hymn_number, query_params, stored_content_path)
示例3: test_invalid_url_for_login
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [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'))
示例4: test_get_dict_value_from_JSON_webserviceExtractCorrectValue
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_get_dict_value_from_JSON_webserviceExtractCorrectValue(self):
get_mock = MagicMock()
return_value = MagicMock()
return_value.text = "{\"programs\":{\"feed\":{\"brightness\":\"blub\"}}}"
get_mock.return_value = return_value
requests.get = get_mock
from flicintegration import eventhelper
self.assertEqual(eventhelper.get_dict_value_from_JSON_webservice(EventHelper.piLedHost, "/getConfiguration",
["programs", "feed", "brightness"]), "blub")
示例5: test_get_status_404
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_get_status_404(self):
response_mock = MagicMock()
response_mock.status_code = 404
response_mock.text = None
self.metro_lisboa._http_connector.get.return_value = response_mock
previous_status = self.metro_lisboa._status
self.metro_lisboa._update_from_remote_site(self.url)
self.assertEqual(previous_status, self.metro_lisboa._status)
示例6: test_respond_to_gift
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_respond_to_gift(self):
mock_request = MagicMock()
mock_request.id = "0"
mock_request.user.screen_name = 'tester'
mock_request.text = "dear @bot give a gift to @gf"
mock_handle_tweet_posting = MagicMock()
mock_handle_tweet_posting.return_value = True
with patch('twitterbot.handle_tweet_posting', mock_handle_tweet_posting):
twitterbot.respond_to_gift_request(mock_request)
mock_handle_tweet_posting.assert_called_once_with("@gf only_answer @tester", "0")
示例7: test_get_status_requests_exception
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_get_status_requests_exception(self):
response_mock = MagicMock()
response_mock.status_code = None
response_mock.text = None
self.metro_lisboa._http_connector.get.side_effect = requests.exceptions.ConnectTimeout(
"Requests: Connection timeout")
previous_status = self.metro_lisboa._status
self.metro_lisboa._update_from_remote_site(self.url)
self.assertEqual(previous_status, self.metro_lisboa._status)
示例8: mock_add_item
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def mock_add_item(level=None, reorder=False, uid=None):
"""Return a mock item and store it."""
mock_item = MagicMock()
mock_item.level = level
mock_item.text = ''
if uid:
mock_item.uid = uid
else:
mock_item.uid = self.next_uid
self.next_uid += 1
self.document.items.append(mock_item)
return mock_item
示例9: build
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def build(self):
m = MagicMock()
m.text = self._text
m.url = self._url
m.status_code = self._status_code
m.headers = CaseInsensitiveDict({
"content-type": self._content_type
})
m.content = self._content
m.encoding = self._encoding
m.elapsed = datetime.timedelta(seconds=self._seconds, microseconds=self._microseconds)
m.json.return_value = self._json
return m
示例10: test_get_dict_value_from_JSON_webserviceCatchesAndLogsKeyErrorOnInvalidJSON
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_get_dict_value_from_JSON_webserviceCatchesAndLogsKeyErrorOnInvalidJSON(self):
get_mock = MagicMock()
return_value = MagicMock()
return_value.text = "{\"programs\":{\"feed\":{\"brighsdftness\":\"blub\"}}}"
get_mock.return_value = return_value
requests.get = get_mock
logger = logging.getLogger('flicintegration')
with patch.object(logger, 'error') as log_mock:
from flicintegration import eventhelper
self.assertRaises(KeyError, lambda: eventhelper.get_dict_value_from_JSON_webservice(EventHelper.piLedHost,
"/getConfiguration",
["programs", "feed",
"brightness"]))
self.assertTrue(log_mock.called)
示例11: test_get_dict_value_from_JSON_webserviceCatchesAndLogsJSONException
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_get_dict_value_from_JSON_webserviceCatchesAndLogsJSONException(self):
get_mock = MagicMock()
return_value = MagicMock()
return_value.text = "{\"programs\":{\"feed\":{\"brightness\":\"blub\"}}}"
get_mock.return_value = return_value
requests.get = get_mock
loads_mock = MagicMock()
loads_mock.side_effect = ValueError("", "", 0)
logger = logging.getLogger('flicintegration')
with patch.object(logger, 'error') as log_mock, patch('json.loads', new=loads_mock):
from flicintegration import eventhelper
self.assertRaises(ValueError,
lambda: eventhelper.get_dict_value_from_JSON_webservice(EventHelper.piLedHost,
"/getConfiguration",
["programs", "feed",
"brightness"]))
self.assertTrue(log_mock.called)
示例12: test_docker_login_with_iid
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_docker_login_with_iid(monkeypatch, tmpdir):
monkeypatch.setattr('os.path.expanduser',
lambda x: x.replace('~', str(tmpdir)))
metaservice = MagicMock()
metaservice.text = '''TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNldGV0dXIgc2FkaXBzY2luZyBlbGl0ciwg
c2VkIGRpYW0gbm9udW15IGVpcm1vZCB0ZW1wb3IgaW52aWR1bnQgdXQgbGFib3JlIGV0IGRvbG9y
ZSBtYWduYSBhbGlxdXlhbSBlcmF0LCBzZWQgZGlhbSB2b2x1cHR1YS4gQXQgdmVybyBlb3MgZXQg
YWNjdXNhbSBldCBqdXN0byBkdW8gZG9sb3JlcyBldCBlYSByZWJ1bS4gU3RldCBjbGl0YSBrYXNk
IGd1YmVyZ3Jlbiwgbm8gc2VhIHRha2ltYXRhIHNhbmN0dXMgZXN0IExvcmVtIGlwc3VtIGRvbG9y
IHNpdCBhbWV0LiBMb3JlbSBpcHN1bSBkb2xvciBzaXQgYW1ldCwgY29uc2V0ZXR1ciBzYWRpcHNj
aW5nIGVsaXRyLCBzZWQgZGlhbSBub251bXkgZWlybW9kIHRlbXBvciBpbnZpZHVudCB1dCBsYWJv
cmUgZXQgZG9sb3JlIG1hZ25hIGFsaXF1eWFtIGVyYXQsIHNlZCBkaWFtIHZvbHVwdHVhLiBBdCB2
ZXJvIGVvcyBldCBhY2N1c2FtIGV0IGp1c3RvIGR1byBkb2xvcmVzIGV0IGVhIHJlYnVtLiBTdGV0
IGNsaXRhIGthc2QgZ3ViZXJncmVuLCBubyBzZWEgdGFraW1hdGEgc2FuY3R1cyBlc3QgTG9yZW0g
aXBzdW0gZG9sb3Igc2l0IGFtZXQuCg=='''
monkeypatch.setattr('pierone.api.request',
MagicMock(return_value=metaservice))
docker_login_with_iid('https://pierone.example.org')
path = os.path.expanduser('~/.docker/config.json')
with open(path) as fd:
data = yaml.safe_load(fd)
assert {'auth': 'aW5zdGFuY2UtaWRlbnRpdHktZG9jdW1lbnQ6VEc5eVpXMGdhWEJ6ZFcwZ1pHOXNiM0lnYzJsMElH'
'RnRaWFFzSUdOdmJuTmxkR1YwZFhJZ2MyRmthWEJ6WTJsdVp5QmxiR2wwY2l3ZwpjMlZrSUdScFlX'
'MGdibTl1ZFcxNUlHVnBjbTF2WkNCMFpXMXdiM0lnYVc1MmFXUjFiblFnZFhRZ2JHRmliM0psSUdW'
'MElHUnZiRzl5ClpTQnRZV2R1WVNCaGJHbHhkWGxoYlNCbGNtRjBMQ0J6WldRZ1pHbGhiU0IyYjJ4'
'MWNIUjFZUzRnUVhRZ2RtVnlieUJsYjNNZ1pYUWcKWVdOamRYTmhiU0JsZENCcWRYTjBieUJrZFc4'
'Z1pHOXNiM0psY3lCbGRDQmxZU0J5WldKMWJTNGdVM1JsZENCamJHbDBZU0JyWVhOawpJR2QxWW1W'
'eVozSmxiaXdnYm04Z2MyVmhJSFJoYTJsdFlYUmhJSE5oYm1OMGRYTWdaWE4wSUV4dmNtVnRJR2x3'
'YzNWdElHUnZiRzl5CklITnBkQ0JoYldWMExpQk1iM0psYlNCcGNITjFiU0JrYjJ4dmNpQnphWFFn'
'WVcxbGRDd2dZMjl1YzJWMFpYUjFjaUJ6WVdScGNITmoKYVc1bklHVnNhWFJ5TENCelpXUWdaR2xo'
'YlNCdWIyNTFiWGtnWldseWJXOWtJSFJsYlhCdmNpQnBiblpwWkhWdWRDQjFkQ0JzWVdKdgpjbVVn'
'WlhRZ1pHOXNiM0psSUcxaFoyNWhJR0ZzYVhGMWVXRnRJR1Z5WVhRc0lITmxaQ0JrYVdGdElIWnZi'
'SFZ3ZEhWaExpQkJkQ0IyClpYSnZJR1Z2Y3lCbGRDQmhZMk4xYzJGdElHVjBJR3AxYzNSdklHUjFi'
'eUJrYjJ4dmNtVnpJR1YwSUdWaElISmxZblZ0TGlCVGRHVjAKSUdOc2FYUmhJR3RoYzJRZ1ozVmla'
'WEpuY21WdUxDQnVieUJ6WldFZ2RHRnJhVzFoZEdFZ2MyRnVZM1IxY3lCbGMzUWdURzl5WlcwZwph'
'WEJ6ZFcwZ1pHOXNiM0lnYzJsMElHRnRaWFF1Q2c9PQ==',
'email': '[email protected]'} == data.get('auths').get('https://pierone.example.org')
示例13: valid_pierone_url
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def valid_pierone_url(monkeypatch):
response = MagicMock()
response.text = 'Pier One API'
monkeypatch.setattr('requests.get', lambda *args, **kw: response)
示例14: test_load_ws_token
# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import text [as 别名]
def test_load_ws_token(self):
mock = MagicMock()
mock.text = installed_js_fragment
with patch.object(requests, 'get', return_value=mock) as mock_requests:
self.assertEqual(get_ws_token(), expected_ws_token)