Files
connexion/tests/test_utils.py
Stephen Hurwitz 43003f8666 Fixes util.deep_get when obj is a list (#1055)
* Fixes util.deep_get when obj is a list

The example below will seem strange. It's the result of distilling a complex
API spec down to the root cause of the issue.

```yaml
---
openapi: 3.0.0
info:
  version: v0
  title: Repo an error
  description: Extremely contrived, but it illustrates the problem
paths:
  /:
    get:
      operationId: app.get
      responses:
        '200':
          description: Just for giggles
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/SomeWeirdResource"
components:
  schemas:
    Resource:
      allOf:
        - type: object
          properties:
            id:
              type: string
    SomeWeirdResource:
      allOf:
        - type: object
          properties:
            id:
              $ref: '#/components/schemas/Resource/allOf/0/properties/id'
```

While convoluted, this is a valid OpenAPI 3 spec.

```sh
$ yarn swagger-cli validate openapi/my_api.yaml
yarn run v1.17.3
$ /[redacted]/node_modules/.bin/swagger-cli validate openapi/my_api.yaml
openapi/my_api.yaml is valid
  Done in 0.23s.
```

`utils.deep_get` assumes the obj to be a dictionary when it can also be a list.

* Updates docstring for utils.deep_get

Comment on the pull request was asking for an explanation to clarify the
code. The updated docstring aims to be clear on how exactly the object
can be a list.
2019-10-16 18:05:23 +02:00

64 lines
1.9 KiB
Python

import math
import pytest
from mock import MagicMock
import connexion.apps
from connexion import utils
def test_get_function_from_name():
function = utils.get_function_from_name('math.ceil')
assert function == math.ceil
assert function(2.7) == 3
def test_get_function_from_name_no_module():
with pytest.raises(ValueError):
utils.get_function_from_name('math')
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.FlaskApp.common_error_handler')
assert function == connexion.FlaskApp.common_error_handler
def test_boolean():
assert utils.boolean('true')
assert utils.boolean('True')
assert utils.boolean('TRUE')
assert utils.boolean(True)
assert not utils.boolean('false')
assert not utils.boolean('False')
assert not utils.boolean('FALSE')
assert not utils.boolean(False)
with pytest.raises(ValueError):
utils.boolean('foo')
with pytest.raises(ValueError):
utils.boolean(None)
def test_deep_get_dict():
obj = {'type': 'object', 'properties': {'id': {'type': 'string'}}}
assert utils.deep_get(obj, ['properties', 'id']) == {'type': 'string'}
def test_deep_get_list():
obj = [{'type': 'object', 'properties': {'id': {'type': 'string'}}}]
assert utils.deep_get(obj, ['0', 'properties', 'id']) == {'type': 'string'}