def test__get_document_path_no_url(self, mocker): get = mocker.patch("httpx.get") loads = mocker.patch("yaml.safe_load") from openapi_python_client import _get_document path = mocker.MagicMock() _get_document(url=None, path=path) get.assert_not_called() path.read_bytes.assert_called_once() loads.assert_called_once_with(path.read_bytes())
def test__get_document_path_no_url(self, tmp_path, mocker): get = mocker.patch("httpx.get") loads = mocker.patch("yaml.safe_load") path = tmp_path / "test.yaml" path.write_text("some test data") from openapi_python_client import _get_document _get_document(url=None, path=path) get.assert_not_called() loads.assert_called_once_with(b"some test data")
def test__get_document_url_no_path(self, mocker): get = mocker.patch("httpx.get") _Path = mocker.patch("openapi_python_client.Path") loads = mocker.patch("yaml.safe_load") from openapi_python_client import _get_document url = "test" _get_document(url=url, path=None) get.assert_called_once_with(url) _Path.assert_not_called() loads.assert_called_once_with(get().content)
def test__get_document_bad_yaml(self, mocker, tmp_path): get = mocker.patch("httpx.get") from openapi_python_client import _get_document path = tmp_path / "test.yaml" path.write_text("'") result = _get_document(url=None, path=path) get.assert_not_called() assert isinstance(result, GeneratorError) assert "Invalid YAML" in result.header
def test__get_document_no_url_or_path(self, mocker): get = mocker.patch("httpx.get") _Path = mocker.patch("openapi_python_client.Path") loads = mocker.patch("yaml.safe_load") from openapi_python_client import _get_document result = _get_document(url=None, path=None) assert result == GeneratorError(header="No URL or Path provided") get.assert_not_called() _Path.assert_not_called() loads.assert_not_called()
def test__get_document_url_and_path(self, mocker): get = mocker.patch("httpx.get") _Path = mocker.patch("openapi_python_client.Path") loads = mocker.patch("yaml.safe_load") from openapi_python_client import _get_document result = _get_document(url=mocker.MagicMock(), path=mocker.MagicMock()) assert result == GeneratorError( header="Provide URL or Path, not both.") get.assert_not_called() _Path.assert_not_called() loads.assert_not_called()
def test__get_document_bad_yaml(self, mocker): get = mocker.patch("httpx.get") loads = mocker.patch("yaml.safe_load", side_effect=yaml.YAMLError) from openapi_python_client import _get_document path = mocker.MagicMock() result = _get_document(url=None, path=path) get.assert_not_called() path.read_bytes.assert_called_once() loads.assert_called_once_with(path.read_bytes()) assert result == GeneratorError( header="Invalid YAML from provided source")
def test__get_document_bad_url(self, mocker): get = mocker.patch("httpx.get", side_effect=httpcore.NetworkError) _Path = mocker.patch("openapi_python_client.Path") loads = mocker.patch("yaml.safe_load") from openapi_python_client import _get_document url = mocker.MagicMock() result = _get_document(url=url, path=None) assert result == GeneratorError( header="Could not get OpenAPI document from provided URL") get.assert_called_once_with(url) _Path.assert_not_called() loads.assert_not_called()
def test__get_document_bad_json(self, mocker): class FakeResponse: content = b'{"foo"}' headers = {"content-type": "application/json; encoding=utf8"} get = mocker.patch("httpx.get", return_value=FakeResponse()) from openapi_python_client import _get_document url = mocker.MagicMock() result = _get_document(url=url, path=None) get.assert_called_once() assert result == GeneratorError( header="Invalid JSON from provided source: " "Expecting ':' delimiter: line 1 column 7 (char 6)")
def test__get_document_json(self, mocker): class FakeResponse: content = b'{\n\t"foo": "bar"}' headers = {"content-type": "application/json; encoding=utf8"} get = mocker.patch("httpx.get", return_value=FakeResponse()) yaml_loads = mocker.patch("yaml.safe_load") json_result = mocker.MagicMock() json_loads = mocker.patch("json.loads", return_value=json_result) from openapi_python_client import _get_document url = mocker.MagicMock() result = _get_document(url=url, path=None) get.assert_called_once() json_loads.assert_called_once_with(FakeResponse.content.decode()) yaml_loads.assert_not_called() assert result == json_result