<commit_before>import unittest

import colander

from cornice.validators import colander_validator, colander_body_validator

from cornice_swagger.swagger import ParameterHandler
from cornice_swagger.converters import convert_schema
from .support import BodySchema, PathSchema, QuerySchema, HeaderSchema


class SchemaParamConversionTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler()

    def test_sanity(self):
        node = colander.MappingSchema()
        params = self.handler.from_schema(node)
        self.assertEquals(params, [])

    def test_covert_with_body_validator_schema(self):
        node = BodySchema()
        validators = [colander_body_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'schema': convert_schema(BodySchema(title='BodySchema'))
        }

        self.assertDictEqual(params[0], expected)

    def test_covert_body_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            body = BodySchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'schema': convert_schema(BodySchema(title='BodySchema'))
        }

        self.assertDictEqual(params[0], expected)

    def test_covert_query_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            querystring = QuerySchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'foo',
            'in': 'query',
            'type': 'string',
            'required': False
        }
        self.assertDictEqual(params[0], expected)

    def test_covert_headers_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            headers = HeaderSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'bar',
            'in': 'header',
            'type': 'string',
            'required': False,
        }
        self.assertDictEqual(params[0], expected)

    def test_covert_path_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            path = PathSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'meh',
            'in': 'path',
            'type': 'string',
            'required': True,
            'default': 'default'
        }
        self.assertDictEqual(params[0], expected)

    def test_convert_multiple_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            body = BodySchema()
            path = PathSchema()
            querystring = QuerySchema()
            headers = HeaderSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)

        names = [param['name'] for param in params]
        expected = ['BodySchema', 'foo', 'bar', 'meh']
        self.assertEqual(sorted(names), sorted(expected))

    def test_convert_descriptions(self):
        class RequestSchema(colander.MappingSchema):
            body = BodySchema(description='my body')

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'description': 'my body',
            'schema': convert_schema(BodySchema(title='BodySchema',
                                                description='my body'))
        }

        self.assertDictEqual(params[0], expected)


class PathParamConversionTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler()

    def test_from_path(self):
        params = self.handler.from_path('/my/{param}/path/{id}')
        names = [param['name'] for param in params]
        expected = ['param', 'id']
        self.assertEqual(sorted(names), sorted(expected))
        for param in params:
            self.assertEquals(param['in'], 'path')


class RefParamTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler(ref=1)<commit_msg>Expose the issue with parameters ref when using colander’s declarative syntax<commit_after>import unittest

import colander

from cornice.validators import colander_validator, colander_body_validator

from cornice_swagger.swagger import ParameterHandler, DefinitionHandler
from cornice_swagger.converters import convert_schema
from .support import BodySchema, PathSchema, QuerySchema, HeaderSchema


class SchemaParamConversionTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler()

    def test_sanity(self):
        node = colander.MappingSchema()
        params = self.handler.from_schema(node)
        self.assertEquals(params, [])

    def test_covert_with_body_validator_schema(self):
        node = BodySchema()
        validators = [colander_body_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'schema': convert_schema(BodySchema(title='BodySchema'))
        }

        self.assertDictEqual(params[0], expected)

    def test_covert_body_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            body = BodySchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'schema': convert_schema(BodySchema(title='BodySchema'))
        }

        self.assertDictEqual(params[0], expected)

    def test_covert_query_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            querystring = QuerySchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'foo',
            'in': 'query',
            'type': 'string',
            'required': False
        }
        self.assertDictEqual(params[0], expected)

    def test_covert_headers_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            headers = HeaderSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'bar',
            'in': 'header',
            'type': 'string',
            'required': False,
        }
        self.assertDictEqual(params[0], expected)

    def test_covert_path_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            path = PathSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)
        self.assertEquals(len(params), 1)

        expected = {
            'name': 'meh',
            'in': 'path',
            'type': 'string',
            'required': True,
            'default': 'default'
        }
        self.assertDictEqual(params[0], expected)

    def test_convert_multiple_with_request_validator_schema(self):

        class RequestSchema(colander.MappingSchema):
            body = BodySchema()
            path = PathSchema()
            querystring = QuerySchema()
            headers = HeaderSchema()

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)

        names = [param['name'] for param in params]
        expected = ['BodySchema', 'foo', 'bar', 'meh']
        self.assertEqual(sorted(names), sorted(expected))

    def test_convert_descriptions(self):
        class RequestSchema(colander.MappingSchema):
            body = BodySchema(description='my body')

        node = RequestSchema()
        validators = [colander_validator]
        params = self.handler.from_schema(node, validators)

        expected = {
            'name': 'BodySchema',
            'in': 'body',
            'required': True,
            'description': 'my body',
            'schema': convert_schema(BodySchema(title='BodySchema',
                                                description='my body'))
        }

        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)

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


class PathParamConversionTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler()

    def test_from_path(self):
        params = self.handler.from_path('/my/{param}/path/{id}')
        names = [param['name'] for param in params]
        expected = ['param', 'id']
        self.assertEqual(sorted(names), sorted(expected))
        for param in params:
            self.assertEquals(param['in'], 'path')


class RefParamTest(unittest.TestCase):

    def setUp(self):
        self.handler = ParameterHandler(ref=1)
{'fraction_non_alphanumeric': 0.10829429594929733, 'fraction_numerical': 0.002480022044640397, 'mean_word_length': 2.4953451043338686, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4869429', 'n_tokens_mistral': 2932, 'n_tokens_neox': 2875, 'n_words': 561}