Skip to content

Commit

Permalink
User contains in group, can use '100500 in group_instance'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgaynetdinov committed Oct 11, 2019
1 parent 52820a3 commit 931ceab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='py-vkontakte',
version='5.69.1',
version='5.69.2',
packages=['vk'],
url='https://github.com/sgaynetdinov/py-vkontakte',
license='MIT License',
Expand Down
25 changes: 25 additions & 0 deletions tests/test_group.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import patch

import pytest

from vk.groups import Group
from vk.users import User


def test_detail(factory):
Expand All @@ -13,3 +16,25 @@ def test_detail(factory):
assert group.type == 'group'
assert group.is_trending == False


@pytest.mark.parametrize("user", [
"100500",
100500.0,
])
def test_contains_fail(factory, user):
group = Group.from_json(None, factory('group_detail.json'))

with pytest.raises(TypeError, match="is not `User`"):
user in group


@pytest.mark.parametrize("user", [
User.from_json(None, {"id": 100500}),
100500,
])
def test_contains(factory, user):
session = type("", (object,), {"fetch": lambda *args, **kw: True})
group = Group.from_json(session, factory('group_detail.json'))

assert user in group

6 changes: 4 additions & 2 deletions vk/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def __contains__(self, user_instance):
"""
https://vk.com/dev/groups.isMember
"""
if not isinstance(user_instance, User):
if not isinstance(user_instance, (int, User)):
raise TypeError("object {0} is not `User`".format(user_instance))

return bool(self._session.fetch("groups.isMember", group_id=self.id, user_id=user_instance.id))
user_id = user_instance.id if hasattr(user_instance, "id") else user_instance

return bool(self._session.fetch("groups.isMember", group_id=self.id, user_id=user_id))

0 comments on commit 931ceab

Please sign in to comment.