Skip to content

Commit e2dc2e4

Browse files
zee-bitneiljp
authored andcommitted
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 6fad946 commit e2dc2e4

File tree

1 file changed

+69
-66
lines changed

1 file changed

+69
-66
lines changed

tests/conftest.py

Lines changed: 69 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

@@ -288,85 +288,88 @@ def display_recipient_factory(recipient_details_list: List[Tuple[int, str]]):
288288
]
289289

290290

291-
stream_msg_template = {
292-
"id": 537286,
293-
"sender_full_name": "Foo Foo",
294-
"timestamp": 1520918722,
295-
"client": "website",
296-
"sender_email": "[email protected]",
297-
"type": "stream",
298-
"sender_realm_str": "",
299-
"flags": ["read"],
300-
"sender_id": 5140,
301-
"content_type": "text/x-markdown",
302-
"stream_id": 205,
303-
"subject": "Test",
304-
"reactions": [],
305-
"subject_links": [],
306-
"avatar_url": "dummy_avatar_url",
307-
"is_me_message": False,
308-
"content": "Stream content here.",
309-
"display_recipient": "PTEST",
310-
}
291+
def msg_template_factory(
292+
msg_id: int,
293+
msg_type: str,
294+
timestamp: int,
295+
*,
296+
subject: str = "",
297+
stream_id: Optional[int] = None,
298+
recipients: Union[str, List[Dict[str, Any]]] = "PTEST",
299+
):
300+
"""
301+
Generate message template for all types of messages(stream/PM/group)
302+
"""
303+
if msg_type == "stream":
304+
assert isinstance(stream_id, int)
305+
assert isinstance(recipients, str)
306+
else:
307+
assert isinstance(recipients, list)
308+
for _val in recipients:
309+
assert isinstance(_val, dict)
310+
311+
return {
312+
"id": msg_id,
313+
"sender_full_name": "Foo Foo",
314+
"timestamp": timestamp,
315+
"client": "website",
316+
"sender_email": "[email protected]",
317+
"type": msg_type,
318+
"sender_realm_str": "",
319+
"flags": ["read"],
320+
"sender_id": 5140,
321+
"content_type": "text/x-markdown",
322+
"stream_id": stream_id,
323+
"subject": subject,
324+
"reactions": [],
325+
"subject_links": [],
326+
"avatar_url": "dummy_avatar_url",
327+
"is_me_message": False,
328+
"content": f"{msg_type} content here.",
329+
"display_recipient": recipients,
330+
}
331+
332+
333+
@pytest.fixture
334+
def stream_msg_template():
335+
msg_template = msg_template_factory(
336+
537286, "stream", 1520918722, subject="Test", stream_id=205
337+
)
338+
return msg_template
339+
340+
341+
@pytest.fixture
342+
def pm_template():
343+
recipients = display_recipient_factory([(5179, "Boo Boo"), (5140, "Foo Foo")])
344+
return msg_template_factory(537287, "private", 1520918736, recipients=recipients)
311345

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

334-
group_pm_template = {
335-
"id": 537288,
336-
"sender_full_name": "Foo Foo",
337-
"timestamp": 1520918737,
338-
"client": "website",
339-
"is_me_message": False,
340-
"sender_email": "[email protected]",
341-
"flags": ["read"],
342-
"sender_id": 5140,
343-
"content_type": "text/x-markdown",
344-
"sender_realm_str": "",
345-
"subject": "",
346-
"reactions": [],
347-
"type": "private",
348-
"avatar_url": "dummy_avatar_url",
349-
"subject_links": [],
350-
"content": "Hey PM content here again.",
351-
"display_recipient": display_recipient_factory(
347+
@pytest.fixture
348+
def group_pm_template():
349+
recipients = display_recipient_factory(
352350
[(5179, "Boo Boo"), (5140, "Foo Foo"), (5180, "Bar Bar")]
353-
),
354-
}
351+
)
352+
return msg_template_factory(537288, "private", 1520918737, recipients=recipients)
355353

356354

357355
@pytest.fixture(
358-
params=[stream_msg_template, pm_template, group_pm_template],
356+
params=["stream_msg_template", "pm_template", "group_pm_template"],
359357
ids=["stream_message", "pm_message", "group_pm_message"],
360358
)
361359
def message_fixture(request):
362360
"""
363361
Acts as a parametrize fixture for stream msg, pms and group_pms.
364362
"""
365-
return deepcopy(request.param)
363+
template = request.getfixturevalue(request.param)
364+
return template
366365

367366

368367
@pytest.fixture
369-
def messages_successful_response() -> Dict[str, Any]:
368+
def messages_successful_response(
369+
stream_msg_template,
370+
pm_template,
371+
group_pm_template,
372+
) -> Dict[str, Any]:
370373
"""
371374
A successful response from a /messages API query.
372375
"""
@@ -653,7 +656,7 @@ def initial_index():
653656

654657

655658
@pytest.fixture
656-
def empty_index():
659+
def empty_index(stream_msg_template, pm_template, group_pm_template):
657660
return deepcopy(
658661
{
659662
"pointer": defaultdict(set, {}),

0 commit comments

Comments
 (0)