Skip to content

Commit ee54b8e

Browse files
committed
Added test for building API dir
#3
1 parent 60ae15f commit ee54b8e

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

tests/test___init__.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,59 @@ def test__build_models(self, mocker):
217217
enum_1_module_path.write_text.assert_called_once_with(enum_render_1)
218218
enum_2_module_path.write_text.assert_called_once_with(enum_render_2)
219219

220-
# def test__build_api(self):
221-
# assert False
220+
def test__build_api(self, mocker):
221+
import pathlib
222+
from openapi_python_client import _Project, OpenAPI
223+
224+
openapi = mocker.MagicMock(autospec=OpenAPI, title="My Test API")
225+
tag_1 = mocker.MagicMock(autospec=str)
226+
tag_2 = mocker.MagicMock(autospec=str)
227+
collection_1 = mocker.MagicMock()
228+
collection_2 = mocker.MagicMock()
229+
openapi.endpoint_collections_by_tag = {tag_1: collection_1, tag_2: collection_2}
230+
project = _Project(openapi=openapi)
231+
project.package_dir = mocker.MagicMock()
232+
client_path = mocker.MagicMock()
233+
api_init = mocker.MagicMock(autospec=pathlib.Path)
234+
collection_1_path = mocker.MagicMock(autospec=pathlib.Path)
235+
collection_2_path = mocker.MagicMock(autospec=pathlib.Path)
236+
api_paths = {
237+
"__init__.py": api_init,
238+
f"{tag_1}.py": collection_1_path,
239+
f"{tag_2}.py": collection_2_path,
240+
}
241+
api_dir = mocker.MagicMock(autospec=pathlib.Path)
242+
api_dir.__truediv__.side_effect = lambda x: api_paths[x]
243+
package_paths = {
244+
"client.py": client_path,
245+
"api": api_dir,
246+
}
247+
project.package_dir.__truediv__.side_effect = lambda x: package_paths[x]
248+
client_template = mocker.MagicMock()
249+
endpoint_template = mocker.MagicMock()
250+
templates = {
251+
"client.pyi": client_template,
252+
"endpoint_module.pyi": endpoint_template,
253+
}
254+
mocker.patch.object(project.env, "get_template", autospec=True, side_effect=lambda x: templates[x])
255+
endpoint_renders = {
256+
collection_1: mocker.MagicMock(),
257+
collection_2: mocker.MagicMock(),
258+
}
259+
endpoint_template.render.side_effect = lambda collection: endpoint_renders[collection]
260+
261+
project._build_api()
262+
263+
project.package_dir.__truediv__.assert_has_calls([mocker.call(key) for key in package_paths])
264+
project.env.get_template.assert_has_calls([mocker.call(key) for key in templates])
265+
client_template.render.assert_called_once()
266+
client_path.write_text.assert_called_once_with(client_template.render())
267+
api_dir.mkdir.assert_called_once()
268+
api_dir.__truediv__.assert_has_calls([mocker.call(key) for key in api_paths])
269+
api_init.write_text.assert_called_once_with('""" Contains all methods for accessing the API """')
270+
endpoint_template.render.assert_has_calls([
271+
mocker.call(collection=collection_1),
272+
mocker.call(collection=collection_2),
273+
])
274+
collection_1_path.write_text.assert_called_once_with(endpoint_renders[collection_1])
275+
collection_2_path.write_text.assert_called_once_with(endpoint_renders[collection_2])

0 commit comments

Comments
 (0)