Skip to content

Commit ecf48a1

Browse files
zulip: Add tests for API functions.
1 parent 4d482e0 commit ecf48a1

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

zulip/tests/test_api_functions.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import unittest
2+
import re
3+
import responses
4+
from zulip import Client
5+
6+
from unittest import TestCase
7+
8+
class TestClient(Client):
9+
def __init__(self, config_file: str) -> None:
10+
super().__init__(config_file=config_file)
11+
self.responses = responses.RequestsMock()
12+
self.responses.start()
13+
self.add_responses()
14+
15+
def add_responses(self) -> None:
16+
# For call_on_each_event
17+
self.responses.add(
18+
responses.POST,
19+
url="{}v1/register".format(self.base_url),
20+
json={'queue_id': 10, 'last_event_id': -1, 'msg': '', 'result': 'success'},
21+
status=200
22+
)
23+
self.responses.add(
24+
responses.GET,
25+
url="{}v1/events".format(self.base_url),
26+
json={'result': 'success', 'msg': '', 'events': [{'id': 123}]},
27+
status=200
28+
)
29+
# For add_reaction
30+
self.responses.add(
31+
responses.POST,
32+
url=re.compile("{}v1/messages/([0-9]*)/reactions".format(self.base_url)),
33+
json={'result': 'success', 'msg': ''},
34+
status=200
35+
)
36+
37+
class TerminationException(Exception):
38+
pass
39+
40+
class TestAPI(TestCase):
41+
def __init__(self, methodName: str) -> None:
42+
super().__init__(methodName=methodName)
43+
self.client = TestClient(config_file="zulip/tests/test_zuliprc")
44+
45+
def test_add_reaction(self) -> None:
46+
request = {
47+
"message_id": 59,
48+
"emoji_name": "octopus",
49+
}
50+
result = self.client.add_reaction(request)
51+
self.assertEqual(result, {'result': 'success', 'msg': ''})
52+
53+
def test_call_on_each_event(self) -> None:
54+
def terminate() -> None:
55+
raise TerminationException()
56+
try:
57+
self.client.call_on_each_event(
58+
lambda x: terminate(),
59+
['message'],
60+
)
61+
except TerminationException:
62+
pass
63+
try:
64+
self.client.call_on_each_event(
65+
lambda x: terminate(),
66+
)
67+
except TerminationException:
68+
pass
69+
70+
if __name__ == '__main__':
71+
unittest.main()

zulip/tests/test_zuliprc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[api]
2+
3+
key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF
4+
site=http://localhost:9991

0 commit comments

Comments
 (0)