|
1 | 1 | from collections import OrderedDict, defaultdict
|
2 | 2 | from copy import deepcopy
|
3 |
| -from typing import Any, Dict, List, Tuple |
| 3 | +from typing import Any, Dict, List, Optional, Tuple, Union |
4 | 4 |
|
5 | 5 | import pytest
|
6 | 6 |
|
@@ -289,85 +289,80 @@ def display_recipient_factory(recipient_details_list: List[Tuple[int, str]]):
|
289 | 289 | ]
|
290 | 290 |
|
291 | 291 |
|
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 | + } |
312 | 324 |
|
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 |
| -} |
334 | 325 |
|
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( |
353 | 343 | [(5179, "Boo Boo"), (5140, "Foo Foo"), (5180, "Bar Bar")]
|
354 |
| - ), |
355 |
| -} |
| 344 | + ) |
| 345 | + return msg_template_factory(537288, "private", 1520918737, recipients=recipients) |
356 | 346 |
|
357 | 347 |
|
358 | 348 | @pytest.fixture(
|
359 |
| - params=[stream_msg_template, pm_template, group_pm_template], |
| 349 | + params=["stream_msg_template", "pm_template", "group_pm_template"], |
360 | 350 | ids=["stream_message", "pm_message", "group_pm_message"],
|
361 | 351 | )
|
362 | 352 | def message_fixture(request):
|
363 | 353 | """
|
364 | 354 | Acts as a parametrize fixture for stream msg, pms and group_pms.
|
365 | 355 | """
|
366 |
| - return deepcopy(request.param) |
| 356 | + template = request.getfixturevalue(request.param) |
| 357 | + return template |
367 | 358 |
|
368 | 359 |
|
369 | 360 | @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]: |
371 | 366 | """
|
372 | 367 | A successful response from a /messages API query.
|
373 | 368 | """
|
@@ -640,7 +635,7 @@ def initial_index():
|
640 | 635 |
|
641 | 636 |
|
642 | 637 | @pytest.fixture
|
643 |
| -def empty_index(): |
| 638 | +def empty_index(stream_msg_template, pm_template, group_pm_template): |
644 | 639 | return deepcopy(
|
645 | 640 | {
|
646 | 641 | "pointer": defaultdict(set, {}),
|
|
0 commit comments