|
| 1 | +from collections import namedtuple |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from app.utils import get_warnings_for_csv |
| 6 | + |
| 7 | +# A minimal stand-in for ``RecipientCSV`` covering only the attributes consumed |
| 8 | +# by ``get_warnings_for_csv``. Keeps these tests independent of the real |
| 9 | +# notifications-utils CSV parser. |
| 10 | +MockRecipients = namedtuple( |
| 11 | + "MockRecipients", |
| 12 | + [ |
| 13 | + "has_duplicate_recipients", |
| 14 | + "count_of_unique_duplicate_recipients", |
| 15 | + "count_of_duplicate_recipient_rows", |
| 16 | + "template_type", |
| 17 | + ], |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "template_type", |
| 23 | + ["sms", "email"], |
| 24 | +) |
| 25 | +def test_returns_empty_list_when_no_duplicates(app_, template_type): |
| 26 | + recipients = MockRecipients( |
| 27 | + has_duplicate_recipients=False, |
| 28 | + count_of_unique_duplicate_recipients=0, |
| 29 | + count_of_duplicate_recipient_rows=0, |
| 30 | + template_type=template_type, |
| 31 | + ) |
| 32 | + with app_.test_request_context(): |
| 33 | + assert get_warnings_for_csv(recipients, template_type) == [] |
| 34 | + |
| 35 | + |
| 36 | +def test_returns_singular_warning_for_one_duplicate_recipient(app_): |
| 37 | + recipients = MockRecipients( |
| 38 | + has_duplicate_recipients=True, |
| 39 | + count_of_unique_duplicate_recipients=1, |
| 40 | + count_of_duplicate_recipient_rows=1, |
| 41 | + template_type="email", |
| 42 | + ) |
| 43 | + with app_.test_request_context(): |
| 44 | + warnings = get_warnings_for_csv(recipients, "email") |
| 45 | + assert len(warnings) == 1 |
| 46 | + assert "1 recipient appears in your list more than once" in warnings[0] |
| 47 | + |
| 48 | + |
| 49 | +def test_returns_plural_warning_for_multiple_duplicate_recipients(app_): |
| 50 | + recipients = MockRecipients( |
| 51 | + has_duplicate_recipients=True, |
| 52 | + count_of_unique_duplicate_recipients=3, |
| 53 | + count_of_duplicate_recipient_rows=3, |
| 54 | + template_type="email", |
| 55 | + ) |
| 56 | + with app_.test_request_context(): |
| 57 | + warnings = get_warnings_for_csv(recipients, "email") |
| 58 | + assert len(warnings) == 1 |
| 59 | + assert "3 recipients appear in your list more than once" in warnings[0] |
| 60 | + |
| 61 | + |
| 62 | +def test_includes_row_count_when_it_differs_from_unique_count(app_): |
| 63 | + # one recipient appears 4 times → 1 unique duplicate, 3 duplicate rows |
| 64 | + recipients = MockRecipients( |
| 65 | + has_duplicate_recipients=True, |
| 66 | + count_of_unique_duplicate_recipients=1, |
| 67 | + count_of_duplicate_recipient_rows=3, |
| 68 | + template_type="sms", |
| 69 | + ) |
| 70 | + with app_.test_request_context(): |
| 71 | + warnings = get_warnings_for_csv(recipients, "sms") |
| 72 | + assert len(warnings) == 2 |
| 73 | + assert "1 recipient appears in your list more than once" in warnings[0] |
| 74 | + assert "3 rows are duplicates of an earlier row" in warnings[1] |
| 75 | + |
| 76 | + |
| 77 | +def test_letter_template_never_warns_about_duplicates(app_): |
| 78 | + # Letters can legitimately share an address (e.g. roommates), so the |
| 79 | + # warning would be misleading and is therefore suppressed. |
| 80 | + recipients = MockRecipients( |
| 81 | + has_duplicate_recipients=True, |
| 82 | + count_of_unique_duplicate_recipients=2, |
| 83 | + count_of_duplicate_recipient_rows=2, |
| 84 | + template_type="letter", |
| 85 | + ) |
| 86 | + with app_.test_request_context(): |
| 87 | + assert get_warnings_for_csv(recipients, "letter") == [] |
0 commit comments