在编写 Python 时,我是一个完全的新手,更不用说测试它了。
这是我的 Flask 端点:
@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
这是我的测试:
@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,
)
我收到的错误是:
AssertionError: Expected call: get_oauth_token('123456')
Not called
我不确定出了什么问题。任何帮助将不胜感激!谢谢!