Skip to content

Commit 9964d28

Browse files
authored
Use factory-boy and pytest-factoryboy to make object factories and fixtures (#643)
1 parent 6528aef commit 9964d28

File tree

7 files changed

+289
-285
lines changed

7 files changed

+289
-285
lines changed

poetry.lock

Lines changed: 76 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pytest-dotenv = "^0.5.2"
3535
types-requests = "^2.31.0"
3636
responses = "^0.23.2"
3737
httpx = "^0.24.1"
38+
factory-boy = "^3.3.0"
39+
pytest-factoryboy = "^2.5.1"
3840

3941
[build-system]
4042
requires = ["poetry-core>=1.0.0"]

tests/conftest.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
import pytest
88
import responses
99
from fastapi.testclient import TestClient
10+
from pytest_factoryboy import register
1011

1112
from jbi import Operation
1213
from jbi.app import app
1314
from jbi.configuration import get_actions
1415
from jbi.environment import Settings
15-
from jbi.models import ActionContext, BugzillaWebhookComment, BugzillaWebhookRequest
16+
from jbi.models import ActionContext, BugzillaWebhookRequest
1617
from jbi.services import bugzilla, jira
1718
from tests.fixtures.factories import *
1819

@@ -54,6 +55,20 @@ def mocked_statsd():
5455
yield _mocked_statsd
5556

5657

58+
register(ActionContextFactory)
59+
register(ActionFactory)
60+
register(ActionsFactory)
61+
register(ActionParamsFactory)
62+
register(BugFactory)
63+
register(BugzillaWebhookFactory)
64+
register(CommentFactory)
65+
register(JiraContextFactory)
66+
register(WebhookFactory)
67+
register(WebhookEventChangeFactory)
68+
register(WebhookEventFactory)
69+
register(WebhookUserFactory)
70+
71+
5772
@pytest.fixture
5873
def anon_client():
5974
"""A test client with no authorization."""
@@ -100,42 +115,24 @@ def mocked_responses():
100115
yield rsps
101116

102117

103-
@pytest.fixture
104-
def context_create_example(action_context_factory) -> ActionContext:
105-
return action_context_factory(
106-
operation=Operation.CREATE,
107-
)
118+
register(ActionContextFactory, "context_create_example", operation=Operation.CREATE)
108119

109120

110121
@pytest.fixture
111-
def context_comment_example(
112-
webhook_user_factory,
113-
bug_factory,
114-
webhook_event_factory,
115-
action_context_factory,
116-
jira_context_factory,
117-
) -> ActionContext:
118-
user = webhook_user_factory(login="[email protected]")
119-
comment = BugzillaWebhookComment.parse_obj({"number": 2, "body": "hello"})
120-
bug = bug_factory(
121-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
122-
comment=comment,
123-
)
124-
event = webhook_event_factory(target="comment", user=user)
125-
context = action_context_factory(
122+
def context_comment_example(action_context_factory) -> ActionContext:
123+
return action_context_factory(
126124
operation=Operation.COMMENT,
127-
bug=bug,
128-
event=event,
129-
jira=jira_context_factory(issue=bug.extract_from_see_also()),
125+
bug__see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
126+
bug__with_comment=True,
127+
bug__comment__number=2,
128+
bug__comment__body="hello",
129+
event__target="comment",
130+
event__user__login="[email protected]",
131+
jira__issue="JBI-234",
130132
)
131-
return context
132133

133134

134-
@pytest.fixture
135-
def webhook_create_example(webhook_factory) -> BugzillaWebhookRequest:
136-
webhook_payload = webhook_factory()
137-
138-
return webhook_payload
135+
register(WebhookFactory, "webhook_create_example")
139136

140137

141138
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)