Improved test setup

This commit is contained in:
Mads Marquart
2018-08-29 10:12:10 +02:00
parent a8ce44b109
commit f367bd2d0d
3 changed files with 55 additions and 63 deletions

View File

@@ -6,7 +6,7 @@ import pytest
from os import path from os import path
from fbchat.models import ThreadType, Message, Mention, EmojiSize, Sticker 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): def test_fetch_all_users(client):
@@ -19,19 +19,7 @@ def test_fetch_thread_list(client):
assert len(threads) == 2 assert len(threads) == 2
@pytest.mark.parametrize( @pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST)
"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)),
],
)
def test_fetch_message_emoji(client, emoji, emoji_size): def test_fetch_message_emoji(client, emoji, emoji_size):
mid = client.sendEmoji(emoji, emoji_size) mid = client.sendEmoji(emoji, emoji_size)
message, = client.fetchThreadMessages(limit=1) message, = client.fetchThreadMessages(limit=1)
@@ -53,13 +41,13 @@ def test_fetch_message_mentions(client):
assert vars(message.mentions[i]) == vars(m) assert vars(message.mentions[i]) == vars(m)
@pytest.mark.parametrize("sticker_id", ["767334476626295"]) @pytest.mark.parametrize("sticker", STICKER_LIST)
def test_fetch_message_sticker(client, sticker_id): def test_fetch_message_sticker(client, sticker):
mid = client.send(Message(sticker=Sticker(sticker_id))) mid = client.send(Message(sticker=sticker))
message, = client.fetchThreadMessages(limit=1) message, = client.fetchThreadMessages(limit=1)
assert subset(vars(message), uid=mid, author=client.uid) 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): def test_fetch_info(client1, group):

View File

@@ -5,20 +5,11 @@ from __future__ import unicode_literals
import pytest import pytest
from os import path from os import path
from fbchat.models import Message, Mention, EmojiSize, FBchatFacebookError, Sticker from fbchat.models import FBchatFacebookError, Message, Mention
from utils import subset from utils import subset, STICKER_LIST, EMOJI_LIST, TEXT_LIST
@pytest.mark.parametrize( @pytest.mark.parametrize("text", TEXT_LIST)
"text",
[
"test_send",
"😆",
"\\\n\t%?&'\"",
"ˁҭʚ¹Ʋջوװ՞ޱɣࠚԹБɑȑңКએ֭ʗыԈٌʼőԈ×௴nચϚࠖణٔє܅Ԇޑط",
"a" * 20000, # Maximum amount of characters you can send
],
)
def test_send_text(client, catch_event, compare, text): def test_send_text(client, catch_event, compare, text):
with catch_event("onMessage") as x: with catch_event("onMessage") as x:
mid = client.sendMessage(text) 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) assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid, text=text)
@pytest.mark.parametrize( @pytest.mark.parametrize("emoji, emoji_size", EMOJI_LIST)
"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)),
],
)
def test_send_emoji(client, catch_event, compare, emoji, emoji_size): def test_send_emoji(client, catch_event, compare, emoji, emoji_size):
with catch_event("onMessage") as x: with catch_event("onMessage") as x:
mid = client.sendEmoji(emoji, emoji_size) 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.fixture
@pytest.mark.parametrize("message", [Message("a" * 20001)]) def message_with_mentions(client, client2, thread):
def test_send_invalid(client, message):
client.send(message)
def test_send_mentions(client, client2, thread, catch_event, compare):
text = "Hi there @me, @other and @thread" text = "Hi there @me, @other and @thread"
mentions = [ mentions = [
dict(thread_id=client.uid, offset=9, length=3), dict(thread_id=client.uid, offset=9, length=3),
dict(thread_id=client2.uid, offset=14, length=6), dict(thread_id=client2.uid, offset=14, length=6),
dict(thread_id=thread["id"], offset=26, length=7), dict(thread_id=thread["id"], offset=26, length=7),
] ]
with catch_event("onMessage") as x: return Message(text, mentions=[Mention(**d) for d in mentions])
mid = client.send(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 # The mentions are not ordered by offset
for m in x.res["message_object"].mentions: 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( @pytest.mark.parametrize("sticker", STICKER_LIST)
"sticker_id", def test_send_sticker(client, catch_event, compare, sticker):
["767334476626295", pytest.mark.xfail("0", raises=FBchatFacebookError)],
)
def test_send_sticker(client, catch_event, compare, sticker_id):
with catch_event("onMessage") as x: 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 compare(x, mid=mid)
assert subset(vars(x.res["message_object"]), uid=mid, author=client.uid) 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( @pytest.mark.parametrize(

View File

@@ -5,17 +5,46 @@ from __future__ import unicode_literals
import threading import threading
import logging import logging
import six import six
import pytest
from os import environ from os import environ
from random import randrange from random import randrange
from contextlib import contextmanager from contextlib import contextmanager
from six import viewitems from six import viewitems
from fbchat import Client 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()) 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): class ClientThread(threading.Thread):
def __init__(self, client, *args, **kwargs): def __init__(self, client, *args, **kwargs):
self.client = client self.client = client