|
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 |
|
@@ -288,85 +288,88 @@ def display_recipient_factory(recipient_details_list: List[Tuple[int, str]]):
|
288 | 288 | ]
|
289 | 289 |
|
290 | 290 |
|
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) |
311 | 345 |
|
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 |
| -} |
333 | 346 |
|
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( |
352 | 350 | [(5179, "Boo Boo"), (5140, "Foo Foo"), (5180, "Bar Bar")]
|
353 |
| - ), |
354 |
| -} |
| 351 | + ) |
| 352 | + return msg_template_factory(537288, "private", 1520918737, recipients=recipients) |
355 | 353 |
|
356 | 354 |
|
357 | 355 | @pytest.fixture(
|
358 |
| - params=[stream_msg_template, pm_template, group_pm_template], |
| 356 | + params=["stream_msg_template", "pm_template", "group_pm_template"], |
359 | 357 | ids=["stream_message", "pm_message", "group_pm_message"],
|
360 | 358 | )
|
361 | 359 | def message_fixture(request):
|
362 | 360 | """
|
363 | 361 | Acts as a parametrize fixture for stream msg, pms and group_pms.
|
364 | 362 | """
|
365 |
| - return deepcopy(request.param) |
| 363 | + template = request.getfixturevalue(request.param) |
| 364 | + return template |
366 | 365 |
|
367 | 366 |
|
368 | 367 | @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]: |
370 | 373 | """
|
371 | 374 | A successful response from a /messages API query.
|
372 | 375 | """
|
@@ -653,7 +656,7 @@ def initial_index():
|
653 | 656 |
|
654 | 657 |
|
655 | 658 | @pytest.fixture
|
656 |
| -def empty_index(): |
| 659 | +def empty_index(stream_msg_template, pm_template, group_pm_template): |
657 | 660 | return deepcopy(
|
658 | 661 | {
|
659 | 662 | "pointer": defaultdict(set, {}),
|
|
0 commit comments