Skip to content

Commit d9440ff

Browse files
Consolidate event creation in test suite with faker-based fixture (#91)
1 parent a1b90c7 commit d9440ff

File tree

7 files changed

+100
-73
lines changed

7 files changed

+100
-73
lines changed

tests/conftest.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from django.test import override_settings
1313
from django.urls import clear_url_caches
1414
from django.urls import path
15+
from faker import Faker
16+
from gidgethub import sansio
1517

1618
from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
1719
from django_github_app.github import AsyncGitHubAPI
@@ -232,3 +234,39 @@ async def arepository(ainstallation, get_mock_github_api, baker):
232234
mock_github_api.installation_id = repository.installation.installation_id
233235
repository.get_gh_client = MagicMock(return_value=mock_github_api)
234236
return repository
237+
238+
239+
@pytest.fixture
240+
def faker():
241+
return Faker()
242+
243+
244+
@pytest.fixture
245+
def create_event(faker):
246+
def _create_event(event_type, delivery_id=None, **data):
247+
if delivery_id is None:
248+
delivery_id = seq.next()
249+
250+
if event_type == "issue_comment" and "comment" not in data:
251+
data["comment"] = {"body": faker.sentence()}
252+
253+
if "comment" in data and isinstance(data["comment"], str):
254+
# Allow passing just the comment body as a string
255+
data["comment"] = {"body": data["comment"]}
256+
257+
if "comment" in data and "user" not in data["comment"]:
258+
data["comment"]["user"] = {"login": faker.user_name()}
259+
260+
if "repository" not in data and event_type in [
261+
"issue_comment",
262+
"pull_request",
263+
"push",
264+
]:
265+
data["repository"] = {
266+
"owner": {"login": faker.user_name()},
267+
"name": faker.slug(),
268+
}
269+
270+
return sansio.Event(data=data, event=event_type, delivery_id=delivery_id)
271+
272+
return _create_event

tests/events/test_ainstallation.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44
from asgiref.sync import sync_to_async
5-
from gidgethub.abc import sansio
65
from model_bakery import baker
76

87
from django_github_app.events.ainstallation import acreate_installation
@@ -20,7 +19,11 @@
2019

2120
@pytest.mark.parametrize("app_settings_app_id_type", [int, str])
2221
async def test_acreate_installation(
23-
app_settings_app_id_type, installation_id, repository_id, override_app_settings
22+
app_settings_app_id_type,
23+
installation_id,
24+
repository_id,
25+
override_app_settings,
26+
create_event,
2427
):
2528
data = {
2629
"installation": {
@@ -31,7 +34,7 @@ async def test_acreate_installation(
3134
{"id": repository_id, "node_id": "node1234", "full_name": "owner/repo"}
3235
],
3336
}
34-
event = sansio.Event(data, event="installation", delivery_id="1234")
37+
event = create_event("installation", delivery_id="1234", **data)
3538

3639
with override_app_settings(
3740
APP_ID=data["installation"]["app_id"]
@@ -47,13 +50,13 @@ async def test_acreate_installation(
4750
assert installation.data == data["installation"]
4851

4952

50-
async def test_adelete_installation(ainstallation):
53+
async def test_adelete_installation(ainstallation, create_event):
5154
data = {
5255
"installation": {
5356
"id": ainstallation.installation_id,
5457
}
5558
}
56-
event = sansio.Event(data, event="installation", delivery_id="1234")
59+
event = create_event("installation", delivery_id="1234", **data)
5760

5861
await adelete_installation(event, None)
5962

@@ -70,7 +73,7 @@ async def test_adelete_installation(ainstallation):
7073
],
7174
)
7275
async def test_atoggle_installation_status_suspend(
73-
status, action, expected, ainstallation
76+
status, action, expected, ainstallation, create_event
7477
):
7578
ainstallation.status = status
7679
await ainstallation.asave()
@@ -81,7 +84,7 @@ async def test_atoggle_installation_status_suspend(
8184
"id": ainstallation.installation_id,
8285
},
8386
}
84-
event = sansio.Event(data, event="installation", delivery_id="1234")
87+
event = create_event("installation", delivery_id="1234", **data)
8588

8689
assert ainstallation.status != expected
8790

@@ -91,13 +94,13 @@ async def test_atoggle_installation_status_suspend(
9194
assert ainstallation.status == expected
9295

9396

94-
async def test_async_installation_data(ainstallation):
97+
async def test_async_installation_data(ainstallation, create_event):
9598
data = {
9699
"installation": {
97100
"id": ainstallation.installation_id,
98101
},
99102
}
100-
event = sansio.Event(data, event="installation", delivery_id="1234")
103+
event = create_event("installation", delivery_id="1234", **data)
101104

102105
assert ainstallation.data != data
103106

@@ -107,7 +110,7 @@ async def test_async_installation_data(ainstallation):
107110
assert ainstallation.data == data["installation"]
108111

109112

110-
async def test_async_installation_repositories(ainstallation):
113+
async def test_async_installation_repositories(ainstallation, create_event):
111114
existing_repo = await sync_to_async(baker.make)(
112115
"django_github_app.Repository",
113116
installation=ainstallation,
@@ -131,7 +134,7 @@ async def test_async_installation_repositories(ainstallation):
131134
}
132135
],
133136
}
134-
event = sansio.Event(data, event="installation", delivery_id="1234")
137+
event = create_event("installation", delivery_id="1234", **data)
135138

