Skip to content

Commit

Permalink
bells and whistles
Browse files Browse the repository at this point in the history
  • Loading branch information
smarzola committed Jan 21, 2017
1 parent 33b1f95 commit 78b9630
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cornice_swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import six

import colander
from cornice.validators import colander_validator, colander_body_validator
from cornice.validators import colander_body_validator

import cornice_swagger.util
from cornice_swagger.converters import convert_schema, convert_parameter
Expand Down
12 changes: 12 additions & 0 deletions tests/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ class ResponseSchema(colander.MappingSchema):
'200': ResponseSchema(description='Return ice cream'),
'404': ResponseSchema(description='Return sadness')
}


class DeclarativeSchema(colander.MappingSchema):
@colander.instantiate(description='my body')
class body(colander.MappingSchema):
id = colander.SchemaNode(colander.String())


class AnotherDeclarativeSchema(colander.MappingSchema):
@colander.instantiate(description='my another body')
class body(colander.MappingSchema):
timestamp = colander.SchemaNode(colander.Int())
27 changes: 8 additions & 19 deletions tests/test_parameter_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from cornice_swagger.swagger import ParameterHandler, DefinitionHandler
from cornice_swagger.converters import convert_schema
from .support import BodySchema, PathSchema, QuerySchema, HeaderSchema
from .support import (BodySchema,
PathSchema,
QuerySchema,
HeaderSchema,
DeclarativeSchema,
AnotherDeclarativeSchema)


class SchemaParamConversionTest(unittest.TestCase):
Expand Down Expand Up @@ -144,20 +149,10 @@ class RequestSchema(colander.MappingSchema):
self.assertDictEqual(params[0], expected)

def test_declarative_schema_handling(self):
class RequestSchema(colander.MappingSchema):
@colander.instantiate(description='my body')
class body(colander.MappingSchema):
id = colander.SchemaNode(colander.String())

class AnotherRequestSchema(colander.MappingSchema):
@colander.instantiate(description='my another body')
class body(colander.MappingSchema):
timestamp = colander.SchemaNode(colander.Int())

validators = [colander_validator]
handler = ParameterHandler(DefinitionHandler(ref=-1))
params = handler.from_schema(RequestSchema(), validators)
another_params = handler.from_schema(AnotherRequestSchema(), validators)
params = handler.from_schema(DeclarativeSchema(), validators)
another_params = handler.from_schema(AnotherDeclarativeSchema(), validators)

self.assertNotEqual(params[0]['schema'], another_params[0]['schema'])

Expand Down Expand Up @@ -210,12 +205,6 @@ def test_ref_from_body_validator_schema(self):
self.assertDictEqual(self.handler.parameter_registry, dict(BodySchema=expected))

def test_ref_from_declarative_body_validator_schema(self):
class DeclarativeSchema(colander.MappingSchema):
@colander.instantiate()
class body(colander.MappingSchema):
id = colander.SchemaNode(colander.String())
timestamp = colander.SchemaNode(colander.Int())

validators = [colander_body_validator]
params = self.handler.from_schema(DeclarativeSchema()['body'], validators)

Expand Down
26 changes: 10 additions & 16 deletions tests/test_response_handler.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import unittest

import colander

from cornice_swagger.swagger import ResponseHandler, CorniceSwaggerException
from cornice_swagger.converters import convert_schema
from .support import BodySchema, HeaderSchema, ResponseSchema, response_schemas
from .support import (BodySchema,
HeaderSchema,
ResponseSchema,
response_schemas,
DeclarativeSchema,
AnotherDeclarativeSchema)


class SchemaResponseConversionTest(unittest.TestCase):
Expand Down Expand Up @@ -59,25 +62,16 @@ def test_from_schema_mapping(self):
convert_schema(HeaderSchema())['properties'])

def test_declarative_response_schemas(self):
class ResponseSchema(colander.MappingSchema):
@colander.instantiate()
class body(colander.MappingSchema):
timestamp = colander.SchemaNode(colander.Int())

class AnotherResponseSchema(colander.MappingSchema):
@colander.instantiate()
class body(colander.MappingSchema):
timestamp = colander.SchemaNode(colander.Int())

self.handler.from_schema_mapping({
'200': ResponseSchema(description='response schema')
'200': DeclarativeSchema(description='response schema')
})

self.handler.from_schema_mapping({
'200': AnotherResponseSchema(description='response schema')
'200': AnotherDeclarativeSchema(description='response schema')
})

ref = self.handler.response_registry['ResponseSchema']
another_ref = self.handler.response_registry['AnotherResponseSchema']
ref = self.handler.response_registry['DeclarativeSchema']
another_ref = self.handler.response_registry['AnotherDeclarativeSchema']

self.assertNotEqual(ref['schema']['title'], another_ref['schema']['title'])

0 comments on commit 78b9630

Please sign in to comment.