Merge pull request #499 from carpedm20/session-in-models

Add ThreadABC helper, and move a bunch of methods out of Client
This commit is contained in:
Mads Marquart
2020-01-09 11:33:45 +01:00
26 changed files with 1011 additions and 1642 deletions

View File

@@ -3,19 +3,24 @@ import json
from utils import *
from contextlib import contextmanager
from fbchat import ThreadType, Message, Mention
from fbchat import Message, Mention
@pytest.fixture(scope="session")
def session():
return object() # TODO: Add a mocked session
@pytest.fixture(scope="session")
def user(client2):
return {"id": client2.id, "type": ThreadType.USER}
return {"id": client2.id, "type": None}
@pytest.fixture(scope="session")
def group(pytestconfig):
return {
"id": load_variable("group_id", pytestconfig.cache),
"type": ThreadType.GROUP,
"type": None,
}
@@ -24,11 +29,9 @@ def group(pytestconfig):
params=["user", "group", pytest.param("none", marks=[pytest.mark.xfail()])],
)
def thread(request, user, group):
return {
"user": user,
"group": group,
"none": {"id": "0", "type": ThreadType.GROUP},
}[request.param]
return {"user": user, "group": group, "none": {"id": "0", "type": None},}[
request.param
]
@pytest.fixture(scope="session")
@@ -98,9 +101,7 @@ def compare(client, thread):
def inner(caught_event, **kwargs):
d = {
"author_id": client.id,
"thread_id": client.id
if thread["type"] == ThreadType.USER
else thread["id"],
"thread_id": client.id if thread["type"] == None else thread["id"],
"thread_type": thread["type"],
}
d.update(kwargs)

View File

@@ -1,7 +1,7 @@
import pytest
from os import path
from fbchat import ThreadType, Message, Mention, EmojiSize, Sticker
from fbchat import Message, Mention, EmojiSize, Sticker
from utils import subset, STICKER_LIST, EMOJI_LIST
pytestmark = pytest.mark.online
@@ -89,7 +89,6 @@ def test_fetch_info(client1, group):
assert info.name == "Mark Zuckerberg"
info = client1.fetch_group_info(group["id"])[group["id"]]
assert info.type == ThreadType.GROUP
def test_fetch_image_url(client):

View File

@@ -1,7 +1,7 @@
from fbchat._group import Group
def test_group_from_graphql():
def test_group_from_graphql(session):
data = {
"name": "Group ABC",
"thread_key": {"thread_fbid": "11223344"},
@@ -26,6 +26,7 @@ def test_group_from_graphql():
"event_reminders": {"nodes": []},
}
assert Group(
session=session,
id="11223344",
photo=None,
name="Group ABC",
@@ -40,4 +41,4 @@ def test_group_from_graphql():
approval_mode=False,
approval_requests=set(),
join_link="",
) == Group._from_graphql(data)
) == Group._from_graphql(session, data)

View File

@@ -2,7 +2,7 @@ import fbchat
from fbchat._page import Page
def test_page_from_graphql():
def test_page_from_graphql(session):
data = {
"id": "123456",
"name": "Some school",
@@ -12,10 +12,11 @@ def test_page_from_graphql():
"city": None,
}
assert Page(
session=session,
id="123456",
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
name="Some school",
url="https://www.facebook.com/some-school/",
city=None,
category="SCHOOL",
) == Page._from_graphql(data)
) == Page._from_graphql(session, data)

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Plan, FBchatFacebookError, ThreadType
from fbchat import Plan, FBchatFacebookError
from utils import random_hex, subset
from time import time
@@ -93,7 +93,7 @@ def test_on_plan_ended(client, thread, catch_event, compare):
x.wait(180)
assert subset(
x.res,
thread_id=client.id if thread["type"] == ThreadType.USER else thread["id"],
thread_id=client.id if thread["type"] is None else thread["id"],
thread_type=thread["type"],
)

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Poll, PollOption, ThreadType
from fbchat import Poll, PollOption
from utils import random_hex, subset
pytestmark = pytest.mark.online
@@ -49,12 +49,7 @@ def poll_data(request, client1, group, catch_event):
def test_create_poll(client1, group, catch_event, poll_data):
event, poll, _ = poll_data
assert subset(
event,
author_id=client1.id,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
assert subset(event, author_id=client1.id, thread=group)
assert subset(
vars(event["poll"]), title=poll.title, options_count=len(poll.options)
)
@@ -88,12 +83,7 @@ def test_update_poll_vote(client1, group, catch_event, poll_data):
new_options=new_options,
)
assert subset(
x.res,
author_id=client1.id,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
assert subset(x.res, author_id=client1.id, thread=group)
assert subset(
vars(x.res["poll"]), title=poll.title, options_count=len(options + new_options)
)

View File

@@ -1,5 +1,4 @@
import pytest
from fbchat import ThreadType
pytestmark = pytest.mark.online
@@ -11,7 +10,6 @@ def test_search_for(client1):
u = users[0]
assert u.id == "4"
assert u.type == ThreadType.USER
assert u.photo[:4] == "http"
assert u.url[:4] == "http"
assert u.name == "Mark Zuckerberg"

View File

@@ -1,12 +1,6 @@
import pytest
import fbchat
from fbchat._thread import ThreadType, ThreadColor, Thread
def test_thread_type_to_class():
assert fbchat.User == ThreadType.USER._to_class()
assert fbchat.Group == ThreadType.GROUP._to_class()
assert fbchat.Page == ThreadType.PAGE._to_class()
from fbchat import ThreadColor, ThreadABC, Thread
def test_thread_color_from_graphql():
@@ -19,8 +13,8 @@ def test_thread_color_from_graphql():
def test_thread_parse_customization_info_empty():
assert {} == Thread._parse_customization_info(None)
assert {} == Thread._parse_customization_info({"customization_info": None})
assert {} == ThreadABC._parse_customization_info(None)
assert {} == ThreadABC._parse_customization_info({"customization_info": None})
def test_thread_parse_customization_info_group():
@@ -43,7 +37,7 @@ def test_thread_parse_customization_info_group():
"color": ThreadColor.BRILLIANT_ROSE,
"nicknames": {"123456789": "A", "987654321": "B"},
}
assert expected == Thread._parse_customization_info(data)
assert expected == ThreadABC._parse_customization_info(data)
def test_thread_parse_customization_info_user():
@@ -62,4 +56,9 @@ def test_thread_parse_customization_info_user():
# ... Other irrelevant fields
}
expected = {"emoji": None, "color": None, "own_nickname": "A", "nickname": "B"}
assert expected == Thread._parse_customization_info(data)
assert expected == ThreadABC._parse_customization_info(data)
def test_thread_create_and_implements_thread_abc(session):
thread = Thread(session=session, id="123")
assert thread._parse_customization_info

