Skip to content

Commit d396bd7

Browse files
update sync version of mock_github_api client
1 parent f85a6f5 commit d396bd7

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

tests/conftest.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,25 @@ async def mock_getiter(*args, **kwargs):
154154
@pytest.fixture
155155
def get_mock_github_api():
156156
def _get_mock_github_api(return_data, installation_id=12345):
157-
from django_github_app.github import SyncGitHubAPI
158-
159-
mock_api = MagicMock(spec=SyncGitHubAPI)
157+
# For sync tests, we still need an async mock because get_gh_client
158+
# always returns AsyncGitHubAPI, even when used through async_to_sync.
159+
# long term, we'll probably need to just duplicate the code between
160+
# sync and async versions instead of relying on async_to_sync/sync_to_async
161+
# from asgiref, so we'll keep this separate sync mock github api client
162+
# around so we can swap the internals out without changing tests (hopefully)
163+
mock_api = AsyncMock(spec=AsyncGitHubAPI)
160164

161-
def mock_getitem(*args, **kwargs):
165+
async def mock_getitem(*args, **kwargs):
162166
return return_data
163167

164-
def mock_getiter(*args, **kwargs):
165-
yield from return_data
166-
167-
def mock_post(*args, **kwargs):
168-
pass
168+
async def mock_getiter(*args, **kwargs):
169+
for data in return_data:
170+
yield data
169171

170172
mock_api.getitem = mock_getitem
171173
mock_api.getiter = mock_getiter
172-
mock_api.post = mock_post
174+
mock_api.__aenter__.return_value = mock_api
175+
mock_api.__aexit__.return_value = None
173176
mock_api.installation_id = installation_id
174177

175178
return mock_api
@@ -178,11 +181,11 @@ def mock_post(*args, **kwargs):
178181

179182

180183
@pytest.fixture
181-
def installation(aget_mock_github_api, baker):
184+
def installation(get_mock_github_api, baker):
182185
installation = baker.make(
183186
"django_github_app.Installation", installation_id=seq.next()
184187
)
185-
mock_github_api = aget_mock_github_api(
188+
mock_github_api = get_mock_github_api(
186189
[
187190
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
188191
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
@@ -210,14 +213,14 @@ async def ainstallation(aget_mock_github_api, baker):
210213

211214

212215
@pytest.fixture
213-
def repository(installation, aget_mock_github_api, baker):
216+
def repository(installation, get_mock_github_api, baker):
214217
repository = baker.make(
215218
"django_github_app.Repository",
216219
repository_id=seq.next(),
217220
full_name="owner/repo",
218221
installation=installation,
219222
)
220-
mock_github_api = aget_mock_github_api(
223+
mock_github_api = get_mock_github_api(
221224
[
222225
{
223226
"number": 1,

tests/test_models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,10 @@ def test_refresh_from_gh(
289289
account_type,
290290
private_key,
291291
installation,
292-
aget_mock_github_api,
292+
get_mock_github_api,
293293
override_app_settings,
294294
):
295-
mock_github_api = aget_mock_github_api({"foo": "bar"})
295+
mock_github_api = get_mock_github_api({"foo": "bar"})
296296
installation.get_gh_client = MagicMock(return_value=mock_github_api)
297297

298298
with override_app_settings(PRIVATE_KEY=private_key):

0 commit comments

Comments
 (0)