Debian Codegraph @

tests/client/test_operation.py

raw
1 """Test client construction"""
2 # pylint: disable=missing-docstring,protected-access
3 from __future__ import annotations
4
5 import asyncio
6 from unittest.mock import patch
7
8 import aiohttp
9 import pytest
10
11 from aiogithubapi import (
12 GitHubAPI,
13 GitHubConnectionException,
14 GitHubException,
15 GitHubRatelimitException,
16 )
17 from aiogithubapi.client import MESSAGE_EXCEPTIONS, STATUS_EXCEPTIONS
18 from aiogithubapi.const import GitHubRequestKwarg, HttpStatusCode
19
20 from tests.common import (
21 EXPECTED_ETAG,
22 TEST_REQUEST_HEADERS,
23 MockedRequests,
24 MockResponse,
25 )
26
27
28 @pytest.mark.asyncio
29 async def test_passing_etag_to_request(
30 github_api: GitHubAPI,
31 mock_requests: MockedRequests,
32 ):
33 response = await github_api.generic("/generic")
34 assert response.status == 200
35 assert "If-None-Match" not in mock_requests.last_request["headers"]
36 mock_requests.clear()
37
38 response = await github_api.generic("/generic", **{GitHubRequestKwarg.ETAG: "test"})
39 assert response.status == 200
40 assert mock_requests.last_request["headers"]["If-None-Match"] == "test"
41
42
43 @pytest.mark.asyncio
44 async def test_passing_headers_to_request(
45 github_api: GitHubAPI,
46 mock_requests: MockedRequests,
47 ):
48 response = await github_api.generic("/generic")
49 assert response.status == 200
50 assert mock_requests.last_request["headers"] == TEST_REQUEST_HEADERS
51 mock_requests.clear()
52
53 response = await github_api.generic(
54 "/generic", **{GitHubRequestKwarg.HEADERS: {"test": "test"}}
55 )
56 assert response.status == 200
57 assert mock_requests.last_request["headers"] == {
58 **TEST_REQUEST_HEADERS,
59 "test": "test",
60 }
61
62
63 @pytest.mark.asyncio
64 async def test_passing_params_to_request(
65 github_api: GitHubAPI,
66 mock_requests: MockedRequests,
67 ):
68 response = await github_api.generic("/generic")
69 assert response.status == 200
70 assert mock_requests.last_request["params"] == {}
71 mock_requests.clear()
72
73 response = await github_api.generic("/generic", **{GitHubRequestKwarg.PARAMS: {"test": "test"}})
74 assert response.status == 200
75 assert mock_requests.last_request["params"] == {"test": "test"}
76 mock_requests.clear()
77
78 response = await github_api.generic("/generic", **{GitHubRequestKwarg.QUERY: {"test": "test"}})
79 assert response.status == 200
80 assert mock_requests.last_request["params"] == {"test": "test"}
81
82
83 @pytest.mark.asyncio
84 async def test_passing_method_to_request(
85 github_api: GitHubAPI,
86 mock_requests: MockedRequests,
87 ):
88 response = await github_api.generic("/generic")
89 assert response.status == 200
90 assert mock_requests.last_request["method"] == "get"
91 mock_requests.clear()
92
93 response = await github_api.generic("/generic", **{GitHubRequestKwarg.METHOD: "POST"})
94 assert response.status == 200
95 assert mock_requests.last_request["method"] == "post"
96
97
98 @pytest.mark.asyncio
99 async def test_client_exceptions(github_api: GitHubAPI):
100 with patch("aiohttp.ClientSession.request", side_effect=aiohttp.ClientError("client_error")):
101 with pytest.raises(
102 GitHubConnectionException,
103 match="Request exception for 'https://api.github.com/generic' with - client_error",
104 ):
105 await github_api.generic("/generic")
106
107 with patch(
108 "aiohttp.ClientSession.request",
109 side_effect=asyncio.CancelledError("cancelation_error"),
110 ):
111 with pytest.raises(
112 GitHubConnectionException,
113 match="Request exception for 'https://api.github.com/generic' with - cancelation_error",
114 ):
115 await github_api.generic("/generic")
116
117 with patch("aiohttp.ClientSession.request", side_effect=asyncio.TimeoutError):
118 with pytest.raises(
119 GitHubConnectionException,
120 match="Timeout of 20 reached while waiting for https://api.github.com/generic",
121 ):
122 await github_api.generic("/generic")
123
124 with patch("aiohttp.ClientSession.request", side_effect=BaseException("Eeeeeh... okey?")):
125 with pytest.raises(
126 GitHubException,
127 match="Unexpected exception for "
128 "'https://api.github.com/generic' with - Eeeeeh... okey?",
129 ):
130 await github_api.generic("/generic")
131
132 with patch("json.loads", side_effect=KeyError("err")):
133 with pytest.raises(
134 GitHubException,
135 match="Could not handle response data from 'https://api.github.com/generic' with - 'err'",
136 ):
137 await github_api.generic("/generic")
138
139
140 @pytest.mark.parametrize(
141 "status,exception",
142 (*((status, STATUS_EXCEPTIONS[status]) for status in STATUS_EXCEPTIONS),),
143 )
144 @pytest.mark.asyncio
145 async def test_status_code_handling(
146 github_api: GitHubAPI,
147 mock_response: MockResponse,
148 status: HttpStatusCode,
149 exception: GitHubException,
150 ):
151
152 mock_response.mock_status = status
153 mock_response.mock_data = {
154 "message": "Error",
155 "documentation_url": "https://example.com",
156 }
157
158 with pytest.raises(exception):
159 await github_api.generic("/generic")
160
161
162 @pytest.mark.parametrize(
163 "message,exception",
164 (
165 *((message, MESSAGE_EXCEPTIONS[message]) for message in MESSAGE_EXCEPTIONS),
166 ("API rate limit exceeded for xxx.xxx.xxx.xxx.", GitHubRatelimitException),
167 ("Random error", GitHubException),
168 ),
169 )
170 @pytest.mark.asyncio
171 async def test_error_message_handling(
172 github_api: GitHubAPI,
173 mock_response: MockResponse,
174 message: str,
175 exception: GitHubException,
176 ):
177
178 mock_response.mock_data = {
179 "message": message,
180 "documentation_url": "https://example.com",
181 }
182
183 with pytest.raises(exception):
184 await github_api.generic("/generic")
185
186
187 @pytest.mark.asyncio
188 async def test_no_content_handling(github_api: GitHubAPI, mock_response: MockResponse):
189 mock_response.mock_status = 204
190 response = await github_api.generic("/generic")
191 assert response.data is None
192
193
194 @pytest.mark.asyncio
195 async def test_response_object(github_api: GitHubAPI, mock_response: MockResponse):
196 response = await github_api.generic("/generic")
197 assert response.etag == EXPECTED_ETAG
198 assert response.page_number == 2
199 assert response.next_page_number == 3
200 assert response.last_page_number == 46
201 assert not response.is_last_page
202
203 mock_response.mock_headers = {"Link": None}
204 response = await github_api.generic("/generic")
205 assert response.is_last_page
206 assert response.page_number == 1
207 assert response.next_page_number is None
208 assert response.last_page_number is None
209 assert response.pages == {}
210
211 mock_response.mock_headers = {"Link": ""}
212 response = await github_api.generic("/generic")
213 assert response.page_number == 1
214
215
216 @pytest.mark.asyncio
217 async def test_created_with_message_handling(github_api: GitHubAPI, mock_response: MockResponse):
218 """Ensure 201 Created with a message does not raise an exception."""
219 mock_response.mock_status = 201
220 mock_response.mock_data = {
221 "message": "Some info message",
222 "id": 123,
223 "name": "created-object"
224 }
225 response = await github_api.generic("/generic")
226 assert response.status == HttpStatusCode.CREATED
227 assert response.data["message"] == "Some info message"
228 assert response.data["id"] == 123
229 assert response.data["name"] == "created-object"