From 73a3755120287f2931988b382801c238540eaf36 Mon Sep 17 00:00:00 2001 From: Eeshan Garg Date: Wed, 31 Jan 2018 00:34:43 -0330 Subject: [PATCH] api/get-all-streams: Make code examples/fixtures testable. This commit uses the Markdown extension defined in zerver/lib/bugdown/api_generate_examples to generate the example fixture and code example, so that both are tested in zerver/lib/api_test_helpers. --- templates/zerver/api/fixtures.json | 42 +++++++++++++++++++ templates/zerver/api/get-all-streams.md | 55 +------------------------ zerver/lib/api_test_helpers.py | 15 ++++++- 3 files changed, 58 insertions(+), 54 deletions(-) diff --git a/templates/zerver/api/fixtures.json b/templates/zerver/api/fixtures.json index 8c61a637e5..dce28592f7 100644 --- a/templates/zerver/api/fixtures.json +++ b/templates/zerver/api/fixtures.json @@ -105,5 +105,47 @@ ] } ] + }, + "get-all-streams": { + "msg":"", + "result":"success", + "streams":[ + { + "description":"A Scandinavian country", + "invite_only":false, + "name":"Denmark", + "stream_id":1 + }, + { + "description":"Yet another Italian city", + "invite_only":false, + "name":"Rome", + "stream_id":2 + }, + { + "description":"Located in the United Kingdom", + "invite_only":false, + "name":"Scotland", + "stream_id":3 + }, + { + "description":"A northeastern Italian city", + "invite_only":false, + "name":"Venice", + "stream_id":4 + }, + { + "description":"A city in Italy", + "invite_only":false, + "name":"Verona", + "stream_id":5 + }, + { + "description":"New stream for testing", + "invite_only":false, + "name":"new stream", + "stream_id":6 + } + ] } } diff --git a/templates/zerver/api/get-all-streams.md b/templates/zerver/api/get-all-streams.md index fe6d369e8e..a935132367 100644 --- a/templates/zerver/api/get-all-streams.md +++ b/templates/zerver/api/get-all-streams.md @@ -31,21 +31,7 @@ curl {{ api_url }}/v1/streams?include_public=false \
-``` -#!/usr/bin/env python - -import zulip - -# Download ~/zuliprc-dev from your dev server -client = zulip.Client(config_file="~/zuliprc-dev") - -# Get all streams that the user has access to -print(client.get_streams()) - -# You may pass in one or more of the query parameters mentioned above -# as keyword arguments, like so: -print(client.get_streams(include_public=False)) -``` +{generate_code_example|get-all-streams|method}
@@ -91,44 +77,7 @@ zulip(config).then((client) => { A typical successful JSON response may look like: -``` -{ - 'result':'success', - 'streams':[ - { - 'stream_id':15, - 'name':'Denmark', - 'invite_only':False, - 'description':'A Scandinavian country' - }, - { - 'stream_id':16, - 'name':'Rome', - 'invite_only':False, - 'description':'Yet another Italian city' - }, - { - 'stream_id':17, - 'name':'Scotland', - 'invite_only':False, - 'description':'Located in the United Kingdom' - }, - { - 'stream_id':18, - 'name':'Venice', - 'invite_only':False, - 'description':'A northeastern Italian city' - }, - { - 'stream_id':19, - 'name':'Verona', - 'invite_only':False, - 'description':'A city in Italy' - } - ], - 'msg':'' -} -``` +{generate_code_example|get-all-streams|fixture} An example of a JSON response for when the user is not authorized to use the `include_all_active` parameter: diff --git a/zerver/lib/api_test_helpers.py b/zerver/lib/api_test_helpers.py index 8235889593..8be6127f95 100644 --- a/zerver/lib/api_test_helpers.py +++ b/zerver/lib/api_test_helpers.py @@ -76,8 +76,20 @@ def get_stream_id(client): def get_streams(client): # type: (Client) -> None + # {code_example|start} + # Get all streams that the user has access to result = client.get_streams() - assert result['result'] == 'success' + + # You may pass in one or more of the query parameters mentioned above + # as keyword arguments, like so: + result = client.get_streams(include_public=True) + # {code_example|end} + + fixture = FIXTURES['get-all-streams'] + test_against_fixture(result, fixture, check_if_equal=['msg', 'result'], + check_if_exists=['streams']) + assert len(result['streams']) == len(fixture['streams']) + streams = [s for s in result['streams'] if s['name'] == 'new stream'] assert streams[0]['description'] == 'New stream for testing' @@ -226,6 +238,7 @@ TEST_FUNCTIONS = { 'update-message': update_message, 'get-stream-id': get_stream_id, 'get-subscribed-streams': list_subscriptions, + 'get-all-streams': get_streams, } # SETUP METHODS FOLLOW