136139
assert await Repository.objects.filter(
137140
repository_id=data["repositories_removed"][0]["id"]

tests/events/test_arepository.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44
from asgiref.sync import sync_to_async
5-
from gidgethub import sansio
65
from model_bakery import baker
76

87
from django_github_app.events.arepository import arename_repository
@@ -12,7 +11,7 @@
1211
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db]
1312

1413

15-
async def test_arename_repository(ainstallation, repository_id):
14+
async def test_arename_repository(ainstallation, repository_id, create_event):
1615
repository = await sync_to_async(baker.make)(
1716
"django_github_app.Repository",
1817
installation=ainstallation,
@@ -26,7 +25,7 @@ async def test_arename_repository(ainstallation, repository_id):
2625
"full_name": f"owner/new_name_{seq.next()}",
2726
},
2827
}
29-
event = sansio.Event(data, event="repository", delivery_id="1234")
28+
event = create_event("repository", delivery_id="1234", **data)
3029

3130
assert not await Repository.objects.filter(
3231
full_name=data["repository"]["full_name"]

tests/events/test_installation.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import pytest
4-
from gidgethub.abc import sansio
54
from model_bakery import baker
65

76
from django_github_app.events.installation import create_installation
@@ -19,7 +18,11 @@
1918

2019
@pytest.mark.parametrize("app_settings_app_id_type", [int, str])
2120
def test_create_installation(
22-
app_settings_app_id_type, installation_id, repository_id, override_app_settings
21+
app_settings_app_id_type,
22+
installation_id,
23+
repository_id,
24+
override_app_settings,
25+
create_event,
2326
):
2427
data = {
2528
"installation": {
@@ -30,7 +33,7 @@ def test_create_installation(
3033
{"id": repository_id, "node_id": "node1234", "full_name": "owner/repo"}
3134
],
3235
}
33-
event = sansio.Event(data, event="installation", delivery_id="1234")
36+
event = create_event("installation", delivery_id="1234", **data)
3437

3538
with override_app_settings(
3639
APP_ID=data["installation"]["app_id"]
@@ -44,13 +47,13 @@ def test_create_installation(
4447
assert installation.data == data["installation"]
4548

4649

47-
def test_delete_installation(installation):
50+
def test_delete_installation(installation, create_event):
4851
data = {
4952
"installation": {
5053
"id": installation.installation_id,
5154
}
5255
}
53-
event = sansio.Event(data, event="installation", delivery_id="1234")
56+
event = create_event("installation", delivery_id="1234", **data)
5457

5558
delete_installation(event, None)
5659

@@ -66,7 +69,9 @@ def test_delete_installation(installation):
6669
(InstallationStatus.INACTIVE, "unsuspend", InstallationStatus.ACTIVE),
6770
],
6871
)
69-
def test_toggle_installation_status_suspend(status, action, expected, installation):
72+
def test_toggle_installation_status_suspend(
73+
status, action, expected, installation, create_event
74+
):
7075
installation.status = status
7176
installation.save()
7277

@@ -76,7 +81,7 @@ def test_toggle_installation_status_suspend(status, action, expected, installati
7681
"id": installation.installation_id,
7782
},
7883
}
79-
event = sansio.Event(data, event="installation", delivery_id="1234")
84+
event = create_event("installation", delivery_id="1234", **data)
8085

8186
assert installation.status != expected
8287

@@ -86,13 +91,13 @@ def test_toggle_installation_status_suspend(status, action, expected, installati
8691
assert installation.status == expected
8792

8893

89-
def test_sync_installation_data(installation):
94+
def test_sync_installation_data(installation, create_event):
9095
data = {
9196
"installation": {
9297
"id": installation.installation_id,
9398
},
9499
}
95-
event = sansio.Event(data, event="installation", delivery_id="1234")
100+
event = create_event("installation", delivery_id="1234", **data)
96101

97102
assert installation.data != data
98103

@@ -102,7 +107,7 @@ def test_sync_installation_data(installation):
102107
assert installation.data == data["installation"]
103108

104109

105-
def test_sync_installation_repositories(installation):
110+
def test_sync_installation_repositories(installation, create_event):
106111
existing_repo = baker.make(
107112
"django_github_app.Repository",
108113
installation=installation,
@@ -126,7 +131,7 @@ def test_sync_installation_repositories(installation):
126131
}
127132
],
128133
}
129-
event = sansio.Event(data, event="installation", delivery_id="1234")
134+
event = create_event("installation", delivery_id="1234", **data)
130135

131136
assert Repository.objects.filter(
132137
repository_id=data["repositories_removed"][0]["id"]

tests/events/test_repository.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import pytest
4-
from gidgethub import sansio
54
from model_bakery import baker
65

76
from django_github_app.events.repository import rename_repository
@@ -11,7 +10,7 @@
1110
pytestmark = [pytest.mark.django_db]
1211

1312

14-
def test_rename_repository(installation, repository_id):
13+
def test_rename_repository(installation, repository_id, create_event):
1514
repository = baker.make(
1615
"django_github_app.Repository",
1716
installation=installation,
@@ -25,7 +24,7 @@ def test_rename_repository(installation, repository_id):
2524
"full_name": f"owner/new_name_{seq.next()}",
2625
},
2726
}
28-
event = sansio.Event(data, event="repository", delivery_id="1234")
27+
event = create_event("repository", delivery_id="1234", **data)
2928

3029
assert not Repository.objects.filter(
3130
full_name=data["repository"]["full_name"]

0 commit comments

Comments
 (0)