From ead7203e40e756ab9eef9e209f5bbe093fc0728f Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 29 Aug 2018 11:03:46 +0200 Subject: [PATCH] Added tests for `fetchMessageInfo` --- tests/conftest.py | 20 +++++++++++++++++++- tests/test_fetch.py | 41 ++++++++++++++++++++++++++++++++++------- tests/test_send.py | 11 ----------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 01cdf4e..7c3025c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,7 @@ import json from utils import * from contextlib import contextmanager -from fbchat.models import ThreadType +from fbchat.models import ThreadType, Message, Mention @pytest.fixture(scope="session") @@ -99,3 +99,21 @@ def compare(client, thread): return subset(caught_event.res, **d) return inner + + +@pytest.fixture(params=["me", "other", "me other"]) +def message_with_mentions(request, client, client2, group): + text = "Hi there [" + mentions = [] + if 'me' in request.param: + mentions.append(Mention(thread_id=client.uid, offset=len(text), length=2)) + text += "me, " + if 'other' in request.param: + mentions.append(Mention(thread_id=client2.uid, offset=len(text), length=5)) + text += "other, " + # Unused, because Facebook don't properly support sending mentions with groups as targets + if 'group' in request.param: + mentions.append(Mention(thread_id=group["id"], offset=len(text), length=5)) + text += "group, " + text += "nothing]" + return Message(text, mentions=mentions) diff --git a/tests/test_fetch.py b/tests/test_fetch.py index 04fe26a..a08ab07 100644 --- a/tests/test_fetch.py +++ b/tests/test_fetch.py @@ -29,16 +29,34 @@ def test_fetch_message_emoji(client, emoji, emoji_size): ) -def test_fetch_message_mentions(client): - text = "This is a test of fetchThreadMessages" - mentions = [Mention(client.uid, offset=10, length=4)] +@pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST) +def test_fetch_message_info_emoji(client, thread, emoji, emoji_size): + mid = client.sendEmoji(emoji, emoji_size) + message = client.fetchMessageInfo(mid, thread_id=thread["id"]) - mid = client.send(Message(text, mentions=mentions)) + assert subset( + vars(message), uid=mid, author=client.uid, text=emoji, emoji_size=emoji_size + ) + + +def test_fetch_message_mentions(client, thread, message_with_mentions): + mid = client.send(message_with_mentions) message, = client.fetchThreadMessages(limit=1) - assert subset(vars(message), uid=mid, author=client.uid, text=text) - for i, m in enumerate(mentions): - assert vars(message.mentions[i]) == vars(m) + assert subset(vars(message), uid=mid, author=client.uid, text=message_with_mentions.text) + # The mentions are not ordered by offset + for m in message.mentions: + assert vars(m) in [vars(x) for x in message_with_mentions.mentions] + + +def test_fetch_message_info_mentions(client, thread, message_with_mentions): + mid = client.send(message_with_mentions) + message = client.fetchMessageInfo(mid, thread_id=thread["id"]) + + assert subset(vars(message), uid=mid, author=client.uid, text=message_with_mentions.text) + # The mentions are not ordered by offset + for m in message.mentions: + assert vars(m) in [vars(x) for x in message_with_mentions.mentions] @pytest.mark.parametrize("sticker", STICKER_LIST) @@ -50,6 +68,15 @@ def test_fetch_message_sticker(client, sticker): assert subset(vars(message.sticker), uid=sticker.uid) +@pytest.mark.parametrize("sticker", STICKER_LIST) +def test_fetch_message_info_sticker(client, thread, sticker): + mid = client.send(Message(sticker=sticker)) + message = client.fetchMessageInfo(mid, thread_id=thread["id"]) + + assert subset(vars(message), uid=mid, author=client.uid) + assert subset(vars(message.sticker), uid=sticker.uid) + + def test_fetch_info(client1, group): info = client1.fetchUserInfo("4")["4"] assert info.name == "Mark Zuckerberg" diff --git a/tests/test_send.py b/tests/test_send.py index 92b986c..acf42f5 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -33,17 +33,6 @@ def test_send_emoji(client, catch_event, compare, emoji, emoji_size): ) -@pytest.fixture -def message_with_mentions(client, client2, thread): - text = "Hi there @me, @other and @thread" - mentions = [ - dict(thread_id=client.uid, offset=9, length=3), - dict(thread_id=client2.uid, offset=14, length=6), - dict(thread_id=thread["id"], offset=26, length=7), - ] - return Message(text, mentions=[Mention(**d) for d in mentions]) - - def test_send_mentions(client, catch_event, compare, message_with_mentions): with catch_event("onMessage") as x: mid = client.send(message_with_mentions)