Commit 2a55c9f7 authored by Vitalii Fisenko's avatar Vitalii Fisenko Committed by Andrey Veltischev
Browse files

Add tests for swagger generator

parent 7f6eaeaf
import pytest
from schematics import types
from schematics.exceptions import DataError
from schematics.swagger_generator import BaseSchemaBuilder
def _get_test_cases_models():
return {
'_schema': {
'models.TestValue': {
'title': 'TestValue',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'TestValue model',
'properties': {
'currency': {
'type': 'string',
'enum': ['EUR', 'UAH', 'USD'],
'x-legalNameUa': 'Валюта',
'x-legalNameEn': 'Currency',
},
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount',
}
},
'required': ['amount', 'currency']
},
'models.EuroPayment': {
'title': 'EuroPayment',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'EuroPayment model',
'properties': {
'value': {
'allOf': [{'$ref': '#/components/schemas/models.TestValue'}, {'type': 'object', 'properties': {
'currency': {
'type': 'string',
'enum': ['EUR', ],
'x-legalNameUa': 'Евро',
'x-legalNameEn': 'Euro',
}
}, 'required': ['currency', ]}]
}
},
'required': ['value', ]
},
'models.Bank': {
'title': 'Bank',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'Bank model',
'properties': {
'name': {
'type': 'string',
'maxLength': 100,
'x-legalNameUa': 'Назва',
'x-legalNameEn': 'Name',
},
'payments': {
'type': 'array',
'default': [],
'items': {
'allOf': [{'$ref': '#/components/schemas/models.EuroPayment'},
{'type': 'object', 'properties': {
'currency': {
'type': 'string',
'enum': ['EUR', 'USD']
}
}}]
}
},
'other_payments': {
'type': 'array',
'default': [],
'items': {
'allOf': [{'$ref': '#/components/schemas/models.EuroPayment'},
{'type': 'object', 'properties': {
'currency': {
'type': 'string',
'enum': ['UAN', 'eurocent']
}
}}]
}
}
},
'required': ['name', 'payments']
},
'models.CustomValueFieldOverriding': {
'title': 'models.CustomValue',
'allOf': [
{'$ref': '#/components/schemas/models.TestValue'},
{
'type': 'object',
'properties': {
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount',
}
}
}
]
},
'models.CustomValueRequirementsOverriding': {
'title': 'models.CustomValue',
'allOf': [
{'$ref': '#/components/schemas/models.TestValue'},
{
'type': 'object',
'properties': {
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount',
}
},
'required': ['currency']
}
]
}
}
}
def _get_required_test_cases_models():
return {
'_schema': {
'models.A': {
'title': 'models.A',
'properties': {
'field_1': {
'type': 'string'
},
'field_2': {
'type': 'string'
}
},
'required': ['field_1']
},
'models.B': {
'title': 'models.B',
'allOf': [
{'$ref': '#/components/schemas/models.A'},
{
'type': 'object',
'properties': {
'field_1': {
'type': 'boolean'
}
},
'required': ['field_1', 'field_2']
}
],
},
'models.C': {
'title': 'models.C',
'allOf': [
{'$ref': '#/components/schemas/models.A'},
{
'type': 'object',
'properties': {
'field_2': {
'type': 'boolean'
}
},
'required': ['field_2']
}
],
},
'models.D': {
'title': 'models.D',
'allOf': [
{'$ref': '#/components/schemas/models.A'},
{
'type': 'object',
'required': ['field_2']
}
],
},
'models.E': {
'title': 'models.E',
'allOf': [
{'$ref': '#/components/schemas/models.A'},
{
'type': 'object',
'required': []
}
],
}
}
}
def test_fields_model_inheritance():
test_class_schema = _get_test_cases_models()['_schema']
schema = BaseSchemaBuilder(test_class_schema, 'models')
schema.generate_schema_from_swagger()
cls = getattr(schema.module, 'EuroPayment')
assert isinstance(cls.fields['value'].fields['currency'], types.StringType)
expanded_model = {
'title': 'EuroPayment',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'EuroPayment model',
'properties': {
'value': {
'title': 'TestValue',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'TestValue model',
'properties': {
'currency': {
'type': 'string',
'enum': ['EUR'],
'x-legalNameUa': 'Евро',
'x-legalNameEn': 'Euro',
},
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount',
}
},
'required': ['currency']
}
},
'required': ['value']
}
assert schema._schemas['models.EuroPayment'] == expanded_model
assert cls.fields['value'].fields['currency'].choices == ['EUR']
assert cls.fields['value'].fields['currency'].required is True
assert cls.fields['value'].required is True
def test_nested_fields_model_inheritance():
test_class_schema = _get_test_cases_models()['_schema']
schema = BaseSchemaBuilder(test_class_schema, 'models')
schema.generate_schema_from_swagger()
cls = getattr(schema.module, 'Bank')
expanded_model = {
'title': 'Bank',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'Bank model',
'properties': {
'name': {
'type': 'string',
'maxLength': 100,
'x-legalNameUa': 'Назва',
'x-legalNameEn': 'Name'},
'payments': {
'type': 'array',
'default': [],
'items': {
'title': 'EuroPayment',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'EuroPayment model',
'properties': {
'value': {
'title': 'TestValue',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'TestValue model',
'properties': {
'currency': {
'type': 'string',
'enum': ['EUR'],
'x-legalNameUa': 'Евро',
'x-legalNameEn': 'Euro'
},
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount'
}
},
'required': ['currency']
},
'currency': {
'type': 'string',
'enum': ['EUR', 'USD']
}
},
'required': ['value']
}
},
'other_payments': {
'type': 'array',
'default': [],
'items': {
'title': 'EuroPayment',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'EuroPayment model',
'properties': {
'value': {
'title': 'TestValue',
'x-baseClass': 'schematics.models.Model',
'type': 'object',
'description': 'TestValue model',
'properties': {
'currency': {
'type': 'string',
'enum': ['EUR'],
'x-legalNameUa': 'Евро',
'x-legalNameEn': 'Euro'
},
'amount': {
'type': 'number',
'format': 'float',
'x-format': 'decimal-float',
'x-legalNameUa': 'Сума',
'x-legalNameEn': 'Amount'
}
},
'required': ['currency']
},
'currency': {
'type': 'string',
'enum': ['UAN', 'eurocent']
}
},
'required': ['value']
}
}
},
'required': ['name', 'payments']
}
assert schema._schemas['models.Bank'] == expanded_model
assert isinstance(cls.fields['payments'], types.ListType)
assert cls.fields['payments'].field.fields['currency'].choices == ['EUR', 'USD']
assert cls.fields['payments'].required is True
assert cls.fields['name'].required is True
assert isinstance(cls.fields['other_payments'], types.ListType)
assert cls.fields['other_payments'].required is False
assert cls.fields['other_payments'].field.fields['currency'].choices == ['UAN', 'eurocent']
def test_overriding_parent_field_stay_required():
test_class_schema = _get_test_cases_models()['_schema']
schema = BaseSchemaBuilder(test_class_schema, 'models')
schema.generate_schema_from_swagger()
cls = getattr(schema.module, 'CustomValueFieldOverriding')
with pytest.raises(DataError, match='{"currency": "This field is required", "amount": "This field is required"}'):
cls()
def test_required_fields_overriding():
test_class_schema = _get_test_cases_models()['_schema']
schema = BaseSchemaBuilder(test_class_schema, 'models')
cls = getattr(schema.module, 'CustomValueRequirementsOverriding')
with pytest.raises(DataError, match='{"currency": "This field is required"}'):
cls()
def test_required_param():
test_class_schema = _get_required_test_cases_models()['_schema']
schema = BaseSchemaBuilder(test_class_schema, 'models')
schema.generate_schema_from_swagger()
a = getattr(schema.module, 'A')
assert a.fields['field_1'].required is True
assert a.fields['field_2'].required is False
b = getattr(schema.module, 'B')
assert b.fields['field_1'].required is True
assert b.fields['field_2'].required is True
c = getattr(schema.module, 'C')
assert c.fields['field_1'].required is False
assert c.fields['field_2'].required is True
d = getattr(schema.module, 'D')
assert d.fields['field_1'].required is False
assert d.fields['field_2'].required is True
e = getattr(schema.module, 'E')
assert e.fields['field_1'].required is False
assert e.fields['field_2'].required is False
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment