8000 Fix ChatMemberUpdated.difference by Bibo-Joshi · Pull Request #2947 · python-telegram-bot/python-telegram-bot · GitHub
[go: up one dir, main page]

Skip to content
8000

Fix ChatMemberUpdated.difference #2947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions telegram/_chatmemberupdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ def to_dict(self) -> JSONDict:

return data

def _get_attribute_difference(self, attribute: str) -> Tuple[object, object]:
try:
old = self.old_chat_member[attribute]
except KeyError:
old = None

try:
new = self.new_chat_member[attribute]
except KeyError:
new = None

return old, new

def difference(
self,
) -> Dict[
Expand Down Expand Up @@ -162,10 +175,7 @@ def difference(
# we can't directly use the values from old_dict ^ new_dict b/c that set is unordered
attributes = (entry[0] for entry in set(old_dict.items()) ^ set(new_dict.items()))

result = {
attribute: (self.old_chat_member[attribute], self.new_chat_member[attribute])
for attribute in attributes
}
result = {attribute: self._get_attribute_difference(attribute) for attribute in attributes}
if old_user_dict != new_user_dict:
result['user'] = (self.old_chat_member.user, self.new_chat_member.user)

Expand Down
20 changes: 19 additions & 1 deletion tests/test_chatmemberupdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
Chat,
ChatMemberUpdated,
ChatInviteLink,
ChatMemberOwner,
ChatMemberBanned,
)
from telegram._utils.datetime import to_timestamp

Expand Down Expand Up @@ -210,7 +212,7 @@ def test_difference_required(self, user, chat):
)
assert chat_member_updated.difference() == {'status': ('old_status', 'new_status')}

# We deliberately change an optional argument here to make sure that comparision doesn't
# We deliberately change an optional argument here to make sure that comparison doesn't
# just happens by id/required args
new_user = User(1, 'First name', False, last_name='last name')
new_chat_member.user = new_user
Expand Down Expand Up @@ -239,3 +241,19 @@ def test_difference_optionals(self, optional_attribute, user, chat):
chat, user, datetime.datetime.utcnow(), old_chat_member, new_chat_member
)
assert chat_member_updated.difference() == {optional_attribute: (old_value, new_value)}

def test_difference_different_classes(self, user, chat):
old_chat_member = ChatMemberOwner(user=user, is_anonymous=False)
new_chat_member = ChatMemberBanned(user=user, until_date=datetime.datetime(2021, 1, 1))
chat_member_updated = ChatMemberUpdated(
chat=chat,
from_user=user,
date=datetime.datetime.utcnow(),
old_chat_member=old_chat_member,
new_chat_member=new_chat_member,
)
diff = chat_member_updated.difference()
assert diff.pop('is_anonymous') == (False, None)
assert diff.pop('until_date') == (None, datetime.datetime(2021, 1, 1))
assert diff.pop('status') == (ChatMember.CREATOR, ChatMember.KICKED)
assert diff == {}
0