Skip to content

Commit e8b4bb1

Browse files
kutercarltongibson
authored andcommitted
Added tests for generateschema management command. (#6442)
1 parent 65f5c11 commit e8b4bb1

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

tests/test_generateschema.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from __future__ import unicode_literals
2+
3+
import pytest
4+
from django.conf.urls import url
5+
from django.core.management import call_command
6+
from django.test import TestCase
7+
from django.test.utils import override_settings
8+
from django.utils import six
9+
10+
from rest_framework.compat import coreapi
11+
from rest_framework.utils import formatting, json
12+
from rest_framework.views import APIView
13+
14+
15+
class FooView(APIView):
16+
def get(self, request):
17+
pass
18+
19+
20+
urlpatterns = [
21+
url(r'^$', FooView.as_view())
22+
]
23+
24+
25+
@override_settings(ROOT_URLCONF='tests.test_generateschema')
26+
@pytest.mark.skipif(not coreapi, reason='coreapi is not installed')
27+
class GenerateSchemaTests(TestCase):
28+
"""Tests for management command generateschema."""
29+
30+
def setUp(self):
31+
self.out = six.StringIO()
32+
33+
@pytest.mark.skipif(six.PY2, reason='PyYAML unicode output is malformed on PY2.')
34+
def test_renders_default_schema_with_custom_title_url_and_description(self):
35+
expected_out = """info:
36+
description: Sample description
37+
title: SampleAPI
38+
version: ''
39+
openapi: 3.0.0
40+
paths:
41+
/:
42+
get:
43+
operationId: list
44+
servers:
45+
- url: http://api.sample.com/
46+
"""
47+
call_command('generateschema',
48+
'--title=SampleAPI',
49+
'--url=http://api.sample.com',
50+
'--description=Sample description',
51+
stdout=self.out)
52+
53+
self.assertIn(formatting.dedent(expected_out), self.out.getvalue())
54+
55+
def test_renders_openapi_json_schema(self):
56+
expected_out = {
57+
"openapi": "3.0.0",
58+
"info": {
59+
"version": "",
60+
"title": "",
61+
"description": ""
62+
},
63+
"servers": [
64+
{
65+
"url": ""
66+
}
67+
],
68+
"paths": {
69+
"/": {
70+
"get": {
71+
"operationId": "list"
72+
}
73+
}
74+
}
75+
}
76+
call_command('generateschema',
77+
'--format=openapi-json',
78+
stdout=self.out)
79+
out_json = json.loads(self.out.getvalue())
80+
81+
self.assertDictEqual(out_json, expected_out)
82+
83+
def test_renders_corejson_schema(self):
84+
expected_out = """{"_type":"document","":{"list":{"_type":"link","url":"/","action":"get"}}}"""
85+
call_command('generateschema',
86+
'--format=corejson',
87+
stdout=self.out)
88+
self.assertIn(expected_out, self.out.getvalue())

0 commit comments

Comments
 (0)