From f367bd2d0dcf3794661f062bdc66c3366e6eccc9 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 29 Aug 2018 10:12:10 +0200 Subject: [PATCH] Improved test setup --- tests/test_fetch.py | 24 +++++------------ tests/test_send.py | 63 ++++++++++++++------------------------------- tests/utils.py | 31 +++++++++++++++++++++- 3 files changed, 55 insertions(+), 63 deletions(-) diff --git a/tests/test_fetch.py b/tests/test_fetch.py index d755c5a..3ce870c 100644 --- a/tests/test_fetch.py +++ b/tests/test_fetch.py @@ -6,7 +6,7 @@ import pytest from os import path from fbchat.models import ThreadType, Message, Mention, EmojiSize, Sticker -from utils import subset +from utils import subset, STICKER_LIST, EMOJI_LIST def test_fetch_all_users(client): @@ -19,19 +19,7 @@ def test_fetch_thread_list(client): assert len(threads) == 2 -@pytest.mark.parametrize( - "emoji, emoji_size", - [ - ("😆", EmojiSize.SMALL), - ("😆", EmojiSize.MEDIUM), - ("😆", EmojiSize.LARGE), - # These fail because the emoji is made into a sticker - # This should be fixed - pytest.mark.xfail((None, EmojiSize.SMALL)), - pytest.mark.xfail((None, EmojiSize.MEDIUM)), - pytest.mark.xfail((None, EmojiSize.LARGE)), - ], -) +@pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST) def test_fetch_message_emoji(client, emoji, emoji_size): mid = client.sendEmoji(emoji, emoji_size) message, = client.fetchThreadMessages(limit=1) @@ -53,13 +41,13 @@ def test_fetch_message_mentions(client): assert vars(message.mentions[i]) == vars(m) -@pytest.mark.parametrize("sticker_id", ["767334476626295"]) -def test_fetch_message_sticker(client, sticker_id): - mid = client.send(Message(sticker=Sticker(sticker_id))) +@pytest.mark.parametrize("sticker", STICKER_LIST) +def test_fetch_message_sticker(client, sticker): + mid = client.send(Message(sticker=sticker)) message, = client.fetchThreadMessages(limit=1) assert subset(vars(message), uid=mid, author=client.uid) - assert subset(vars(message.sticker), uid=sticker_id) + assert subset(vars(message.sticker), uid=sticker.uid) def test_fetch_info(client1, group): diff --git a/tests/test_send.py b/tests/test_send.py index 82098d7..f957775 100644 --- a/tests/test_send.py +++ b/tests/test_send.py @@ -5,20 +5,11 @@ from __future__ import unicode_literals import pytest from os import path -from fbchat.models import Message, Mention, EmojiSize, FBchatFacebookError, Sticker -from utils import subset +from fbchat.models import FBchatFacebookError, Message, Mention +from utils import subset, STICKER_LIST, EMOJI_LIST, TEXT_LIST -@pytest.mark.parametrize( - "text", - [ - "test_send", - "😆", - "\\\n\t%?&'\"", - "ˁҭʚ¹Ʋջوװ՞ޱɣࠚԹБɑȑңКએ֭ʗыԈٌʼőԈ×௴nચϚࠖణٔє܅Ԇޑط", - "a" * 20000, # Maximum amount of characters you can send - ], -) +@pytest.mark.parametrize("text", TEXT_LIST) def test_send_text(client, catch_event, compare, text): with catch_event("onMessage") as x: mid = client.sendMessage(text) @@ -27,19 +18,7 @@ def test_send_text(client, catch_event, compare, text): assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid, text=text) -@pytest.mark.parametrize( - "emoji, emoji_size", - [ - ("😆", EmojiSize.SMALL), - ("😆", EmojiSize.MEDIUM), - ("😆", EmojiSize.LARGE), - # These fail because the emoji is made into a sticker - # This should be fixed - pytest.mark.xfail((None, EmojiSize.SMALL)), - pytest.mark.xfail((None, EmojiSize.MEDIUM)), - pytest.mark.xfail((None, EmojiSize.LARGE)), - ], -) +@pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST) def test_send_emoji(client, catch_event, compare, emoji, emoji_size): with catch_event("onMessage") as x: mid = client.sendEmoji(emoji, emoji_size) @@ -54,40 +33,36 @@ def test_send_emoji(client, catch_event, compare, emoji, emoji_size): ) -@pytest.mark.xfail(raises=FBchatFacebookError) -@pytest.mark.parametrize("message", [Message("a" * 20001)]) -def test_send_invalid(client, message): - client.send(message) - - -def test_send_mentions(client, client2, thread, catch_event, compare): +@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), ] - with catch_event("onMessage") as x: - mid = client.send(Message(text, mentions=[Mention(**d) for d in mentions])) + return Message(text, mentions=[Mention(**d) for d in mentions]) - assert compare(x, mid=mid, message=text) - assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid, text=text) + +def test_send_mentions(client, catch_event, compare, message_with_mentions): + with catch_event("onMessage") as x: + mid = client.send(message_with_mentions) + + assert compare(x, mid=mid, message=message_with_mentions.text) + assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid, text=message_with_mentions.text) # The mentions are not ordered by offset for m in x.res["message_object"].mentions: - assert vars(m) in mentions + assert vars(m) in [vars(x) for x in message_with_mentions.mentions] -@pytest.mark.parametrize( - "sticker_id", - ["767334476626295", pytest.mark.xfail("0", raises=FBchatFacebookError)], -) -def test_send_sticker(client, catch_event, compare, sticker_id): +@pytest.mark.parametrize("sticker", STICKER_LIST) +def test_send_sticker(client, catch_event, compare, sticker): with catch_event("onMessage") as x: - mid = client.send(Message(sticker=Sticker(sticker_id))) + mid = client.send(Message(sticker=sticker)) assert compare(x, mid=mid) assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid) - assert subset(vars(x.res["message_object"].sticker), uid=sticker_id) + assert subset(vars(x.res["message_object"].sticker), uid=sticker.uid) @pytest.mark.parametrize( diff --git a/tests/utils.py b/tests/utils.py index 0da1571..cbef21f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -5,17 +5,46 @@ from __future__ import unicode_literals import threading import logging import six +import pytest from os import environ from random import randrange from contextlib import contextmanager from six import viewitems from fbchat import Client -from fbchat.models import ThreadType +from fbchat.models import ThreadType, EmojiSize, FBchatFacebookError, Sticker log = logging.getLogger("fbchat.tests").addHandler(logging.NullHandler()) +EMOJI_LIST = [ + ("😆", EmojiSize.SMALL), + ("😆", EmojiSize.MEDIUM), + ("😆", EmojiSize.LARGE), + # These fail in `catch_event` because the emoji is made into a sticker + # This should be fixed + pytest.mark.xfail((None, EmojiSize.SMALL)), + pytest.mark.xfail((None, EmojiSize.MEDIUM)), + pytest.mark.xfail((None, EmojiSize.LARGE)), +] + +STICKER_LIST = [ + Sticker("767334476626295"), + pytest.mark.xfail(Sticker("0"), raises=FBchatFacebookError), + pytest.mark.xfail(Sticker(None), raises=FBchatFacebookError), +] + +TEXT_LIST = [ + "test_send", + "😆", + "\\\n\t%?&'\"", + "ˁҭʚ¹Ʋջوװ՞ޱɣࠚԹБɑȑңКએ֭ʗыԈٌʼőԈ×௴nચϚࠖణٔє܅Ԇޑط", + "a" * 20000, # Maximum amount of characters you can send + pytest.mark.xfail("a" * 20001, raises=FBchatFacebookError), + pytest.mark.xfail(None, raises=FBchatFacebookError), +] + + class ClientThread(threading.Thread): def __init__(self, client, *args, **kwargs): self.client = client