Skip to content

Commit

Permalink
zulip: Add tests for API functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
LoopThrough-i-j committed Apr 29, 2021
1 parent 4d482e0 commit 076da5f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions zulip/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def recur_expand(target_root: Any, dir: Any) -> Generator[Tuple[str, List[str]],

setuptools_info = dict(
install_requires=['requests[security]>=0.12.1',
'responses',
'matrix_client',
'distro',
],
Expand Down
71 changes: 71 additions & 0 deletions zulip/tests/test_api_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import unittest
import re
import responses
from zulip import Client

from unittest import TestCase

class TestClient(Client):
def __init__(self, config_file: str) -> None:
super().__init__(config_file=config_file)
self.responses = responses.RequestsMock()
self.responses.start()
self.add_responses()

def add_responses(self) -> None:
# For call_on_each_event
self.responses.add(
responses.POST,
url="{}v1/register".format(self.base_url),
json={'queue_id': 10, 'last_event_id': -1, 'msg': '', 'result': 'success'},
status=200
)
self.responses.add(
responses.GET,
url="{}v1/events".format(self.base_url),
json={'result': 'success', 'msg': '', 'events': [{'id': 123}]},
status=200
)
# For add_reaction
self.responses.add(
responses.POST,
url=re.compile("{}v1/messages/([0-9]*)/reactions".format(self.base_url)),
json={'result': 'success', 'msg': ''},
status=200
)

class TerminationException(Exception):
pass

class TestAPI(TestCase):
def __init__(self, methodName: str) -> None:
super().__init__(methodName=methodName)
self.client = TestClient(config_file="zulip/tests/test_zuliprc")

def test_add_reaction(self) -> None:
request = {
"message_id": 59,
"emoji_name": "octopus",
}
result = self.client.add_reaction(request)
self.assertEqual(result, {'result': 'success', 'msg': ''})

def test_call_on_each_event(self) -> None:
def terminate() -> None:
raise TerminationException()
try:
self.client.call_on_each_event(
lambda x: terminate(),
['message'],
)
except TerminationException:
pass
try:
self.client.call_on_each_event(
lambda x: terminate(),
)
except TerminationException:
pass

if __name__ == '__main__':
unittest.main()
4 changes: 4 additions & 0 deletions zulip/tests/test_zuliprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[api]
[email protected]
key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF
site=http://localhost:9991

0 comments on commit 076da5f

Please sign in to comment.