View File

@@ -1,6 +1,6 @@
import pytest
from fbchat import Message, ThreadType, FBchatFacebookError, TypingStatus, ThreadColor
from fbchat import Message, FBchatFacebookError, TypingStatus, ThreadColor
from utils import random_hex, subset
from os import path
@@ -42,14 +42,8 @@ def test_remove_from_and_add_admins_to_group(client1, client2, group, catch_even
def test_change_title(client1, group, catch_event):
title = random_hex()
with catch_event("on_title_change") as x:
client1.change_thread_title(title, group["id"], thread_type=ThreadType.GROUP)
assert subset(
x.res,
author_id=client1.id,
new_title=title,
thread_id=group["id"],
thread_type=ThreadType.GROUP,
)
client1.change_thread_title(title, group["id"])
assert subset(x.res, author_id=client1.id, new_title=title, thread=group)
def test_change_nickname(client, client_all, catch_event, compare):

View File

@@ -4,7 +4,7 @@ import fbchat
from fbchat._user import User, ActiveStatus
def test_user_from_graphql():
def test_user_from_graphql(session):
data = {
"id": "1234",
"name": "Abc Def Ghi",
@@ -17,6 +17,7 @@ def test_user_from_graphql():
"viewer_affinity": 0.4560002,
}
assert User(
session=session,
id="1234",
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
name="Abc Def Ghi",
@@ -26,10 +27,10 @@ def test_user_from_graphql():
is_friend=True,
gender="female_singular",
affinity=0.4560002,
) == User._from_graphql(data)
) == User._from_graphql(session, data)
def test_user_from_thread_fetch():
def test_user_from_thread_fetch(session):
data = {
"thread_key": {"thread_fbid": None, "other_user_id": "1234"},
"name": None,
@@ -138,6 +139,7 @@ def test_user_from_thread_fetch():
"delivery_receipts": ...,
}
assert User(
session=session,
id="1234",
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
name="Abc Def Ghi",
@@ -152,10 +154,10 @@ def test_user_from_thread_fetch():
own_nickname="B",
color=None,
emoji=None,
) == User._from_thread_fetch(data)
) == User._from_thread_fetch(session, data)
def test_user_from_all_fetch():
def test_user_from_all_fetch(session):
data = {
"id": "1234",
"name": "Abc Def Ghi",
@@ -176,6 +178,7 @@ def test_user_from_all_fetch():
"is_blocked": False,
}
assert User(
session=session,
id="1234",
photo=fbchat.Image(url="https://scontent-arn2-1.xx.fbcdn.net/v/..."),
name="Abc Def Ghi",
@@ -183,7 +186,7 @@ def test_user_from_all_fetch():
first_name="Abc",
is_friend=True,
gender="female_singular",
) == User._from_all_fetch(data)
) == User._from_all_fetch(session, data)
@pytest.mark.skip(reason="can't gather test data, the pulling is broken")

View File

@@ -5,7 +5,7 @@ import pytest
from os import environ
from random import randrange
from contextlib import contextmanager
from fbchat import ThreadType, EmojiSize, FBchatFacebookError, Sticker, Client
from fbchat import EmojiSize, FBchatFacebookError, Sticker, Client
log = logging.getLogger("fbchat.tests").addHandler(logging.NullHandler())