| 1 |
import pytest |
| 2 |
|
| 3 |
from apispec import yaml_utils |
| 4 |
|
| 5 |
|
| 6 |
def test_load_yaml_from_docstring(): |
| 7 |
def f(): |
| 8 |
""" |
| 9 |
Foo |
| 10 |
bar |
| 11 |
baz quux |
| 12 |
|
| 13 |
--- |
| 14 |
herp: 1 |
| 15 |
derp: 2 |
| 16 |
""" |
| 17 |
|
| 18 |
result = yaml_utils.load_yaml_from_docstring(f.__doc__) |
| 19 |
assert result == {"herp": 1, "derp": 2} |
| 20 |
|
| 21 |
|
| 22 |
@pytest.mark.parametrize("docstring", (None, "", "---")) |
| 23 |
def test_load_yaml_from_docstring_empty_docstring(docstring): |
| 24 |
assert yaml_utils.load_yaml_from_docstring(docstring) == {} |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize("docstring", (None, "", "---")) |
| 28 |
def test_load_operations_from_docstring_empty_docstring(docstring): |
| 29 |
assert yaml_utils.load_operations_from_docstring(docstring) == {} |
| 30 |
|
| 31 |
|
| 32 |
def test_dict_to_yaml_unicode(): |
| 33 |
assert yaml_utils.dict_to_yaml({"가": "나"}) == '"\\uAC00": "\\uB098"\n' |
| 34 |
assert yaml_utils.dict_to_yaml({"가": "나"}, {"allow_unicode": True}) == "가: 나\n" |
| 35 |
|
| 36 |
|
| 37 |
def test_dict_to_yaml_keys_are_not_sorted_by_default(): |
| 38 |
assert yaml_utils.dict_to_yaml({"herp": 1, "derp": 2}) == "herp: 1\nderp: 2\n" |
| 39 |
|
| 40 |
|
| 41 |
def test_dict_to_yaml_keys_can_be_sorted_with_yaml_dump_kwargs(): |
| 42 |
assert ( |
| 43 |
yaml_utils.dict_to_yaml( |
| 44 |
{"herp": 1, "derp": 2}, yaml_dump_kwargs={"sort_keys": True} |
| 45 |
) |
| 46 |
== "derp: 2\nherp: 1\n" |
| 47 |
) |