Skip to content

Commit d805cd3

Browse files
committed
refactor: conftest: Generate msg templates from factory function.
This commit adds a new function msg_template_factory that can generate templates for all message types, i.e. stream/PM/group with minimum input parameters. This new factory simplifies the code and reduces duplication in tests. All the different message template functions are defined as fixtures so that they can be reused throughout the tests. This new factory also makes it easy to create a large number of output templates with minimal modification of the input.
1 parent 303ef4a commit d805cd3

File tree

1 file changed

+61
-66
lines changed

1 file changed

+61
-66
lines changed

tests/conftest.py

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections import OrderedDict, defaultdict
22
from copy import deepcopy
3-
from typing import Any, Dict, List, Tuple
3+
from typing import Any, Dict, List, Optional, Tuple, Union
44

55
import pytest
66

@@ -289,85 +289,80 @@ def display_recipient_factory(recipient_details_list: List[Tuple[int, str]]):
289289
]
290290

291291

292-
stream_msg_template = {
293-
"id": 537286,
294-
"sender_full_name": "Foo Foo",
295-
"timestamp": 1520918722,
296-
"client": "website",
297-
"sender_email": "[email protected]",
298-
"type": "stream",
299-
"sender_realm_str": "",
300-
"flags": ["read"],
301-
"sender_id": 5140,
302-
"content_type": "text/x-markdown",
303-
"stream_id": 205,
304-
"subject": "Test",
305-
"reactions": [],
306-
"subject_links": [],
307-
"avatar_url": "dummy_avatar_url",
308-
"is_me_message": False,
309-
"content": "Stream content here.",
310-
"display_recipient": "PTEST",
311-
}
292+
def msg_template_factory(
293+
msg_id: int,
294+
msg_type: str,
295+
timestamp: int,
296+
*,
297+
subject: str = "",
298+
stream_id: Optional[int] = None,
299+
recipients: Union[str, List[Dict[str, Any]]] = "PTEST",
300+
):
301+
"""
302+
Generate message template for all types of messages(stream/PM/group)
303+
"""
304+
return {
305+
"id": msg_id,
306+
"sender_full_name": "Foo Foo",
307+
"timestamp": timestamp,
308+
"client": "website",
309+
"sender_email": "[email protected]",
310+
"type": msg_type,
311+
"sender_realm_str": "",
312+
"flags": ["read"],
313+
"sender_id": 5140,
314+
"content_type": "text/x-markdown",
315+
"stream_id": stream_id,
316+
"subject": subject,
317+
"reactions": [],
318+
"subject_links": [],
319+
"avatar_url": "dummy_avatar_url",
320+
"is_me_message": False,
321+
"content": f"{msg_type} content here.",
322+
"display_recipient": recipients,
323+
}
312324

313-
pm_template = {
314-
"id": 537287,
315-
"sender_full_name": "Foo Foo",
316-
"timestamp": 1520918736,
317-
"client": "website",
318-
"is_me_message": False,
319-
"sender_email": "[email protected]",
320-
"flags": ["read"],
321-
"sender_id": 5140,
322-
"content_type": "text/x-markdown",
323-
"sender_realm_str": "",
324-
"subject": "",
325-
"reactions": [],
326-
"type": "private",
327-
"avatar_url": "dummy_avatar_url",
328-
"subject_links": [],
329-
"content": "Hey PM content here.",
330-
"display_recipient": display_recipient_factory(
331-
[(5179, "Boo Boo"), (5140, "Foo Foo")]
332-
),
333-
}
334325

335-
group_pm_template = {
336-
"id": 537288,
337-
"sender_full_name": "Foo Foo",
338-
"timestamp": 1520918737,
339-
"client": "website",
340-
"is_me_message": False,
341-
"sender_email": "[email protected]",
342-
"flags": ["read"],
343-
"sender_id": 5140,
344-
"content_type": "text/x-markdown",
345-
"sender_realm_str": "",
346-
"subject": "",
347-
"reactions": [],
348-
"type": "private",
349-
"avatar_url": "dummy_avatar_url",
350-
"subject_links": [],
351-
"content": "Hey PM content here again.",
352-
"display_recipient": display_recipient_factory(
326+
@pytest.fixture
327+
def stream_msg_template():
328+
msg_template = msg_template_factory(
329+
537286, "stream", 1520918722, subject="PTEST", stream_id=205)
330+
msg_template["subject"] = "Test"
331+
return msg_template
332+
333+
334+
@pytest.fixture
335+
def pm_template():
336+
recipients = display_recipient_factory([(5179, "Boo Boo"), (5140, "Foo Foo")])
337+
return msg_template_factory(537287, "private", 1520918736, recipients=recipients)
338+
339+
340+
@pytest.fixture
341+
def group_pm_template():
342+
recipients = display_recipient_factory(
353343
[(5179, "Boo Boo"), (5140, "Foo Foo"), (5180, "Bar Bar")]
354-
),
355-
}
344+
)
345+
return msg_template_factory(537288, "private", 1520918737, recipients=recipients)
356346

357347

358348
@pytest.fixture(
359-
params=[stream_msg_template, pm_template, group_pm_template],
349+
params=["stream_msg_template", "pm_template", "group_pm_template"],
360350
ids=["stream_message", "pm_message", "group_pm_message"],
361351
)
362352
def message_fixture(request):
363353
"""
364354
Acts as a parametrize fixture for stream msg, pms and group_pms.
365355
"""
366-
return deepcopy(request.param)
356+
template = request.getfixturevalue(request.param)
357+
return template
367358

368359

369360
@pytest.fixture
370-
def messages_successful_response() -> Dict[str, Any]:
361+
def messages_successful_response(
362+
stream_msg_template,
363+
pm_template,
364+
group_pm_template,
365+
) -> Dict[str, Any]:
371366
"""
372367
A successful response from a /messages API query.
373368
"""
@@ -640,7 +635,7 @@ def initial_index():
640635

641636

642637
@pytest.fixture
643-
def empty_index():
638+
def empty_index(stream_msg_template, pm_template, group_pm_template):
644639
return deepcopy(
645640
{
646641
"pointer": defaultdict(set, {}),

0 commit comments

Comments
 (0)