# -*- coding: utf-8 -*-
import pytest
from mock import Mock

from bravado_core.exception import SwaggerMappingError
from bravado_core.operation import Operation
from bravado_core.param import get_param_type_spec
from bravado_core.param import Param
from bravado_core.spec import Spec


@pytest.fixture
def body_param_spec():
    return {
        'name': 'body',
        'in': 'body',
        'description': 'pet id',
        'required': True,
        'schema': {
            'type': 'string',
        },
    }


def test_location_is_body(empty_swagger_spec, body_param_spec):
    param = Param(empty_swagger_spec, Mock(spec=Operation), body_param_spec)
    assert body_param_spec['schema'] == get_param_type_spec(param)


def test_location_is_not_body(empty_swagger_spec):
    for location in ('path', 'query', 'header', 'formData',):
        param_spec = {
            'name': 'petId',
            'in': location,
            'description': 'ID of pet that needs to be updated',
            'required': True,
            'type': 'string',
        }
        param = Param(empty_swagger_spec, Mock(spec=Operation), param_spec)
        assert param_spec == get_param_type_spec(param)


def test_location_invalid(empty_swagger_spec, body_param_spec):
    body_param_spec['in'] = 'foo'
    param = Param(empty_swagger_spec, Mock(spec=Operation), body_param_spec)

    with pytest.raises(SwaggerMappingError) as excinfo:
        get_param_type_spec(param)
    assert 'location foo' in str(excinfo.value)


def test_ref(minimal_swagger_dict, body_param_spec):
    minimal_swagger_dict['parameters'] = {
        'PetIdParam': body_param_spec,
    }
    param_ref_spec = {'$ref': '#/parameters/PetIdParam'}
    swagger_spec = Spec(minimal_swagger_dict)
    param = Param(swagger_spec, Mock(spec=Operation), param_ref_spec)
    assert {'type': 'string'} == get_param_type_spec(param)

[((57, 19, 57, 45), 'bravado_core.spec.Spec', 'Spec', ({(57, 24, 57, 44): 'minimal_swagger_dict'}, {}), '(minimal_swagger_dict)', False, 'from bravado_core.spec import Spec\n'), ((26, 38, 26, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((27, 40, 27, 66), 'bravado_core.param.get_param_type_spec', 'get_param_type_spec', ({(27, 60, 27, 65): 'param'}, {}), '(param)', False, 'from bravado_core.param import get_param_type_spec\n'), ((45, 38, 45, 58), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((47, 9, 47, 43), 'pytest.raises', 'pytest.raises', ({(47, 23, 47, 42): 'SwaggerMappingError'}, {}), '(SwaggerMappingError)', False, 'import pytest\n'), ((48, 8, 48, 34), 'bravado_core.param.get_param_type_spec', 'get_param_type_spec', ({(48, 28, 48, 33): 'param'}, {}), '(param)', False, 'from bravado_core.param import get_param_type_spec\n'), ((58, 32, 58, 52), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((59, 33, 59, 59), 'bravado_core.param.get_param_type_spec', 'get_param_type_spec', ({(59, 53, 59, 58): 'param'}, {}), '(param)', False, 'from bravado_core.param import get_param_type_spec\n'), ((39, 42, 39, 62), 'mock.Mock', 'Mock', (), '', False, 'from mock import Mock\n'), ((40, 29, 40, 55), 'bravado_core.param.get_param_type_spec', 'get_param_type_spec', ({(40, 49, 40, 54): 'param'}, {}), '(param)', False, 'from bravado_core.param import get_param_type_spec\n')]