Skip to content

Commit b0cea21

Browse files
committed
Add tests for config_handler, json_type_handlers
1 parent 936e0b3 commit b0cea21

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from unittest import mock
3+
4+
from paths_cli.compiling._gendocs.config_handler import *
5+
6+
GOOD_DATA = {'engine': {'header': "Engines", 'description': "desc"}}
7+
8+
MODULE_LOC = "paths_cli.compiling._gendocs.config_handler."
9+
10+
11+
def test_load_config():
12+
# load_config should create the correct DocCategoryInfo objects
13+
mock_open = mock.mock_open(read_data="unused")
14+
mock_loader = mock.Mock(return_value=mock.Mock(return_value=GOOD_DATA))
15+
with mock.patch(MODULE_LOC + "open", mock_open) as m_open, \
16+
mock.patch(MODULE_LOC + "select_loader", mock_loader) as m_load:
17+
config = load_config("foo.yml")
18+
assert 'engine' in config
19+
data = config['engine']
20+
assert isinstance(data, DocCategoryInfo)
21+
assert data.header == "Engines"
22+
assert data.description == "desc"
23+
24+
25+
def test_load_config_file_error():
26+
# if the filename is bad, we should get a FileNotFoundError
27+
with pytest.raises(FileNotFoundError):
28+
load_config("foo.yml")
29+
30+
31+
def test_load_config_data_error():
32+
# if the data can't be mapped to a DocCategoryInfo class, we should get
33+
# a TypeError
34+
BAD_DATA = {'engine': {'foo': 'bar'}}
35+
mock_open = mock.mock_open(read_data="unused")
36+
mock_loader = mock.Mock(return_value=mock.Mock(return_value=BAD_DATA))
37+
with mock.patch(MODULE_LOC + "open", mock_open) as m_open, \
38+
mock.patch(MODULE_LOC + "select_loader", mock_loader) as m_load:
39+
with pytest.raises(TypeError, match='unexpected keyword'):
40+
load_config("foo.yml")
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
3+
from paths_cli.compiling._gendocs.json_type_handlers import *
4+
5+
6+
@pytest.mark.parametrize('json_type', ['object', 'foo'])
7+
def test_handle_object(json_type):
8+
# object should convert to dict; anything else should be is-identical
9+
if json_type == "foo":
10+
json_type = {'foo': 'bar'} # make the `is` test meaningful
11+
result = handle_object(json_type)
12+
if json_type == 'object':
13+
assert result == 'dict'
14+
else:
15+
assert result is json_type
16+
17+
18+
def test_handle_listof():
19+
json_type = {'type': "array", "items": "float"}
20+
assert handle_listof(json_type) == "list of float"
21+
22+
23+
def test_category_handler():
24+
json_type = {"$ref": "#/definitions/engine_type"}
25+
handler = CategoryHandler('engine')
26+
assert handler(json_type) == ":ref:`engine <compiling--engine>`"
27+
28+
29+
def test_eval_handler():
30+
json_type = {"$ref": "#/definitions/EvalInt"}
31+
handler = EvalHandler("EvalInt", link_to="EvalInt")
32+
assert handler(json_type) == ":ref:`EvalInt <EvalInt>`"
33+
34+
35+
@pytest.mark.parametrize('json_type', ['string', 'float', 'list-float',
36+
'engine', 'list-engine'])
37+
def test_json_type_to_string(json_type):
38+
# integration tests for json_type_to_string
39+
inputs, expected = {
40+
'string': ("string", "string"),
41+
'float': ("float", "float"),
42+
'list-float': ({'type': 'array', 'items': 'float'},
43+
"list of float"),
44+
'engine': ({"$ref": "#/definitions/engine_type"},
45+
":ref:`engine <compiling--engine>`"),
46+
'list-engine': ({"type": "array",
47+
"items": {"$ref": "#/definitions/engine_type"}},
48+
"list of :ref:`engine <compiling--engine>`"),
49+
}[json_type]
50+
assert json_type_to_string(inputs) == expected

0 commit comments

Comments
 (0)