diff --git a/setup.py b/setup.py index 4fe4839..656319f 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/test_group.py b/tests/test_group.py index 430ef69..c04df11 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -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): @@ -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 + diff --git a/vk/groups.py b/vk/groups.py index 370c342..2a3eb5b 100644 --- a/vk/groups.py +++ b/vk/groups.py @@ -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))