mirror of https://github.com/zulip/zulip.git
api/render-message: Make code examples and fixtures testable.
This commit uses the Markdown extension defined in zerver/lib/bugdown/api_generate_example to generate the example fixture and code example, so that both are tested in tools/lib/api_tests.
This commit is contained in:
parent
c89550cfa0
commit
a1a69a0ac2
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"render-message":{
|
||||
"msg":"",
|
||||
"rendered":"<p><strong>foo<\/strong><\/p>",
|
||||
"result":"success"
|
||||
}
|
||||
}
|
|
@ -24,17 +24,9 @@ curl {{ api_url }}/v1/messages/render \
|
|||
</div>
|
||||
|
||||
<div data-language="python" markdown="1">
|
||||
```python
|
||||
#!/usr/bin/env python
|
||||
|
||||
import zulip
|
||||
{generate_code_example|render-message|method}
|
||||
|
||||
# Download ~/zuliprc-dev from your dev server
|
||||
client = zulip.Client(config_file="~/zuliprc-dev")
|
||||
|
||||
# Render a message
|
||||
print(client.render_message({"content": "**foo**"}))
|
||||
```
|
||||
</div>
|
||||
|
||||
<div data-language="javascript" markdown="1">
|
||||
|
@ -76,13 +68,7 @@ zulip(config).then((client) => {
|
|||
|
||||
A typical successful JSON response may look like:
|
||||
|
||||
```
|
||||
{
|
||||
'result':'success',
|
||||
'msg':'',
|
||||
'rendered':'<p><strong>foo</strong></p>'
|
||||
}
|
||||
```
|
||||
{generate_code_example|render-message|fixture}
|
||||
|
||||
A typical JSON response for when the required argument `content`
|
||||
is not supplied:
|
||||
|
|
|
@ -1,7 +1,21 @@
|
|||
from typing import Dict, Any, Optional, Iterable
|
||||
import os
|
||||
import ujson
|
||||
|
||||
if False:
|
||||
from zulip import Client
|
||||
|
||||
ZULIP_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
FIXTURE_PATH = os.path.join(ZULIP_DIR, 'templates', 'zerver', 'api', 'fixtures.json')
|
||||
|
||||
def load_api_fixtures():
|
||||
# type: () -> Dict[str, Any]
|
||||
with open(FIXTURE_PATH, 'r') as fp:
|
||||
json_dict = ujson.loads(fp.read())
|
||||
return json_dict
|
||||
|
||||
FIXTURES = load_api_fixtures()
|
||||
|
||||
def add_subscriptions(client):
|
||||
# type: (Client) -> None
|
||||
|
||||
|
@ -96,10 +110,16 @@ def remove_subscriptions(client):
|
|||
def render_message(client):
|
||||
# type: (Client) -> None
|
||||
|
||||
request = dict(content='**foo**')
|
||||
# {code_example|start}
|
||||
# Render a message
|
||||
request = {
|
||||
'content': '**foo**'
|
||||
}
|
||||
result = client.render_message(request)
|
||||
assert result['result'] == 'success'
|
||||
assert result['rendered'] == '<p><strong>foo</strong></p>'
|
||||
# {code_example|end}
|
||||
|
||||
fixture = FIXTURES['render-message']
|
||||
test_against_fixture(result, fixture)
|
||||
|
||||
def send_message(client):
|
||||
# type: (Client) -> int
|
||||
|
@ -145,8 +165,26 @@ def update_message(client, message_id):
|
|||
assert result['result'] == 'success'
|
||||
assert result['raw_content'] == 'new content'
|
||||
|
||||
TEST_FUNCTIONS = {
|
||||
'render-message': render_message,
|
||||
}
|
||||
|
||||
# SETUP METHODS FOLLOW
|
||||
|
||||
def test_against_fixture(result, fixture, check_if_equal=[], check_if_exists=[]):
|
||||
# type: (Dict[str, Any], Dict[str, Any], Optional[Iterable[str]], Optional[Iterable[str]]) -> None
|
||||
if not check_if_equal and not check_if_exists:
|
||||
for key, value in fixture.items():
|
||||
assert result[key] == fixture[key]
|
||||
|
||||
if check_if_equal:
|
||||
for key in check_if_equal:
|
||||
assert result[key] == fixture[key]
|
||||
|
||||
if check_if_exists:
|
||||
for key in check_if_exists:
|
||||
assert key in result
|
||||
|
||||
def test_messages(client):
|
||||
# type: (Client) -> None
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ from django.utils.safestring import mark_safe
|
|||
|
||||
import zerver.lib.bugdown.fenced_code
|
||||
import zerver.lib.bugdown.api_arguments_table_generator
|
||||
import zerver.lib.bugdown.api_code_examples
|
||||
|
||||
register = Library()
|
||||
|
||||
|
@ -86,6 +87,7 @@ def render_markdown_path(markdown_file_path, context=None):
|
|||
zerver.lib.bugdown.fenced_code.makeExtension(),
|
||||
zerver.lib.bugdown.api_arguments_table_generator.makeExtension(
|
||||
base_path='templates/zerver/api/'),
|
||||
zerver.lib.bugdown.api_code_examples.makeExtension(),
|
||||
]
|
||||
if md_macro_extension is None:
|
||||
md_macro_extension = markdown_include.include.makeExtension(
|
||||
|
|
Loading…
Reference in New Issue