Skip to content

Consolidate event creation in test suite with faker-based fixture #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.test import override_settings
from django.urls import clear_url_caches
from django.urls import path
from faker import Faker
from gidgethub import sansio

from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
from django_github_app.github import AsyncGitHubAPI
Expand Down Expand Up @@ -232,3 +234,39 @@ async def arepository(ainstallation, get_mock_github_api, baker):
mock_github_api.installation_id = repository.installation.installation_id
repository.get_gh_client = MagicMock(return_value=mock_github_api)
return repository


@pytest.fixture
def faker():
return Faker()


@pytest.fixture
def create_event(faker):
def _create_event(event_type, delivery_id=None, **data):
if delivery_id is None:
delivery_id = seq.next()

if event_type == "issue_comment" and "comment" not in data:
data["comment"] = {"body": faker.sentence()}

if "comment" in data and isinstance(data["comment"], str):
# Allow passing just the comment body as a string
data["comment"] = {"body": data["comment"]}

if "comment" in data and "user" not in data["comment"]:
data["comment"]["user"] = {"login": faker.user_name()}

if "repository" not in data and event_type in [
"issue_comment",
"pull_request",
"push",
]:
data["repository"] = {
"owner": {"login": faker.user_name()},
"name": faker.slug(),
}

return sansio.Event(data=data, event=event_type, delivery_id=delivery_id)

return _create_event
25 changes: 14 additions & 11 deletions tests/events/test_ainstallation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from asgiref.sync import sync_to_async
from gidgethub.abc import sansio
from model_bakery import baker

from django_github_app.events.ainstallation import acreate_installation
Expand All @@ -20,7 +19,11 @@

@pytest.mark.parametrize("app_settings_app_id_type", [int, str])
async def test_acreate_installation(
app_settings_app_id_type, installation_id, repository_id, override_app_settings
app_settings_app_id_type,
installation_id,
repository_id,
override_app_settings,
create_event,
):
data = {
"installation": {
Expand All @@ -31,7 +34,7 @@ async def test_acreate_installation(
{"id": repository_id, "node_id": "node1234", "full_name": "owner/repo"}
],
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

with override_app_settings(
APP_ID=data["installation"]["app_id"]
Expand All @@ -47,13 +50,13 @@ async def test_acreate_installation(
assert installation.data == data["installation"]


async def test_adelete_installation(ainstallation):
async def test_adelete_installation(ainstallation, create_event):
data = {
"installation": {
"id": ainstallation.installation_id,
}
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

await adelete_installation(event, None)

Expand All @@ -70,7 +73,7 @@ async def test_adelete_installation(ainstallation):
],
)
async def test_atoggle_installation_status_suspend(
status, action, expected, ainstallation
status, action, expected, ainstallation, create_event
):
ainstallation.status = status
await ainstallation.asave()
Expand All @@ -81,7 +84,7 @@ async def test_atoggle_installation_status_suspend(
"id": ainstallation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert ainstallation.status != expected

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


async def test_async_installation_data(ainstallation):
async def test_async_installation_data(ainstallation, create_event):
data = {
"installation": {
"id": ainstallation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert ainstallation.data != data

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


async def test_async_installation_repositories(ainstallation):
async def test_async_installation_repositories(ainstallation, create_event):
existing_repo = await sync_to_async(baker.make)(
"django_github_app.Repository",
installation=ainstallation,
Expand All @@ -131,7 +134,7 @@ async def test_async_installation_repositories(ainstallation):
}
],
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert await Repository.objects.filter(
repository_id=data["repositories_removed"][0]["id"]
Expand Down
5 changes: 2 additions & 3 deletions tests/events/test_arepository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from asgiref.sync import sync_to_async
from gidgethub import sansio
from model_bakery import baker

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


async def test_arename_repository(ainstallation, repository_id):
async def test_arename_repository(ainstallation, repository_id, create_event):
repository = await sync_to_async(baker.make)(
"django_github_app.Repository",
installation=ainstallation,
Expand All @@ -26,7 +25,7 @@ async def test_arename_repository(ainstallation, repository_id):
"full_name": f"owner/new_name_{seq.next()}",
},
}
event = sansio.Event(data, event="repository", delivery_id="1234")
event = create_event("repository", delivery_id="1234", **data)

assert not await Repository.objects.filter(
full_name=data["repository"]["full_name"]
Expand Down
27 changes: 16 additions & 11 deletions tests/events/test_installation.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import pytest
from gidgethub.abc import sansio
from model_bakery import baker

from django_github_app.events.installation import create_installation
Expand All @@ -19,7 +18,11 @@

@pytest.mark.parametrize("app_settings_app_id_type", [int, str])
def test_create_installation(
app_settings_app_id_type, installation_id, repository_id, override_app_settings
app_settings_app_id_type,
installation_id,
repository_id,
override_app_settings,
create_event,
):
data = {
"installation": {
Expand All @@ -30,7 +33,7 @@ def test_create_installation(
{"id": repository_id, "node_id": "node1234", "full_name": "owner/repo"}
],
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

with override_app_settings(
APP_ID=data["installation"]["app_id"]
Expand All @@ -44,13 +47,13 @@ def test_create_installation(
assert installation.data == data["installation"]


def test_delete_installation(installation):
def test_delete_installation(installation, create_event):
data = {
"installation": {
"id": installation.installation_id,
}
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

delete_installation(event, None)

Expand All @@ -66,7 +69,9 @@ def test_delete_installation(installation):
(InstallationStatus.INACTIVE, "unsuspend", InstallationStatus.ACTIVE),
],
)
def test_toggle_installation_status_suspend(status, action, expected, installation):
def test_toggle_installation_status_suspend(
status, action, expected, installation, create_event
):
installation.status = status
installation.save()

Expand All @@ -76,7 +81,7 @@ def test_toggle_installation_status_suspend(status, action, expected, installati
"id": installation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert installation.status != expected

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


def test_sync_installation_data(installation):
def test_sync_installation_data(installation, create_event):
data = {
"installation": {
"id": installation.installation_id,
},
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert installation.data != data

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


def test_sync_installation_repositories(installation):
def test_sync_installation_repositories(installation, create_event):
existing_repo = baker.make(
"django_github_app.Repository",
installation=installation,
Expand All @@ -126,7 +131,7 @@ def test_sync_installation_repositories(installation):
}
],
}
event = sansio.Event(data, event="installation", delivery_id="1234")
event = create_event("installation", delivery_id="1234", **data)

assert Repository.objects.filter(
repository_id=data["repositories_removed"][0]["id"]
Expand Down
5 changes: 2 additions & 3 deletions tests/events/test_repository.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import pytest
from gidgethub import sansio
from model_bakery import baker

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


def test_rename_repository(installation, repository_id):
def test_rename_repository(installation, repository_id, create_event):
repository = baker.make(
"django_github_app.Repository",
installation=installation,
Expand All @@ -25,7 +24,7 @@ def test_rename_repository(installation, repository_id):
"full_name": f"owner/new_name_{seq.next()}",
},
}
event = sansio.Event(data, event="repository", delivery_id="1234")
event = create_event("repository", delivery_id="1234", **data)

assert not Repository.objects.filter(
full_name=data["repository"]["full_name"]
Expand Down
Loading