Python:使用Flask模拟补丁错误 [英] Python: Mock Patch Errors with Flask
问题描述
以下是我的Flask端点:
@ blueprint.route '/ mailing_finish /< account_id> /< sumall_stream_id> /',methods = ['POST'])
def mailing_finish(account_id,sumall_stream_id):
mail_id和更新数据集:
* MYEMMA_EMAIL_SENDS:response ['sent']
#TODO:webhook不会触发
data = json.loads(request.data)['data']
access_token = sumall_redis.get_oauth_token(account_id)
response_data = sumall_audience.get_response_data(
access_token = access_token ,
account_id = account_id,
mailing_id = data ['mailing_id'],
)
event = {
'timeStamp':data ['timestamp格式(
mailing_id = data ['mailing_id'],
),
'data。'] [3:],
'eventId':'mailing_id:{mailing_id} ':{
'MYEMMA_EMAIL_SENDS':{
'value':response_data ['recipient_count'],
'dimensions':[
{
'dimension':'MAILINGS',
'value':data ['mailing_id'],
},
],
},
},
}
status = sumall_api.post_stream_event(
stream_id = sumall_stream_id,
event = event,
)
返回'发送到SumAll的数据',状态
$ c
以下是我的测试:
@ mock.patch('sumall.utils.sumall_api')
@ mock.patch('sumall.utils.sumall_audience')
@ mock.patch('sumall.utils.sumall_redis')
def test_mailing_finish(self,sumall_redis_mock,sumall_audience_mock,sumall_api_mock):
sumall_redis_mock.get_oauth_token.return_value = self.access_token
sumall_audience_mock.get_response_data.return_value = {'recipient_count':2}
event = {$ b $'timeStamp':self.data ['timestamp'] [3:],
'格式(mailing_id = self.data ['mailing_id'],),
'data':{$ b $'EMAIL_SENDS':{
'value ':sumall_audience_mock.get_response_data.get('recipient_count'),
'dimensions':[
{
'dimension':'MAILINGS',
'value':self.data ['mailing_id'],
},
],
},
},
}
res = self.client.post (
/ webhooks / mailing_finish / {account_id} / {sumall_stream_id}'。format(
account_id = self.account_id,
sumall_stream_id = self.sumall_stream_id
),
da ta = json.dumps({'data':self.data}),
content_type ='application / json',
)
sumall_redis_mock.get_oauth_token.assert_called_with(self。 account_id)
sumall_audience_mock.get_response_data.assert_called_with(
access_token = self.access_token,
account_id = self.account_id,
mailing_id = self.data ['mailing_id']
)
sumall_api_mock.post_stream_event.assert_called_with(
stream_id = self.sumall_stream_id,
event = event,
)
我收到的错误是:
AssertionError:期望的调用:get_oauth_token('123456')
不叫
我是不知道什么是错的。任何帮助将不胜感激!谢谢!
解决方案 您在视图中导入了 sumall_redis
作为本地名称模块,但嘲笑原始 sumall.utils.sumall_redis
。
您可能在视图顶部
from sumall.utils import sumall_redis
这将该对象绑定到模块中的本地名称。当测试开始并应用修补程序时,只有 sumall.utils
模块中的原始 sumall_redis
对象将受到影响,而不是这个本地名称。
您需要模拟您的视图模块中绑定的名称:
@ mock.patch('view_module.sumall_redis')
这也适用于您的其他2个进口。
$ b
mock
文档包含关于要在哪里修补,您可能想要阅读。
I am a complete newb when it comes to writing Python, let alone testing it.
Here is my Flask endpoint:
@blueprint.route('/mailing_finish/<account_id>/<sumall_stream_id>/', methods=['POST'])
def mailing_finish(account_id, sumall_stream_id):
"""
Get Response for mailing_id and update Dataset:
* MYEMMA_EMAIL_SENDS: response['sent']
"""
# TODO: webhook does not fire
data = json.loads(request.data)['data']
access_token = sumall_redis.get_oauth_token(account_id)
response_data = sumall_audience.get_response_data(
access_token=access_token,
account_id=account_id,
mailing_id=data['mailing_id'],
)
event = {
'timeStamp': data['timestamp'][3:],
'eventId': 'mailing_id:{mailing_id}'.format(
mailing_id=data['mailing_id'],
),
'data': {
'MYEMMA_EMAIL_SENDS': {
'value': response_data['recipient_count'],
'dimensions': [
{
'dimension': 'MAILINGS',
'value': data['mailing_id'],
},
],
},
},
}
status = sumall_api.post_stream_event(
stream_id=sumall_stream_id,
event=event,
)
return 'Data sent to SumAll', status
Here is my test:
@mock.patch('sumall.utils.sumall_api')
@mock.patch('sumall.utils.sumall_audience')
@mock.patch('sumall.utils.sumall_redis')
def test_mailing_finish(self, sumall_redis_mock, sumall_audience_mock, sumall_api_mock):
sumall_redis_mock.get_oauth_token.return_value = self.access_token
sumall_audience_mock.get_response_data.return_value = {'recipient_count': 2}
event = {
'timeStamp': self.data['timestamp'][3:],
'eventId': 'mailing_id:{mailing_id}'.format(mailing_id=self.data['mailing_id'],),
'data': {
'EMAIL_SENDS': {
'value': sumall_audience_mock.get_response_data.get('recipient_count'),
'dimensions': [
{
'dimension': 'MAILINGS',
'value': self.data['mailing_id'],
},
],
},
},
}
res = self.client.post(
'/webhooks/mailing_finish/{account_id}/{sumall_stream_id}'.format(
account_id=self.account_id,
sumall_stream_id=self.sumall_stream_id
),
data=json.dumps({'data': self.data}),
content_type='application/json',
)
sumall_redis_mock.get_oauth_token.assert_called_with(self.account_id)
sumall_audience_mock.get_response_data.assert_called_with(
access_token=self.access_token,
account_id=self.account_id,
mailing_id=self.data['mailing_id']
)
sumall_api_mock.post_stream_event.assert_called_with(
stream_id=self.sumall_stream_id,
event=event,
)
The error I am receiving is:
AssertionError: Expected call: get_oauth_token('123456')
Not called
I am not sure what is wrong. Any help would be greatly appreciated! Thanks!
解决方案 You imported sumall_redis
as a local name in your view module, but mock the original sumall.utils.sumall_redis
.
You probably have this at the top of your view module:
from sumall.utils import sumall_redis
This binds that object to a local name in the module. When the test starts and the patch is applied, only the original sumall_redis
object in the sumall.utils
module will be affected, not this local name.
You'll need to mock the name bound in your view module instead:
@mock.patch('view_module.sumall_redis')
This applies to your other 2 imports as well.
The mock
documentation includes a guide on where to patch that you might want to read.
这篇关于Python:使用Flask模拟补丁错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文