Skip to content

Commit d289c50

Browse files
authored
Make all factories fixtures (#612)
* Make all factories fixtures explicitly * Add Actions factory * Replace actions_example fixture with actions_factory * Move fixtures in conftest to modules - if a fixture is only used in one module, move it to that module - where possible, avoid patching attributes of "fixture objects" and use factories to generate new objects instead
1 parent 47a0be9 commit d289c50

File tree

8 files changed

+444
-363
lines changed

8 files changed

+444
-363
lines changed

tests/conftest.py

Lines changed: 18 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,9 @@
1212
from jbi.app import app
1313
from jbi.configuration import get_actions
1414
from jbi.environment import Settings
15-
from jbi.models import (
16-
Action,
17-
ActionContext,
18-
Actions,
19-
BugzillaWebhookComment,
20-
BugzillaWebhookRequest,
21-
)
15+
from jbi.models import ActionContext, BugzillaWebhookComment, BugzillaWebhookRequest
2216
from jbi.services import bugzilla, jira
23-
from tests.fixtures import factories
17+
from tests.fixtures.factories import *
2418

2519

2620
class FilteredLogCaptureFixture(pytest.LogCaptureFixture):
@@ -107,160 +101,43 @@ def mocked_responses():
107101

108102

109103
@pytest.fixture
110-
def context_create_example() -> ActionContext:
111-
return factories.action_context_factory(
104+
def context_create_example(action_context_factory) -> ActionContext:
105+
return action_context_factory(
112106
operation=Operation.CREATE,
113107
)
114108

115109

116110
@pytest.fixture
117-
def context_update_example() -> ActionContext:
118-
bug = factories.bug_factory(
119-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"]
120-
)
121-
context = factories.action_context_factory(
122-
operation=Operation.UPDATE,
123-
bug=bug,
124-
jira=factories.jira_context_factory(issue=bug.extract_from_see_also()),
125-
)
126-
return context
127-
128-
129-
@pytest.fixture
130-
def context_update_status_assignee() -> ActionContext:
131-
bug = factories.bug_factory(
132-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"]
133-
)
134-
changes = [
135-
{
136-
"field": "status",
137-
"removed": "OPEN",
138-
"added": "FIXED",
139-
},
140-
{
141-
"field": "assignee",
142-
"removed": "[email protected]",
143-
"added": "[email protected]",
144-
},
145-
]
146-
event = factories.webhook_event_factory(routing_key="bug.modify", changes=changes)
147-
context = factories.action_context_factory(
148-
operation=Operation.UPDATE,
149-
bug=bug,
150-
event=event,
151-
jira=factories.jira_context_factory(issue=bug.extract_from_see_also()),
152-
)
153-
return context
154-
155-
156-
@pytest.fixture
157-
def context_comment_example() -> ActionContext:
158-
user = factories.webhook_user_factory(login="[email protected]")
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]")
159119
comment = BugzillaWebhookComment.parse_obj({"number": 2, "body": "hello"})
160-
bug = factories.bug_factory(
120+
bug = bug_factory(
161121
see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
162122
comment=comment,
163123
)
164-
event = factories.webhook_event_factory(target="comment", user=user)
165-
context = factories.action_context_factory(
124+
event = webhook_event_factory(target="comment", user=user)
125+
context = action_context_factory(
166126
operation=Operation.COMMENT,
167127
bug=bug,
168128
event=event,
169-
jira=factories.jira_context_factory(issue=bug.extract_from_see_also()),
170-
)
171-
return context
172-
173-
174-
@pytest.fixture
175-
def context_update_resolution_example() -> ActionContext:
176-
bug = factories.bug_factory(
177-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"]
178-
)
179-
event = factories.webhook_event_factory(
180-
action="modify",
181-
changes=[
182-
factories.webhook_event_change_factory(
183-
field="resolution", removed="OPEN", added="FIXED"
184-
),
185-
],
186-
)
187-
context = factories.action_context_factory(
188-
operation=Operation.UPDATE,
189-
bug=bug,
190-
event=event,
191-
jira=factories.jira_context_factory(issue=bug.extract_from_see_also()),
129+
jira=jira_context_factory(issue=bug.extract_from_see_also()),
192130
)
193131
return context
194132

195133

196134
@pytest.fixture
197-
def webhook_create_example() -> BugzillaWebhookRequest:
198-
webhook_payload = factories.webhook_factory()
135+
def webhook_create_example(webhook_factory) -> BugzillaWebhookRequest:
136+
webhook_payload = webhook_factory()
199137

200138
return webhook_payload
201139

202140

203-
@pytest.fixture
204-
def webhook_comment_example() -> BugzillaWebhookRequest:
205-
user = factories.webhook_user_factory(login="[email protected]")
206-
comment = BugzillaWebhookComment.parse_obj({"number": 2, "body": "hello"})
207-
bug = factories.bug_factory(
208-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
209-
comment=comment,
210-
)
211-
event = factories.webhook_event_factory(target="comment", user=user)
212-
webhook_payload = factories.webhook_factory(bug=bug, event=event)
213-
214-
return webhook_payload
215-
216-
217-
@pytest.fixture
218-
def webhook_private_comment_example() -> BugzillaWebhookRequest:
219-
user = factories.webhook_user_factory(login="[email protected]")
220-
event = factories.webhook_event_factory(target="comment", user=user)
221-
bug = factories.bug_factory(
222-
comment={"id": 344, "number": 2, "is_private": True},
223-
see_also=["https://mozilla.atlassian.net/browse/JBI-234"],
224-
)
225-
webhook_payload = factories.webhook_factory(bug=bug, event=event)
226-
return webhook_payload
227-
228-
229-
@pytest.fixture
230-
def webhook_change_status_assignee():
231-
changes = [
232-
factories.webhook_event_change_factory(
233-
field="status", removed="OPEN", added="FIXED"
234-
),
235-
factories.webhook_event_change_factory(
236-
field="assignee", removed="[email protected]", added="[email protected]"
237-
),
238-
]
239-
event = factories.webhook_event_factory(routing_key="bug.modify", changes=changes)
240-
webhook_payload = factories.webhook_factory(event=event)
241-
return webhook_payload
242-
243-
244-
@pytest.fixture
245-
def action_params_factory():
246-
return factories.action_params_factory
247-
248-
249-
@pytest.fixture
250-
def action_factory():
251-
return factories.action_factory
252-
253-
254-
@pytest.fixture
255-
def action_example() -> Action:
256-
return factories.action_factory()
257-
258-
259-
@pytest.fixture
260-
def actions_example(action_example) -> Actions:
261-
return Actions.parse_obj([action_example])
262-
263-
264141
@pytest.fixture(autouse=True)
265142
def sleepless(monkeypatch):
266143
# https://stackoverflow.com/a/54829577

0 commit comments

Comments
 (0)