Skip to content
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

test: Add tests for name, status, status message callbacks. #108

Merged
merged 1 commit into from
Feb 24, 2024
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
1 change: 1 addition & 0 deletions pytox/toxcore/tox.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class Tox_Ptr:
def friend_exists(self, friend_number: Tox_Friend_Number) -> bool: ...
def friend_get_connection_status(self, friend_number: Tox_Friend_Number) -> Tox_Connection: ...
def friend_get_last_online(self, friend_number: Tox_Friend_Number) -> int: ...
def friend_get_name(self, friend_number: Tox_Friend_Number) -> bytes: ...
def friend_get_public_key(self, friend_number: Tox_Friend_Number) -> bytes: ...
def friend_get_status(self, friend_number: Tox_Friend_Number) -> Tox_User_Status: ...
def friend_get_status_message(self, friend_number: Tox_Friend_Number) -> bytes: ...
Expand Down
14 changes: 14 additions & 0 deletions pytox/toxcore/tox.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,20 @@ cdef class Tox_Ptr:
finally:
free(data)

def friend_get_name(self, friend_number: Tox_Friend_Number) -> bytes:
cdef Tox_Err_Friend_Query err = TOX_ERR_FRIEND_QUERY_OK
cdef size_t size = tox_friend_get_name_size(self._get(), friend_number, &err)
if err:
raise ApiException(Tox_Err_Friend_Query(err))
cdef uint8_t *data = <uint8_t*> malloc(size * sizeof(uint8_t))
try:
tox_friend_get_name(self._get(), friend_number, data, &err)
if err:
raise ApiException(Tox_Err_Friend_Query(err))
return data[:size]
finally:
free(data)

def friend_get_connection_status(self, friend_number: Tox_Friend_Number) -> Tox_Connection:
cdef Tox_Err_Friend_Query err = TOX_ERR_FRIEND_QUERY_OK
cdef Tox_Connection res = tox_friend_get_connection_status(self._get(), friend_number, &err)
Expand Down
54 changes: 54 additions & 0 deletions test/auto_tests/self_connection_status_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class TestException(Exception):
@dataclass
class FriendInfo:
connection_status: core.Tox_Connection = core.TOX_CONNECTION_NONE
status: core.Tox_User_Status = core.TOX_USER_STATUS_NONE
status_message: bytes = b""
name: bytes = b""
request_message: bytes = b""
messages: list[tuple[core.Tox_Message_Type, bytes]] = field(
default_factory=list)
Expand Down Expand Up @@ -64,6 +67,17 @@ def handle_friend_lossless_packet(self, friend_number: int,
message: bytes) -> None:
self.friends[friend_number].lossless_packets.append(message)

def handle_friend_name(self, friend_number: int, name: bytes) -> None:
self.friends[friend_number].name = name

def handle_friend_status(self, friend_number: int,
status: core.Tox_User_Status) -> None:
self.friends[friend_number].status = status

def handle_friend_status_message(self, friend_number: int,
status_message: bytes) -> None:
self.friends[friend_number].status_message = status_message


class AutoTest(unittest.TestCase):

Expand Down Expand Up @@ -169,6 +183,46 @@ def test_send_lossless_packet(self) -> None:
self._iterate(100, lambda: not friend.lossless_packets)
self.assertEqual(friend.lossless_packets[0], b"\xa0general kenobi.")

def test_status(self) -> None:
self._wait_for_friend_online()
self.assertEqual(self.tox1.status, core.TOX_USER_STATUS_NONE)
self.tox1.status = core.TOX_USER_STATUS_AWAY
self.assertEqual(self.tox1.status, core.TOX_USER_STATUS_AWAY)
friend_number = self.tox2.friend_by_public_key(self.tox1.public_key)
friend = self.tox2.friends[friend_number]
self._iterate(100, lambda: friend.status == core.TOX_USER_STATUS_NONE)
self.assertEqual(friend.status, core.TOX_USER_STATUS_AWAY)
self.assertEqual(self.tox2.friend_get_status(friend_number),
core.TOX_USER_STATUS_AWAY)

def test_name(self) -> None:
self._wait_for_friend_online()
self.assertEqual(self.tox1.name, b"")
self.tox1.name = b"Now that's a name I haven't heard in a long time"
self.assertEqual(self.tox1.name,
b"Now that's a name I haven't heard in a long time")
friend_number = self.tox2.friend_by_public_key(self.tox1.public_key)
friend = self.tox2.friends[friend_number]
self._iterate(100, lambda: friend.name == b"")
self.assertEqual(friend.name,
b"Now that's a name I haven't heard in a long time")
self.assertEqual(
self.tox2.friend_get_name(friend_number),
b"Now that's a name I haven't heard in a long time",
)

def test_status_message(self) -> None:
self._wait_for_friend_online()
self.assertEqual(self.tox1.status_message, b"")
self.tox1.status_message = b"Python rocks!"
self.assertEqual(self.tox1.status_message, b"Python rocks!")
friend_number = self.tox2.friend_by_public_key(self.tox1.public_key)
friend = self.tox2.friends[friend_number]
self._iterate(100, lambda: friend.status_message == b"")
self.assertEqual(friend.status_message, b"Python rocks!")
self.assertEqual(self.tox2.friend_get_status_message(friend_number),
b"Python rocks!")


if __name__ == "__main__":
unittest.main()