Added tests for fetchMessageInfo

This commit is contained in:
Mads Marquart
2018-08-29 11:03:46 +02:00
parent bd2b947255
commit ead7203e40
3 changed files with 53 additions and 19 deletions

View File

@@ -7,7 +7,7 @@ import json
from utils import * from utils import *
from contextlib import contextmanager from contextlib import contextmanager
from fbchat.models import ThreadType from fbchat.models import ThreadType, Message, Mention
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
@@ -99,3 +99,21 @@ def compare(client, thread):
return subset(caught_event.res, **d) return subset(caught_event.res, **d)
return inner 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)

View File

@@ -29,16 +29,34 @@ def test_fetch_message_emoji(client, emoji, emoji_size):
) )
def test_fetch_message_mentions(client): @pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST)
text = "This is a test of fetchThreadMessages" def test_fetch_message_info_emoji(client, thread, emoji, emoji_size):
mentions = [Mention(client.uid, offset=10, length=4)] 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) message, = client.fetchThreadMessages(limit=1)
assert subset(vars(message), uid=mid, author=client.uid, text=text) assert subset(vars(message), uid=mid, author=client.uid, text=message_with_mentions.text)
for i, m in enumerate(mentions): # The mentions are not ordered by offset
assert vars(message.mentions[i]) == vars(m) 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) @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) 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): def test_fetch_info(client1, group):
info = client1.fetchUserInfo("4")["4"] info = client1.fetchUserInfo("4")["4"]
assert info.name == "Mark Zuckerberg" assert info.name == "Mark Zuckerberg"

View File

@@ -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): def test_send_mentions(client, catch_event, compare, message_with_mentions):
with catch_event("onMessage") as x: with catch_event("onMessage") as x:
mid = client.send(message_with_mentions) mid = client.send(message_with_mentions)