@@ -154,22 +154,25 @@ async def mock_getiter(*args, **kwargs):
154
154
@pytest .fixture
155
155
def get_mock_github_api ():
156
156
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 )
160
164
161
- def mock_getitem (* args , ** kwargs ):
165
+ async def mock_getitem (* args , ** kwargs ):
162
166
return return_data
163
167
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
169
171
170
172
mock_api .getitem = mock_getitem
171
173
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
173
176
mock_api .installation_id = installation_id
174
177
175
178
return mock_api
@@ -178,11 +181,11 @@ def mock_post(*args, **kwargs):
178
181
179
182
180
183
@pytest .fixture
181
- def installation (aget_mock_github_api , baker ):
184
+ def installation (get_mock_github_api , baker ):
182
185
installation = baker .make (
183
186
"django_github_app.Installation" , installation_id = seq .next ()
184
187
)
185
- mock_github_api = aget_mock_github_api (
188
+ mock_github_api = get_mock_github_api (
186
189
[
187
190
{"id" : seq .next (), "node_id" : "node1" , "full_name" : "owner/repo1" },
188
191
{"id" : seq .next (), "node_id" : "node2" , "full_name" : "owner/repo2" },
@@ -210,14 +213,14 @@ async def ainstallation(aget_mock_github_api, baker):
210
213
211
214
212
215
@pytest .fixture
213
- def repository (installation , aget_mock_github_api , baker ):
216
+ def repository (installation , get_mock_github_api , baker ):
214
217
repository = baker .make (
215
218
"django_github_app.Repository" ,
216
219
repository_id = seq .next (),
217
220
full_name = "owner/repo" ,
218
221
installation = installation ,
219
222
)
220
- mock_github_api = aget_mock_github_api (
223
+ mock_github_api = get_mock_github_api (
221
224
[
222
225
{
223
226
"number" : 1 ,
0 commit comments