-
-
Notifications
You must be signed in to change notification settings - Fork 379
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
zulip: Add tests for API functions. #682
Open
LoopThrough-i-j
wants to merge
1
commit into
zulip:main
Choose a base branch
from
LoopThrough-i-j:test-suite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import json | ||
import unittest | ||
import responses | ||
import zulip | ||
import urllib | ||
|
||
from typing import Any, Tuple, Dict | ||
from unittest import TestCase | ||
|
||
class TerminationException(Exception): | ||
pass | ||
|
||
class TestAPI(TestCase): | ||
|
||
@responses.activate | ||
def test_add_reaction(self) -> None: | ||
def request_callback(request: Any) -> Tuple[int, Dict[str, str], str]: | ||
params = {} | ||
for param in request.body.split("&"): | ||
key, value = param.split("=") | ||
params[key] = urllib.parse.unquote(value) | ||
assert "emoji_name" in params or "emoji_code" in params | ||
return (200, {}, json.dumps({'result': 'success', 'msg': ''})) | ||
|
||
responses.add_callback( | ||
method=responses.POST, | ||
url="https://test.zulipapi.com/api/v1/messages/10/reactions", | ||
callback=request_callback | ||
) | ||
|
||
client = zulip.Client(config_file="zulip/tests/test_zuliprc") | ||
# request with emoji name | ||
request = { | ||
"message_id": 10, | ||
"emoji_name": "octopus", | ||
} | ||
result = client.add_reaction(request) | ||
self.assertEqual(result, {'result': 'success', 'msg': ''}) | ||
# request with emoji code | ||
request = { | ||
"message_id": 10, | ||
"emoji_code": "1f419", | ||
} | ||
result = client.add_reaction(request) | ||
self.assertEqual(result, {'result': 'success', 'msg': ''}) | ||
|
||
@responses.activate | ||
def test_call_on_each_event(self) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this test could benefit from a comment or docstring explaining what we're trying to test here. |
||
|
||
responses.add( | ||
responses.POST, | ||
url="https://test.zulipapi.com/api/v1/register", | ||
json={'queue_id': '123456789', 'last_event_id': -1, 'msg': '', 'result': 'success'}, | ||
status=200 | ||
) | ||
responses.add( | ||
responses.GET, | ||
url="https://test.zulipapi.com/api/v1/events", | ||
json={'result': 'success', 'msg': '', 'events': [{'id': 0}, {'id': 1}, {'id': 2}]}, | ||
status=200 | ||
) | ||
|
||
def event_callback(x: Dict[str, Any]) -> None: | ||
print(x) | ||
if x['id'] == 2: | ||
raise TerminationException() | ||
|
||
client = zulip.Client(config_file="zulip/tests/test_zuliprc") | ||
|
||
try: | ||
client.call_on_each_event( | ||
event_callback, | ||
) | ||
except TerminationException: | ||
pass | ||
|
||
try: | ||
client.call_on_each_event( | ||
event_callback, | ||
['message'], | ||
) | ||
except TerminationException: | ||
pass | ||
|
||
try: | ||
client.call_on_each_event( | ||
event_callback, | ||
['message'], | ||
[['some', 'narrow']] | ||
) | ||
except TerminationException: | ||
pass | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[api] | ||
[email protected] | ||
key=K1PZuAp18Cn9RFjTsf5O1HeRW6TVpyhF | ||
site=https://test.zulipapi.com |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this function -- why do we need this parsing logic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function needs some work as well as I understand. This test is primarily for testing whether the payload provided is properly parsed, the URL is correct, and all the It accepts and sends a proper response, as a part of the normal API workflow.
This test makes sure all intermediate functions for an API call like
do_api_query
andcall_endpoint
, work correctly.