Skip to content

Commit 2c21f94

Browse files
create new override_app_settings pytest fixture (#3)
1 parent 0fd67d8 commit 2c21f94

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import logging
45
from unittest.mock import AsyncMock
56
from unittest.mock import MagicMock
67

78
import pytest
89
from asgiref.sync import sync_to_async
910
from django.conf import settings
11+
from django.test import override_settings
1012
from model_bakery import baker
1113

14+
from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
1215
from django_github_app.github import AsyncGitHubAPI
1316

1417
from .settings import DEFAULT_SETTINGS
@@ -55,6 +58,16 @@ def pytest_configure(config):
5558
}
5659

5760

61+
@pytest.fixture
62+
def override_app_settings():
63+
@contextlib.contextmanager
64+
def _override_app_settings(**kwargs):
65+
with override_settings(**{GITHUB_APP_SETTINGS_NAME: {**kwargs}}):
66+
yield
67+
68+
return _override_app_settings
69+
70+
5871
@pytest.fixture
5972
def installation_id():
6073
return seq.next()

tests/events/test_installation.py

Lines changed: 4 additions & 5 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 django.test import override_settings
65
from gidgethub.abc import sansio
76
from model_bakery import baker
87

@@ -19,7 +18,9 @@
1918
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db]
2019

2120

22-
async def test_create_installation(installation_id, repository_id):
21+
async def test_create_installation(
22+
installation_id, repository_id, override_app_settings
23+
):
2324
data = {
2425
"installation": {
2526
"id": installation_id,
@@ -31,9 +32,7 @@ async def test_create_installation(installation_id, repository_id):
3132
}
3233
event = sansio.Event(data, event="installation", delivery_id="1234")
3334

34-
with override_settings(
35-
DJANGO_GITHUB_APP={"APP_ID": str(data["installation"]["app_id"])}
36-
):
35+
with override_app_settings(APP_ID=str(data["installation"]["app_id"])):
3736
await create_installation(event, None)
3837

3938
installation = await Installation.objects.aget(

tests/test_conf.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import pytest
44
from django.conf import settings
5-
from django.test import override_settings
65

6+
from django_github_app.conf import GITHUB_APP_SETTINGS_NAME
77
from django_github_app.conf import app_settings
88

99

@@ -20,7 +20,7 @@
2020
],
2121
)
2222
def test_default_settings(setting, default_setting):
23-
user_settings = getattr(settings, "DJANGO_GITHUB_APP", {})
23+
user_settings = getattr(settings, GITHUB_APP_SETTINGS_NAME, {})
2424

2525
assert user_settings == {}
2626
assert getattr(app_settings, setting) == default_setting
@@ -46,10 +46,6 @@ def test_default_settings(setting, default_setting):
4646
("v1.0.0-beta", "v100-beta"),
4747
],
4848
)
49-
def test_slug(name, expected):
50-
with override_settings(
51-
DJANGO_GITHUB_APP={
52-
"NAME": name,
53-
},
54-
):
49+
def test_slug(name, expected, override_app_settings):
50+
with override_app_settings(NAME=name):
5551
assert app_settings.SLUG == expected

tests/test_models.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pytest
66
from asgiref.sync import sync_to_async
7-
from django.test import override_settings
87
from django.utils import timezone
98
from gidgethub import sansio
109
from model_bakery import baker
@@ -109,7 +108,7 @@ def test_action_property(self, payload, expected):
109108

110109
class TestInstallationManager:
111110
@pytest.mark.asyncio
112-
async def test_acreate_from_event(self, create_event):
111+
async def test_acreate_from_event(self, create_event, override_app_settings):
113112
repositories = [
114113
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
115114
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
@@ -126,9 +125,7 @@ async def test_acreate_from_event(self, create_event):
126125
"installation",
127126
)
128127

129-
with override_settings(
130-
DJANGO_GITHUB_APP={"APP_ID": str(installation_data["app_id"])}
131-
):
128+
with override_app_settings(APP_ID=str(installation_data["app_id"])):
132129
installation = await Installation.objects.acreate_from_event(event)
133130

134131
assert installation.installation_id == installation_data["id"]
@@ -137,7 +134,7 @@ async def test_acreate_from_event(self, create_event):
137134
installation=installation
138135
).acount() == len(repositories)
139136

140-
def test_create_from_event(self, create_event):
137+
def test_create_from_event(self, create_event, override_app_settings):
141138
repositories = [
142139
{"id": seq.next(), "node_id": "node1", "full_name": "owner/repo1"},
143140
{"id": seq.next(), "node_id": "node2", "full_name": "owner/repo2"},
@@ -154,9 +151,7 @@ def test_create_from_event(self, create_event):
154151
"installation",
155152
)
156153

157-
with override_settings(
158-
DJANGO_GITHUB_APP={"APP_ID": str(installation_data["app_id"])}
159-
):
154+
with override_app_settings(APP_ID=str(installation_data["app_id"])):
160155
installation = Installation.objects.create_from_event(event)
161156

162157
assert installation.installation_id == installation_data["id"]

tests/test_views.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from asgiref.sync import sync_to_async
1010
from django.core.exceptions import BadRequest
1111
from django.http import JsonResponse
12-
from django.test import override_settings
1312
from django.utils import timezone
1413
from gidgethub import sansio
1514
from model_bakery import baker
@@ -28,8 +27,8 @@
2827

2928

3029
@pytest.fixture(autouse=True)
31-
def webhook_secret():
32-
with override_settings(DJANGO_GITHUB_APP={"WEBHOOK_SECRET": WEBHOOK_SECRET}):
30+
def webhook_secret(override_app_settings):
31+
with override_app_settings(WEBHOOK_SECRET=WEBHOOK_SECRET):
3332
yield
3433

3534

0 commit comments

Comments
 (0)