Skip to content

Commit af7fd99

Browse files
use @pytest_asyncio.fixture for all async fixtures and fix awaits (#76)
1 parent 59fe66f commit af7fd99

File tree

5 files changed

+39
-60
lines changed

5 files changed

+39
-60
lines changed

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest.mock import MagicMock
77

88
import pytest
9+
import pytest_asyncio
910
from asgiref.sync import sync_to_async
1011
from django.conf import settings
1112
from django.test import override_settings
@@ -163,7 +164,7 @@ def installation(get_mock_github_api, baker):
163164
return installation
164165

165166

166-
@pytest.fixture
167+
@pytest_asyncio.fixture
167168
async def ainstallation(get_mock_github_api, baker):
168169
installation = await sync_to_async(baker.make)(
169170
"django_github_app.Installation", installation_id=seq.next()
@@ -206,14 +207,13 @@ def repository(installation, get_mock_github_api, baker):
206207
return repository
207208

208209

209-
@pytest.fixture
210+
@pytest_asyncio.fixture
210211
async def arepository(ainstallation, get_mock_github_api, baker):
211-
installation = await ainstallation
212212
repository = await sync_to_async(baker.make)(
213213
"django_github_app.Repository",
214214
repository_id=seq.next(),
215215
full_name="owner/repo",
216-
installation=installation,
216+
installation=ainstallation,
217217
)
218218
mock_github_api = get_mock_github_api(
219219
[

tests/events/test_ainstallation.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ async def test_acreate_installation(
4848

4949

5050
async def test_adelete_installation(ainstallation):
51-
installation = await ainstallation
5251
data = {
5352
"installation": {
54-
"id": installation.installation_id,
53+
"id": ainstallation.installation_id,
5554
}
5655
}
5756
event = sansio.Event(data, event="installation", delivery_id="1234")
@@ -73,55 +72,51 @@ async def test_adelete_installation(ainstallation):
7372
async def test_atoggle_installation_status_suspend(
7473
status, action, expected, ainstallation
7574
):
76-
installation = await ainstallation
77-
installation.status = status
78-
await installation.asave()
75+
ainstallation.status = status
76+
await ainstallation.asave()
7977

8078
data = {
8179
"action": action,
8280
"installation": {
83-
"id": installation.installation_id,
81+
"id": ainstallation.installation_id,
8482
},
8583
}
8684
event = sansio.Event(data, event="installation", delivery_id="1234")
8785

88-
assert installation.status != expected
86+
assert ainstallation.status != expected
8987

9088
await atoggle_installation_status(event, None)
9189

92-
await installation.arefresh_from_db()
93-
assert installation.status == expected
90+
await ainstallation.arefresh_from_db()
91+
assert ainstallation.status == expected
9492

9593

9694
async def test_async_installation_data(ainstallation):
97-
installation = await ainstallation
98-
9995
data = {
10096
"installation": {
101-
"id": installation.installation_id,
97+
"id": ainstallation.installation_id,
10298
},
10399
}
104100
event = sansio.Event(data, event="installation", delivery_id="1234")
105101

106-
assert installation.data != data
102+
assert ainstallation.data != data
107103

108104
await async_installation_data(event, None)
109105

110-
await installation.arefresh_from_db()
111-
assert installation.data == data["installation"]
106+
await ainstallation.arefresh_from_db()
107+
assert ainstallation.data == data["installation"]
112108

113109

114110
async def test_async_installation_repositories(ainstallation):
115-
installation = await ainstallation
116111
existing_repo = await sync_to_async(baker.make)(
117112
"django_github_app.Repository",
118-
installation=installation,
113+
installation=ainstallation,
119114
repository_id=seq.next(),
120115
)
121116

122117
data = {
123118
"installation": {
124-
"id": installation.installation_id,
119+
"id": ainstallation.installation_id,
125120
},
126121
"repositories_removed": [
127122
{

tests/events/test_arepository.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414

1515
async def test_arename_repository(ainstallation, repository_id):
16-
installation = await ainstallation
1716
repository = await sync_to_async(baker.make)(
1817
"django_github_app.Repository",
19-
installation=installation,
18+
installation=ainstallation,
2019
repository_id=repository_id,
2120
full_name=f"owner/old_name_{seq.next()}",
2221
)

tests/test_github.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414
@pytest.mark.django_db
1515
class TestAsyncGitHubAPI:
1616
async def test_init_with_two_installation_kwargs(self, ainstallation):
17-
installation = await ainstallation
18-
1917
with pytest.raises(ValueError):
2018
AsyncGitHubAPI(
2119
"test",
22-
installation=installation,
23-
installation_id=installation.installation_id,
20+
installation=ainstallation,
21+
installation_id=ainstallation.installation_id,
2422
)
2523

2624
async def test_request(self, httpx_mock):
@@ -36,9 +34,7 @@ async def mock_aget_access_token(*args, **kwargs):
3634

3735
monkeypatch.setattr(Installation, "aget_access_token", mock_aget_access_token)
3836

39-
installation = await ainstallation
40-
41-
async with AsyncGitHubAPI("test", installation=installation) as gh:
37+
async with AsyncGitHubAPI("test", installation=ainstallation) as gh:
4238
assert gh.oauth_token == "ABC123"
4339

4440
async def test_oauth_token_installation_id(self, ainstallation, monkeypatch):
@@ -47,10 +43,8 @@ async def mock_aget_access_token(*args, **kwargs):
4743

4844
monkeypatch.setattr(Installation, "aget_access_token", mock_aget_access_token)
4945

50-
installation = await ainstallation
51-
5246
async with AsyncGitHubAPI(
53-
"test", installation_id=installation.installation_id
47+
"test", installation_id=ainstallation.installation_id
5448
) as gh:
5549
assert gh.oauth_token == "ABC123"
5650

tests/test_models.py

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,13 @@ def test_create_from_gh_data(self):
220220

221221
@pytest.mark.asyncio
222222
async def test_aget_from_event(self, ainstallation, create_event):
223-
installation = await ainstallation
224223
event = create_event(
225-
{"installation": {"id": installation.installation_id}}, "installation"
224+
{"installation": {"id": ainstallation.installation_id}}, "installation"
226225
)
227226

228227
result = await Installation.objects.aget_from_event(event)
229228

230-
assert result == installation
229+
assert result == ainstallation
231230

232231
@pytest.mark.asyncio
233232
async def test_aget_from_event_doesnotexist(self, installation_id, create_event):
@@ -287,15 +286,13 @@ async def test_arefresh_from_gh(
287286
get_mock_github_api,
288287
override_app_settings,
289288
):
290-
installation = await ainstallation
291-
292289
mock_github_api = get_mock_github_api({"foo": "bar"})
293-
installation.get_gh_client = MagicMock(return_value=mock_github_api)
290+
ainstallation.get_gh_client = MagicMock(return_value=mock_github_api)
294291

295292
with override_app_settings(PRIVATE_KEY=private_key):
296-
await installation.arefresh_from_gh(account_type, "test")
293+
await ainstallation.arefresh_from_gh(account_type, "test")
297294

298-
assert installation.data == {"foo": "bar"}
295+
assert ainstallation.data == {"foo": "bar"}
299296

300297
@pytest.mark.parametrize("account_type", ["org", "user"])
301298
def test_refresh_from_gh(
@@ -320,9 +317,7 @@ def test_refresh_from_gh_invalid_account_type(self, installation):
320317

321318
@pytest.mark.asyncio
322319
async def test_aget_repos(self, ainstallation):
323-
installation = await ainstallation
324-
325-
repos = await installation.aget_repos()
320+
repos = await ainstallation.aget_repos()
326321

327322
assert len(repos) == 2
328323
assert repos[0]["node_id"] == "node1"
@@ -353,20 +348,21 @@ def test_app_slug(self):
353348
class TestRepositoryManager:
354349
@pytest.mark.asyncio
355350
async def test_acreate_from_gh_data_list(self, ainstallation):
356-
installation = await ainstallation
357351
data = [
358352
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
359353
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
360354
]
361355

362-
repositories = await Repository.objects.acreate_from_gh_data(data, installation)
356+
repositories = await Repository.objects.acreate_from_gh_data(
357+
data, ainstallation
358+
)
363359

364360
assert len(repositories) == len(data)
365361
for i, repo in enumerate(repositories):
366362
assert repo.repository_id == data[i]["id"]
367363
assert repo.repository_node_id == data[i]["node_id"]
368364
assert repo.full_name == data[i]["full_name"]
369-
assert repo.installation_id == installation.id
365+
assert repo.installation_id == ainstallation.id
370366

371367
def test_create_from_gh_data_list(self, installation):
372368
data = [
@@ -385,15 +381,14 @@ def test_create_from_gh_data_list(self, installation):
385381

386382
@pytest.mark.asyncio
387383
async def test_acreate_from_gh_data_single(self, ainstallation):
388-
installation = await ainstallation
389384
data = {"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"}
390385

391-
repository = await Repository.objects.acreate_from_gh_data(data, installation)
386+
repository = await Repository.objects.acreate_from_gh_data(data, ainstallation)
392387

393388
assert repository.repository_id == data["id"]
394389
assert repository.repository_node_id == data["node_id"]
395390
assert repository.full_name == data["full_name"]
396-
assert repository.installation_id == installation.id
391+
assert repository.installation_id == ainstallation.id
397392

398393
def test_create_from_gh_data_single(self, installation):
399394
data = {"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"}
@@ -407,13 +402,11 @@ def test_create_from_gh_data_single(self, installation):
407402

408403
@pytest.mark.asyncio
409404
async def test_aget_from_event(self, arepository, create_event):
410-
repository = await arepository
411-
412405
data = {
413406
"repository": {
414-
"id": repository.repository_id,
415-
"node_id": repository.repository_node_id,
416-
"full_name": repository.full_name,
407+
"id": arepository.repository_id,
408+
"node_id": arepository.repository_node_id,
409+
"full_name": arepository.full_name,
417410
}
418411
}
419412

@@ -424,7 +417,7 @@ async def test_aget_from_event(self, arepository, create_event):
424417
assert repo.repository_id == data["repository"]["id"]
425418
assert repo.repository_node_id == data["repository"]["node_id"]
426419
assert repo.full_name == data["repository"]["full_name"]
427-
assert repo.installation_id == repository.installation.id
420+
assert repo.installation_id == arepository.installation.id
428421

429422
@pytest.mark.asyncio
430423
async def test_aget_from_event_doesnotexist(self, repository_id, create_event):
@@ -466,9 +459,7 @@ def test_get_gh_client(self, repository):
466459

467460
@pytest.mark.asyncio
468461
async def test_aget_issues(self, arepository):
469-
repository = await arepository
470-
471-
issues = await repository.aget_issues()
462+
issues = await arepository.aget_issues()
472463

473464
assert len(issues) == 2
474465
assert issues[0]["number"] == 1

0 commit comments

Comments
 (0)