Skip to content

Commit

Permalink
#218 Raise import errors on endpoint handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcs committed Jun 8, 2016
1 parent 15211d8 commit 87afbfe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
13 changes: 11 additions & 2 deletions connexion/utils.py
Expand Up @@ -99,14 +99,23 @@ def get_function_from_name(function_name):
"""
module_name, attr_path = function_name.rsplit('.', 1)
module = None
last_import_error = None

while not module:

try:
module = importlib.import_module(module_name)
except ImportError:
except ImportError as import_error:
last_import_error = import_error
module_name, attr_path1 = module_name.rsplit('.', 1)
attr_path = '{0}.{1}'.format(attr_path1, attr_path)
function = deep_getattr(module, attr_path)
try:
function = deep_getattr(module, attr_path)
except AttributeError:
if last_import_error:
raise last_import_error
else:
raise
return function


Expand Down
5 changes: 2 additions & 3 deletions tests/test_api.py
@@ -1,10 +1,9 @@
import pathlib

import pytest
from connexion.api import Api
from swagger_spec_validator.common import SwaggerValidationError

import pytest

TEST_FOLDER = pathlib.Path(__file__).parent


Expand All @@ -28,7 +27,7 @@ def test_template():


def test_invalid_operation_does_stop_application_to_setup():
with pytest.raises(AttributeError):
with pytest.raises(ImportError):
Api(TEST_FOLDER / "fakeapi/op_error_api.yaml", "/api/v1.0",
{'title': 'OK'})

Expand Down
15 changes: 14 additions & 1 deletion tests/test_utils.py
Expand Up @@ -2,8 +2,8 @@

import connexion.app
import connexion.utils as utils

import pytest
from mock import MagicMock


def test_flaskify_path():
Expand All @@ -26,6 +26,19 @@ def test_get_function_from_name():
assert function(2.7) == 3


def test_get_function_from_name_attr_error(monkeypatch):
"""
Test attribute error without import error on get_function_from_name.
Attribute errors due to import errors are tested on
test_api.test_invalid_operation_does_stop_application_to_setup
"""
deep_attr_mock = MagicMock()
deep_attr_mock.side_effect = AttributeError
monkeypatch.setattr("connexion.utils.deep_getattr", deep_attr_mock)
with pytest.raises(AttributeError):
utils.get_function_from_name('math.ceil')


def test_get_function_from_name_for_class_method():
function = utils.get_function_from_name('connexion.app.App.common_error_handler')
assert function == connexion.app.App.common_error_handler
Expand Down

0 comments on commit 87afbfe

Please sign in to comment.