|
| 1 | +import pytest |
| 2 | + |
| 3 | +MODULE_NAME = "openapi_python_client.openapi_parser.responses" |
| 4 | + |
| 5 | + |
| 6 | +class TestResponse: |
| 7 | + def test_return_string(self): |
| 8 | + from openapi_python_client.openapi_parser.responses import Response |
| 9 | + |
| 10 | + r = Response(200) |
| 11 | + |
| 12 | + assert r.return_string() == "None" |
| 13 | + |
| 14 | + def test_constructor(self): |
| 15 | + from openapi_python_client.openapi_parser.responses import Response |
| 16 | + |
| 17 | + r = Response(200) |
| 18 | + |
| 19 | + assert r.constructor() == "None" |
| 20 | + |
| 21 | + |
| 22 | +class TestListRefResponse: |
| 23 | + def test_return_string(self, mocker): |
| 24 | + from openapi_python_client.openapi_parser.responses import ListRefResponse |
| 25 | + |
| 26 | + r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass")) |
| 27 | + |
| 28 | + assert r.return_string() == "List[SuperCoolClass]" |
| 29 | + |
| 30 | + def test_constructor(self, mocker): |
| 31 | + from openapi_python_client.openapi_parser.responses import ListRefResponse |
| 32 | + |
| 33 | + r = ListRefResponse(200, reference=mocker.MagicMock(class_name="SuperCoolClass")) |
| 34 | + |
| 35 | + assert r.constructor() == "[SuperCoolClass.from_dict(item) for item in response.json()]" |
| 36 | + |
| 37 | + |
| 38 | +class TestStringResponse: |
| 39 | + def test_return_string(self): |
| 40 | + from openapi_python_client.openapi_parser.responses import StringResponse |
| 41 | + |
| 42 | + r = StringResponse(200) |
| 43 | + |
| 44 | + assert r.return_string() == "str" |
| 45 | + |
| 46 | + def test_constructor(self): |
| 47 | + from openapi_python_client.openapi_parser.responses import StringResponse |
| 48 | + |
| 49 | + r = StringResponse(200) |
| 50 | + |
| 51 | + assert r.constructor() == "response.text" |
| 52 | + |
| 53 | + |
| 54 | +class TestResponseFromDict: |
| 55 | + def test_response_from_dict_no_content(self): |
| 56 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 57 | + |
| 58 | + with pytest.raises(ValueError): |
| 59 | + response_from_dict(status_code=200, data={}) |
| 60 | + |
| 61 | + def test_response_from_dict_unsupported_content_type(self): |
| 62 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 63 | + |
| 64 | + with pytest.raises(ValueError): |
| 65 | + response_from_dict(status_code=200, data={"content": {"not/real": {}}}) |
| 66 | + |
| 67 | + def test_response_from_dict_ref(self, mocker): |
| 68 | + ref = mocker.MagicMock() |
| 69 | + status_code = mocker.MagicMock(autospec=int) |
| 70 | + data = {"content": {"application/json": {"schema": {"$ref": ref}}}} |
| 71 | + Reference = mocker.patch(f"{MODULE_NAME}.Reference") |
| 72 | + RefResponse = mocker.patch(f"{MODULE_NAME}.RefResponse") |
| 73 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 74 | + |
| 75 | + response = response_from_dict(status_code=status_code, data=data) |
| 76 | + |
| 77 | + Reference.assert_called_once_with(ref) |
| 78 | + RefResponse.assert_called_once_with(status_code=status_code, reference=Reference()) |
| 79 | + assert response == RefResponse() |
| 80 | + |
| 81 | + def test_response_from_dict_empty(self, mocker): |
| 82 | + status_code = mocker.MagicMock(autospec=int) |
| 83 | + data = {"content": {"application/json": {"schema": {}}}} |
| 84 | + Response = mocker.patch(f"{MODULE_NAME}.Response") |
| 85 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 86 | + |
| 87 | + response = response_from_dict(status_code=status_code, data=data) |
| 88 | + |
| 89 | + Response.assert_called_once_with(status_code=status_code) |
| 90 | + assert response == Response() |
| 91 | + |
| 92 | + def test_response_from_dict_array(self, mocker): |
| 93 | + ref = mocker.MagicMock() |
| 94 | + status_code = mocker.MagicMock(autospec=int) |
| 95 | + data = {"content": {"application/json": {"schema": {"type": "array", "items": {"$ref": ref}}}}} |
| 96 | + Reference = mocker.patch(f"{MODULE_NAME}.Reference") |
| 97 | + ListRefResponse = mocker.patch(f"{MODULE_NAME}.ListRefResponse") |
| 98 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 99 | + |
| 100 | + response = response_from_dict(status_code=status_code, data=data) |
| 101 | + |
| 102 | + Reference.assert_called_once_with(ref) |
| 103 | + ListRefResponse.assert_called_once_with(status_code=status_code, reference=Reference()) |
| 104 | + assert response == ListRefResponse() |
| 105 | + |
| 106 | + def test_response_from_dict_string(self, mocker): |
| 107 | + status_code = mocker.MagicMock(autospec=int) |
| 108 | + data = {"content": {"text/html": {"schema": {"type": "string"}}}} |
| 109 | + StringResponse = mocker.patch(f"{MODULE_NAME}.StringResponse") |
| 110 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 111 | + |
| 112 | + response = response_from_dict(status_code=status_code, data=data) |
| 113 | + |
| 114 | + StringResponse.assert_called_once_with(status_code=status_code) |
| 115 | + assert response == StringResponse() |
| 116 | + |
| 117 | + def test_response_from_dict_unsupported_type(self): |
| 118 | + from openapi_python_client.openapi_parser.responses import response_from_dict |
| 119 | + |
| 120 | + with pytest.raises(ValueError): |
| 121 | + response_from_dict(status_code=200, data={"content": {"application/json": {"schema": {"type": "blah"}}}}) |
0 commit comments