committed by
Cristi Vîjdea
parent
9f14114520
commit
1f190744cd
+18
-1
@@ -4,12 +4,13 @@ import os
|
||||
from collections import OrderedDict
|
||||
|
||||
import pytest
|
||||
from datadiff.tools import assert_equal
|
||||
from django.contrib.auth.models import User
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from drf_yasg import openapi, codecs
|
||||
from drf_yasg.codecs import yaml_sane_load
|
||||
from drf_yasg.codecs import yaml_sane_load, yaml_sane_dump
|
||||
from drf_yasg.generators import OpenAPISchemaGenerator
|
||||
|
||||
|
||||
@@ -63,6 +64,22 @@ def validate_schema(db):
|
||||
return validate_schema
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def compare_schemas():
|
||||
def compare_schemas(schema1, schema2):
|
||||
schema1 = OrderedDict(schema1)
|
||||
schema2 = OrderedDict(schema2)
|
||||
ignore = ['info', 'host', 'schemes', 'basePath', 'securityDefinitions']
|
||||
for attr in ignore:
|
||||
schema1.pop(attr, None)
|
||||
schema2.pop(attr, None)
|
||||
|
||||
# print diff between YAML strings because it's prettier
|
||||
assert_equal(yaml_sane_dump(schema1, binary=False), yaml_sane_dump(schema2, binary=False))
|
||||
|
||||
return compare_schemas
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def swagger_settings(settings):
|
||||
swagger_settings = copy.deepcopy(settings.SWAGGER_SETTINGS)
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import string
|
||||
import tempfile
|
||||
from collections import OrderedDict
|
||||
|
||||
import pytest
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import call_command
|
||||
from six import StringIO
|
||||
|
||||
from drf_yasg.codecs import yaml_sane_load
|
||||
|
||||
|
||||
def call_generate_swagger(output_file='-', overwrite=False, format='', api_url='',
|
||||
mock=False, user='', private=False, **kwargs):
|
||||
out = StringIO()
|
||||
call_command(
|
||||
'generate_swagger', stdout=out,
|
||||
output_file=output_file, overwrite=overwrite, format=format,
|
||||
api_url=api_url, mock=mock, user=user, private=private,
|
||||
**kwargs
|
||||
)
|
||||
return out.getvalue()
|
||||
|
||||
|
||||
def test_reference_schema(db, reference_schema):
|
||||
User.objects.create_superuser('admin', 'admin@admin.admin', 'blabla')
|
||||
|
||||
output = call_generate_swagger(format='yaml', api_url='http://test.local:8002/', user='admin')
|
||||
output_schema = yaml_sane_load(output)
|
||||
assert output_schema == reference_schema
|
||||
|
||||
|
||||
def test_non_public(db):
|
||||
output = call_generate_swagger(format='yaml', api_url='http://test.local:8002/', private=True)
|
||||
output_schema = yaml_sane_load(output)
|
||||
assert len(output_schema['paths']) == 0
|
||||
|
||||
|
||||
def test_no_mock(db):
|
||||
output = call_generate_swagger()
|
||||
output_schema = json.loads(output, object_pairs_hook=OrderedDict)
|
||||
assert len(output_schema['paths']) > 0
|
||||
|
||||
|
||||
def silentremove(filename):
|
||||
try:
|
||||
os.remove(filename)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def test_file_output(db):
|
||||
prefix = os.path.join(tempfile.gettempdir(), tempfile.gettempprefix())
|
||||
name = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8))
|
||||
yaml_file = prefix + name + '.yaml'
|
||||
json_file = prefix + name + '.json'
|
||||
other_file = prefix + name + '.txt'
|
||||
|
||||
try:
|
||||
# when called with output file nothing should be written to stdout
|
||||
assert call_generate_swagger(output_file=yaml_file) == ''
|
||||
assert call_generate_swagger(output_file=json_file) == ''
|
||||
assert call_generate_swagger(output_file=other_file) == ''
|
||||
|
||||
with pytest.raises(OSError):
|
||||
# a second call should fail because file exists
|
||||
call_generate_swagger(output_file=yaml_file)
|
||||
|
||||
# a second call with overwrite should still succeed
|
||||
assert call_generate_swagger(output_file=json_file, overwrite=True) == ''
|
||||
|
||||
with open(yaml_file) as f:
|
||||
content = f.read()
|
||||
# YAML is a superset of JSON - that means we have to check that
|
||||
# the file is really YAML and not just JSON parsed by the YAML parser
|
||||
with pytest.raises(ValueError):
|
||||
json.loads(content)
|
||||
output_yaml = yaml_sane_load(content)
|
||||
with open(json_file) as f:
|
||||
output_json = json.load(f, object_pairs_hook=OrderedDict)
|
||||
with open(other_file) as f:
|
||||
output_other = json.load(f, object_pairs_hook=OrderedDict)
|
||||
|
||||
assert output_yaml == output_json == output_other
|
||||
finally:
|
||||
silentremove(yaml_file)
|
||||
silentremove(json_file)
|
||||
silentremove(other_file)
|
||||
@@ -1,21 +1,8 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
from datadiff.tools import assert_equal
|
||||
|
||||
from drf_yasg.codecs import yaml_sane_dump
|
||||
from drf_yasg.inspectors import FieldInspector, SerializerInspector, PaginatorInspector, FilterInspector
|
||||
|
||||
|
||||
def test_reference_schema(swagger_dict, reference_schema):
|
||||
swagger_dict = OrderedDict(swagger_dict)
|
||||
reference_schema = OrderedDict(reference_schema)
|
||||
ignore = ['info', 'host', 'schemes', 'basePath', 'securityDefinitions']
|
||||
for attr in ignore:
|
||||
swagger_dict.pop(attr, None)
|
||||
reference_schema.pop(attr, None)
|
||||
|
||||
# print diff between YAML strings because it's prettier
|
||||
assert_equal(yaml_sane_dump(swagger_dict, binary=False), yaml_sane_dump(reference_schema, binary=False))
|
||||
def test_reference_schema(swagger_dict, reference_schema, compare_schemas):
|
||||
compare_schemas(swagger_dict, reference_schema)
|
||||
|
||||
|
||||
class NoOpFieldInspector(FieldInspector):
|
||||
@@ -34,7 +21,7 @@ class NoOpPaginatorInspector(PaginatorInspector):
|
||||
pass
|
||||
|
||||
|
||||
def test_noop_inspectors(swagger_settings, swagger_dict, reference_schema):
|
||||
def test_noop_inspectors(swagger_settings, swagger_dict, reference_schema, compare_schemas):
|
||||
from drf_yasg import app_settings
|
||||
|
||||
def set_inspectors(inspectors, setting_name):
|
||||
@@ -43,4 +30,4 @@ def test_noop_inspectors(swagger_settings, swagger_dict, reference_schema):
|
||||
set_inspectors([NoOpFieldInspector, NoOpSerializerInspector], 'DEFAULT_FIELD_INSPECTORS')
|
||||
set_inspectors([NoOpFilterInspector], 'DEFAULT_FILTER_INSPECTORS')
|
||||
set_inspectors([NoOpPaginatorInspector], 'DEFAULT_PAGINATOR_INSPECTORS')
|
||||
test_reference_schema(swagger_dict, reference_schema)
|
||||
compare_schemas(swagger_dict, reference_schema)
|
||||
|
||||
Reference in New Issue
Block a user