Можно ли вызывать build_admins_message
в тестах, чтобы получить ожидаемый результат, который будет использоваться в фиктивном утверждении?
Реализация:
@slack_messages.on_pattern('(?i)^admins$')
def handle_admins_message(event, body, match):
team_id = event['team_id']
user_id = body['user']
message = build_admins_message(team_id, user_id)
Slack(team_id).send_message(user_id, **message)
Тесты:
class TestAdminsMessageHandler(TestCase):
def setUp(self):
team = SlackTeam.objects.create(team_id='TEAMID')
SlackUser.objects.create(team=team, user_id='USERID')
def tearDown(self):
SlackUser.objects.all().delete()
SlackTeam.objects.all().delete()
@mock.patch('slango.slack.Slack.send_message')
def test_correct_text(self, send_message_mock):
event = {
'team_id': 'TEAMID',
'event': {
'text': 'admins',
'user': 'USERID'
}
}
handle_admins_message(event, event['event'])
expected_message = build_admins_message('TEAMID', 'USERID')
send_message_mock.assert_called_with('USERID', **expected_message)
Реализация build_admins_message
:
def build_admins_message(team_id, user_id):
user = SlackUser.retrieve(team_id, user_id)
admins = SlackUser.objects.filter(
is_bot_admin=True, team__team_id=team_id).order_by(
'real_name', 'display_name')
attachments = []
if user.is_bot_admin:
attachments.append(build_admin_picker())
for admin in admins:
attachments.append(build_admin_item(user, admin))
attachments.append(build_admin_more())
return {
'text': "Here is users with admin rights:",
'attachments': attachments
}