From aafa7178f96e461f936e0e8807f8ba3257e08dab Mon Sep 17 00:00:00 2001 From: Carlo Costino Date: Fri, 17 May 2024 17:14:46 -0400 Subject: [PATCH] Updated tests to match the formatting work done in the admin repo Signed-off-by: Carlo Costino --- .../antivirus/test_antivirus_client.py | 34 +- .../clients/redis/test_redis_client.py | 48 +-- .../clients/redis/test_request_cache.py | 16 +- .../clients/zendesk/test_zendesk_client.py | 19 +- tests/notifications_utils/conftest.py | 6 +- tests/notifications_utils/test_base64_uuid.py | 2 +- .../notifications_utils/test_base_template.py | 2 +- tests/notifications_utils/test_countries.py | 22 +- .../notifications_utils/test_countries_iso.py | 12 +- tests/notifications_utils/test_field.py | 10 +- .../test_field_html_handling.py | 8 +- .../test_formatted_list.py | 0 tests/notifications_utils/test_formatters.py | 36 +- .../test_insensitive_dict.py | 2 +- .../test_international_billing_rates.py | 4 +- .../test_letter_timings.py | 9 +- tests/notifications_utils/test_logging.py | 2 +- tests/notifications_utils/test_markdown.py | 337 +++++++++--------- .../notifications_utils/test_placeholders.py | 12 +- .../test_postal_address.py | 82 ++--- .../notifications_utils/test_recipient_csv.py | 55 +-- .../test_recipient_validation.py | 13 +- .../test_request_header_authentication.py | 4 +- tests/notifications_utils/test_safe_string.py | 4 +- .../notifications_utils/test_sanitise_text.py | 16 +- .../test_serialised_model.py | 8 +- .../test_template_change.py | 6 +- .../test_template_types.py | 273 +++++++------- tests/notifications_utils/test_timezones.py | 4 +- .../test_url_safe_tokens.py | 6 +- 30 files changed, 553 insertions(+), 499 deletions(-) delete mode 100644 tests/notifications_utils/test_formatted_list.py diff --git a/tests/notifications_utils/clients/antivirus/test_antivirus_client.py b/tests/notifications_utils/clients/antivirus/test_antivirus_client.py index a8f7a85bc..e19327e55 100644 --- a/tests/notifications_utils/clients/antivirus/test_antivirus_client.py +++ b/tests/notifications_utils/clients/antivirus/test_antivirus_client.py @@ -9,7 +9,7 @@ from notifications_utils.clients.antivirus.antivirus_client import ( ) -@pytest.fixture(scope="function") +@pytest.fixture() def antivirus(app, mocker): client = AntivirusClient() app.config["ANTIVIRUS_API_HOST"] = "https://antivirus" @@ -39,25 +39,33 @@ def test_scan_document(antivirus, rmock): def test_should_raise_for_status(antivirus, rmock): with pytest.raises(AntivirusError) as excinfo: - rmock.request( - "POST", - "https://antivirus/scan", - json={"error": "Antivirus error"}, - status_code=400, - ) - - antivirus.scan(io.BytesIO(b"document")) + _test_one_statement_for_status(antivirus, rmock) assert excinfo.value.message == "Antivirus error" assert excinfo.value.status_code == 400 +def _test_one_statement_for_status(antivirus, rmock): + rmock.request( + "POST", + "https://antivirus/scan", + json={"error": "Antivirus error"}, + status_code=400, + ) + + antivirus.scan(io.BytesIO(b"document")) + + def test_should_raise_for_connection_errors(antivirus, rmock): with pytest.raises(AntivirusError) as excinfo: - rmock.request( - "POST", "https://antivirus/scan", exc=requests.exceptions.ConnectTimeout - ) - antivirus.scan(io.BytesIO(b"document")) + _test_one_statement_for_connection_errors(antivirus, rmock) assert excinfo.value.message == "connection error" assert excinfo.value.status_code == 503 + + +def _test_one_statement_for_connection_errors(antivirus, rmock): + rmock.request( + "POST", "https://antivirus/scan", exc=requests.exceptions.ConnectTimeout + ) + antivirus.scan(io.BytesIO(b"document")) diff --git a/tests/notifications_utils/clients/redis/test_redis_client.py b/tests/notifications_utils/clients/redis/test_redis_client.py index 1f122206b..536cce967 100644 --- a/tests/notifications_utils/clients/redis/test_redis_client.py +++ b/tests/notifications_utils/clients/redis/test_redis_client.py @@ -8,17 +8,17 @@ from freezegun import freeze_time from notifications_utils.clients.redis.redis_client import RedisClient, prepare_value -@pytest.fixture(scope="function") +@pytest.fixture() def mocked_redis_pipeline(): return Mock() -@pytest.fixture +@pytest.fixture() def delete_mock(): return Mock(return_value=4) -@pytest.fixture(scope="function") +@pytest.fixture() def mocked_redis_client(app, mocked_redis_pipeline, delete_mock, mocker): app.config["REDIS_ENABLED"] = True @@ -46,14 +46,16 @@ def mocked_redis_client(app, mocked_redis_pipeline, delete_mock, mocker): return redis_client -@pytest.fixture +@pytest.fixture() def failing_redis_client(mocked_redis_client, delete_mock): - mocked_redis_client.redis_store.get.side_effect = Exception("get failed") - mocked_redis_client.redis_store.set.side_effect = Exception("set failed") - mocked_redis_client.redis_store.incr.side_effect = Exception("incr failed") - mocked_redis_client.redis_store.pipeline.side_effect = Exception("pipeline failed") - mocked_redis_client.redis_store.delete.side_effect = Exception("delete failed") - delete_mock.side_effect = Exception("delete by pattern failed") + # nota bene: using KeyError because flake8 thinks Exception + # and BaseException are too broad + mocked_redis_client.redis_store.get.side_effect = KeyError("get failed") + mocked_redis_client.redis_store.set.side_effect = KeyError("set failed") + mocked_redis_client.redis_store.incr.side_effect = KeyError("incr failed") + mocked_redis_client.redis_store.pipeline.side_effect = KeyError("pipeline failed") + mocked_redis_client.redis_store.delete.side_effect = KeyError("delete failed") + delete_mock.side_effect = KeyError("delete by pattern failed") return mocked_redis_client @@ -85,29 +87,29 @@ def test_should_raise_exception_if_raise_set_to_true( app, failing_redis_client, ): - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.get("test", raise_exception=True) - assert str(e.value) == "get failed" + assert str(e.value) == "'get failed'" - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.set("test", "test", raise_exception=True) - assert str(e.value) == "set failed" + assert str(e.value) == "'set failed'" - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.incr("test", raise_exception=True) - assert str(e.value) == "incr failed" + assert str(e.value) == "'incr failed'" - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.exceeded_rate_limit("test", 100, 200, raise_exception=True) - assert str(e.value) == "pipeline failed" + assert str(e.value) == "'pipeline failed'" - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.delete("test", raise_exception=True) - assert str(e.value) == "delete failed" + assert str(e.value) == "'delete failed'" - with pytest.raises(Exception) as e: + with pytest.raises(KeyError) as e: failing_redis_client.delete_by_pattern("pattern", raise_exception=True) - assert str(e.value) == "delete by pattern failed" + assert str(e.value) == "'delete by pattern failed'" def test_should_not_call_if_not_enabled(mocked_redis_client, delete_mock): @@ -198,7 +200,7 @@ def test_delete_multi(mocked_redis_client): @pytest.mark.parametrize( - "input,output", + ("input", "output"), [ (b"asdf", b"asdf"), ("asdf", "asdf"), diff --git a/tests/notifications_utils/clients/redis/test_request_cache.py b/tests/notifications_utils/clients/redis/test_request_cache.py index 876c4a976..472d9398b 100644 --- a/tests/notifications_utils/clients/redis/test_request_cache.py +++ b/tests/notifications_utils/clients/redis/test_request_cache.py @@ -4,7 +4,7 @@ from notifications_utils.clients.redis import RequestCache from notifications_utils.clients.redis.redis_client import RedisClient -@pytest.fixture(scope="function") +@pytest.fixture() def mocked_redis_client(app): app.config["REDIS_ENABLED"] = True redis_client = RedisClient() @@ -12,19 +12,19 @@ def mocked_redis_client(app): return redis_client -@pytest.fixture +@pytest.fixture() def cache(mocked_redis_client): return RequestCache(mocked_redis_client) @pytest.mark.parametrize( - "args, kwargs, expected_cache_key", - ( + ("args", "kwargs", "expected_cache_key"), + [ ([1, 2, 3], {}, "1-2-3-None-None-None"), ([1, 2, 3, 4, 5, 6], {}, "1-2-3-4-5-6"), ([1, 2, 3], {"x": 4, "y": 5, "z": 6}, "1-2-3-4-5-6"), ([1, 2, 3, 4], {"y": 5}, "1-2-3-4-5-None"), - ), + ], ) def test_set( mocker, @@ -60,13 +60,13 @@ def test_set( @pytest.mark.parametrize( - "cache_set_call, expected_redis_client_ttl", - ( + ("cache_set_call", "expected_redis_client_ttl"), + [ (0, 0), (1, 1), (1.111, 1), ("2000", 2_000), - ), + ], ) def test_set_with_custom_ttl( mocker, diff --git a/tests/notifications_utils/clients/zendesk/test_zendesk_client.py b/tests/notifications_utils/clients/zendesk/test_zendesk_client.py index be19c9a89..d89bf466f 100644 --- a/tests/notifications_utils/clients/zendesk/test_zendesk_client.py +++ b/tests/notifications_utils/clients/zendesk/test_zendesk_client.py @@ -9,7 +9,7 @@ from notifications_utils.clients.zendesk.zendesk_client import ( ) -@pytest.fixture(scope="function") +@pytest.fixture() def zendesk_client(app): client = ZendeskClient() @@ -67,8 +67,8 @@ def test_zendesk_client_send_ticket_to_zendesk_error( @pytest.mark.parametrize( - "p1_arg, expected_tags, expected_priority", - ( + ("p1_arg", "expected_tags", "expected_priority"), + [ ( {}, ["govuk_notify_support"], @@ -88,7 +88,7 @@ def test_zendesk_client_send_ticket_to_zendesk_error( ["govuk_notify_emergency"], "urgent", ), - ), + ], ) def test_notify_support_ticket_request_data(p1_arg, expected_tags, expected_priority): notify_ticket_form = NotifySupportTicket("subject", "message", "question", **p1_arg) @@ -126,7 +126,7 @@ def test_notify_support_ticket_request_data_with_message_hidden_from_requester() @pytest.mark.parametrize( - "name, zendesk_name", [("Name", "Name"), (None, "(no name supplied)")] + ("name", "zendesk_name"), [("Name", "Name"), (None, "(no name supplied)")] ) def test_notify_support_ticket_request_data_with_user_name_and_email( name, zendesk_name @@ -145,7 +145,14 @@ def test_notify_support_ticket_request_data_with_user_name_and_email( @pytest.mark.parametrize( - "custom_fields, tech_ticket_tag, categories, org_id, org_type, service_id", + ( + "custom_fields", + "tech_ticket_tag", + "categories", + "org_id", + "org_type", + "service_id", + ), [ ( {"technical_ticket": True}, diff --git a/tests/notifications_utils/conftest.py b/tests/notifications_utils/conftest.py index 7150b1486..0b8f3bce8 100644 --- a/tests/notifications_utils/conftest.py +++ b/tests/notifications_utils/conftest.py @@ -9,7 +9,7 @@ class FakeService: id = "1234" -@pytest.fixture +@pytest.fixture() def app(): flask_app = Flask(__name__) ctx = flask_app.app_context() @@ -20,7 +20,7 @@ def app(): ctx.pop() -@pytest.fixture +@pytest.fixture() def celery_app(mocker): app = Flask(__name__) app.config["CELERY"] = {"broker_url": "foo"} @@ -39,7 +39,7 @@ def sample_service(): return FakeService() -@pytest.fixture +@pytest.fixture() def rmock(): with requests_mock.mock() as rmock: yield rmock diff --git a/tests/notifications_utils/test_base64_uuid.py b/tests/notifications_utils/test_base64_uuid.py index e9d35b24d..d8293b4b4 100644 --- a/tests/notifications_utils/test_base64_uuid.py +++ b/tests/notifications_utils/test_base64_uuid.py @@ -38,7 +38,7 @@ def test_base64_converter_to_url(python_val): @pytest.mark.parametrize( - "url_val,expectation", + ("url_val", "expectation"), [ ( "this_is_valid_base64_but_is_too_long_to_be_a_uuid", diff --git a/tests/notifications_utils/test_base_template.py b/tests/notifications_utils/test_base_template.py index c9d126d12..ac46e219e 100644 --- a/tests/notifications_utils/test_base_template.py +++ b/tests/notifications_utils/test_base_template.py @@ -80,7 +80,7 @@ def test_matches_keys_to_placeholder_names(): @pytest.mark.parametrize( - "template_content, template_subject, expected", + ("template_content", "template_subject", "expected"), [ ("the quick brown fox", "jumps", []), ("the quick ((colour)) fox", "jumps", ["colour"]), diff --git a/tests/notifications_utils/test_countries.py b/tests/notifications_utils/test_countries.py index 473b2731b..07f48d0e4 100644 --- a/tests/notifications_utils/test_countries.py +++ b/tests/notifications_utils/test_countries.py @@ -34,7 +34,7 @@ def test_constants(): assert Postage.UK == "united-kingdom" -@pytest.mark.parametrize("synonym, canonical", ADDITIONAL_SYNONYMS) +@pytest.mark.parametrize(("synonym", "canonical"), ADDITIONAL_SYNONYMS) def test_hand_crafted_synonyms_map_to_canonical_countries(synonym, canonical): exceptions_to_canonical_countries = [ "Easter Island", @@ -55,7 +55,7 @@ def test_hand_crafted_synonyms_map_to_canonical_countries(synonym, canonical): assert Country(synonym).canonical_name == canonical -@pytest.mark.parametrize("welsh_name, canonical", WELSH_NAMES) +@pytest.mark.parametrize(("welsh_name", "canonical"), WELSH_NAMES) def test_welsh_names_map_to_canonical_countries(welsh_name, canonical): assert Country(canonical).canonical_name == canonical assert Country(welsh_name).canonical_name == canonical @@ -74,8 +74,8 @@ def test_crowdsourced_test_data(): @pytest.mark.parametrize( - "search, expected", - ( + ("search", "expected"), + [ ("u.s.a", "United States"), ("america", "United States"), ("United States America", "United States"), @@ -120,7 +120,7 @@ def test_crowdsourced_test_data(): ("Illes Balears", "Balearic Islands"), ("Corsica", "Corsica"), ("Corse", "Corsica"), - ), + ], ) def test_hand_crafted_synonyms(search, expected): assert Country(search).canonical_name == expected @@ -135,11 +135,11 @@ def test_auto_checking_for_country_starting_with_the(): @pytest.mark.parametrize( - "search, expected_error_message", - ( + ("search", "expected_error_message"), + [ ("Qumran", "Not a known country or territory (Qumran)"), ("Kumrahn", "Not a known country or territory (Kumrahn)"), - ), + ], ) def test_non_existant_countries(search, expected_error_message): with pytest.raises(KeyError) as error: @@ -149,8 +149,8 @@ def test_non_existant_countries(search, expected_error_message): @pytest.mark.parametrize( - "search, expected", - ( + ("search", "expected"), + [ ("u.s.a", "rest-of-world"), ("Rep of Ireland", "europe"), ("deutschland", "europe"), @@ -159,7 +159,7 @@ def test_non_existant_countries(search, expected_error_message): ("Guernsey", "united-kingdom"), ("isle-of-man", "united-kingdom"), ("ESPAÑA", "europe"), - ), + ], ) def test_get_postage(search, expected): assert Country(search).postage_zone == expected diff --git a/tests/notifications_utils/test_countries_iso.py b/tests/notifications_utils/test_countries_iso.py index e1d3a68d8..ada46e930 100644 --- a/tests/notifications_utils/test_countries_iso.py +++ b/tests/notifications_utils/test_countries_iso.py @@ -11,8 +11,8 @@ def _country_not_found(*test_case): @pytest.mark.parametrize( - "alpha_2, expected_name", - ( + ("alpha_2", "expected_name"), + [ ("AF", "Afghanistan"), ("AL", "Albania"), ("DZ", "Algeria"), @@ -262,15 +262,15 @@ def _country_not_found(*test_case): ("ZM", "Zambia"), ("ZW", "Zimbabwe"), ("AX", "Åland Islands"), - ), + ], ) def test_iso_alpha_2_country_codes(alpha_2, expected_name): assert Country(alpha_2).canonical_name == expected_name @pytest.mark.parametrize( - "alpha_3, expected_name", - ( + ("alpha_3", "expected_name"), + [ _country_not_found("AFG", "Afghanistan"), _country_not_found("ALB", "Albania"), _country_not_found("DZA", "Algeria"), @@ -520,7 +520,7 @@ def test_iso_alpha_2_country_codes(alpha_2, expected_name): _country_not_found("ZMB", "Zambia"), _country_not_found("ZWE", "Zimbabwe"), _country_not_found("ALA", "Åland Islands"), - ), + ], ) def test_iso_alpha_3_country_codes(alpha_3, expected_name): assert Country(alpha_3).canonical_name == expected_name diff --git a/tests/notifications_utils/test_field.py b/tests/notifications_utils/test_field.py index 686556b68..fcd50cf3a 100644 --- a/tests/notifications_utils/test_field.py +++ b/tests/notifications_utils/test_field.py @@ -23,7 +23,7 @@ def test_returns_a_string_without_placeholders(content): @pytest.mark.parametrize( - "template_content,data,expected", + ("template_content", "data", "expected"), [ ("((colour))", {"colour": "red"}, "red"), ("the quick ((colour)) fox", {"colour": "brown"}, "the quick brown fox"), @@ -111,7 +111,7 @@ def test_replacement_of_placeholders(template_content, data, expected): @pytest.mark.parametrize( - "template_content,data,expected", + ("template_content", "data", "expected"), [ ( "((code)) is your security code", @@ -142,7 +142,7 @@ def test_optional_redacting_of_missing_values(template_content, data, expected): @pytest.mark.parametrize( - "content,expected", + ("content", "expected"), [ ("((colour))", "((colour))"), ( @@ -189,7 +189,7 @@ def test_formatting_of_placeholders(content, expected): @pytest.mark.parametrize( - "content, values, expected", + ("content", "values", "expected"), [ ( "((name)) ((colour))", @@ -244,7 +244,7 @@ def test_what_will_trigger_conditional_placeholder(value): @pytest.mark.parametrize( - "values, expected, expected_as_markdown", + ("values", "expected", "expected_as_markdown"), [ ( {"placeholder": []}, diff --git a/tests/notifications_utils/test_field_html_handling.py b/tests/notifications_utils/test_field_html_handling.py index d113f816c..16f83826c 100644 --- a/tests/notifications_utils/test_field_html_handling.py +++ b/tests/notifications_utils/test_field_html_handling.py @@ -4,7 +4,13 @@ from notifications_utils.field import Field @pytest.mark.parametrize( - "content, values, expected_stripped, expected_escaped, expected_passthrough", + ( + "content", + "values", + "expected_stripped", + "expected_escaped", + "expected_passthrough", + ), [ ( "string with html", diff --git a/tests/notifications_utils/test_formatted_list.py b/tests/notifications_utils/test_formatted_list.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/tests/notifications_utils/test_formatters.py b/tests/notifications_utils/test_formatters.py index c1acac5dc..61185d974 100644 --- a/tests/notifications_utils/test_formatters.py +++ b/tests/notifications_utils/test_formatters.py @@ -25,7 +25,7 @@ from notifications_utils.template import ( @pytest.mark.parametrize( - "url, expected_html", + ("url", "expected_html"), [ ( """https://example.com/"onclick="alert('hi')""", @@ -78,7 +78,7 @@ def test_escaping_govuk_in_email_templates(): @pytest.mark.parametrize( - "template_content,expected", + ("template_content", "expected"), [ # Cases that we add the breaking space ("GOV.UK", "GOV.\u200BUK"), @@ -136,7 +136,7 @@ def test_unlink_govuk_escaped(template_content, expected): @pytest.mark.parametrize( - "prefix, body, expected", + ("prefix", "body", "expected"), [ ("a", "b", "a: b"), (None, "b", "b"), @@ -173,7 +173,7 @@ def test_sms_encode(mocker): @pytest.mark.parametrize( - "items, kwargs, expected_output", + ("items", "kwargs", "expected_output"), [ ([1], {}, "‘1’"), ([1, 2], {}, "‘1’ and ‘2’"), @@ -205,8 +205,8 @@ def test_bleach_doesnt_try_to_make_valid_html_before_cleaning(): @pytest.mark.parametrize( - "content, expected_escaped", - ( + ("content", "expected_escaped"), + [ ("&?a;", "&?a;"), ("&>a;", "&>a;"), ("&*a;", "&*a;"), @@ -234,7 +234,7 @@ def test_bleach_doesnt_try_to_make_valid_html_before_cleaning(): # We let users use ( and ) because otherwise it’s # impossible to put brackets in the body of conditional placeholders ("((var??(in brackets)))", "((var??(in brackets)))"), - ), + ], ) def test_escaping_html_entities( content, @@ -244,7 +244,7 @@ def test_escaping_html_entities( @pytest.mark.parametrize( - "dirty, clean", + ("dirty", "clean"), [ ( "Hello ((name)) ,\n\nThis is a message", @@ -262,7 +262,7 @@ def test_removing_whitespace_before_commas(dirty, clean): @pytest.mark.parametrize( - "dirty, clean", + ("dirty", "clean"), [ ( "Hello ((name)) .\n\nThis is a message", @@ -280,7 +280,7 @@ def test_removing_whitespace_before_full_stops(dirty, clean): @pytest.mark.parametrize( - "dumb, smart", + ("dumb", "smart"), [ ( """And I said, "what about breakfast at Tiffany's"?""", @@ -301,7 +301,7 @@ def test_smart_quotes(dumb, smart): @pytest.mark.parametrize( - "nasty, nice", + ("nasty", "nice"), [ ( ( @@ -436,8 +436,8 @@ def test_normalise_whitespace(value): @pytest.mark.parametrize( - "content, expected_html", - ( + ("content", "expected_html"), + [ ( "http://example.com", 'http://example.com', @@ -545,15 +545,15 @@ def test_normalise_whitespace(value): "with-subdomain@test.example.com", "with-subdomain@test.example.com", ), - ), + ], ) def test_autolink_urls_matches_correctly(content, expected_html): assert autolink_urls(content) == expected_html @pytest.mark.parametrize( - "extra_kwargs, expected_html", - ( + ("extra_kwargs", "expected_html"), + [ ( {}, 'http://example.com', @@ -564,14 +564,14 @@ def test_autolink_urls_matches_correctly(content, expected_html): }, 'http://example.com', ), - ), + ], ) def test_autolink_urls_applies_correct_attributes(extra_kwargs, expected_html): assert autolink_urls("http://example.com", **extra_kwargs) == expected_html @pytest.mark.parametrize( - "content", ("without link", "with link to https://example.com") + "content", ["without link", "with link to https://example.com"] ) def test_autolink_urls_returns_markup(content): assert isinstance(autolink_urls(content), Markup) diff --git a/tests/notifications_utils/test_insensitive_dict.py b/tests/notifications_utils/test_insensitive_dict.py index ae7f62f9c..e019b55ca 100644 --- a/tests/notifications_utils/test_insensitive_dict.py +++ b/tests/notifications_utils/test_insensitive_dict.py @@ -50,7 +50,7 @@ def test_missing_data(): ], ) @pytest.mark.parametrize( - "key, should_be_present", + ("key", "should_be_present"), [ ("foo", True), ("f_o_o", True), diff --git a/tests/notifications_utils/test_international_billing_rates.py b/tests/notifications_utils/test_international_billing_rates.py index 5e68d767b..d7bd1a727 100644 --- a/tests/notifications_utils/test_international_billing_rates.py +++ b/tests/notifications_utils/test_international_billing_rates.py @@ -12,7 +12,7 @@ def test_international_billing_rates_exists(): @pytest.mark.parametrize( - "country_prefix, values", sorted(INTERNATIONAL_BILLING_RATES.items()) + ("country_prefix", "values"), sorted(INTERNATIONAL_BILLING_RATES.items()) ) def test_international_billing_rates_are_in_correct_format(country_prefix, values): assert isinstance(country_prefix, str) @@ -38,7 +38,7 @@ def test_country_codes(): @pytest.mark.parametrize( - "number, expected", + ("number", "expected"), [ ("+48123654789", False), # Poland alpha: Yes ("+1-403-123-5687", True), # Canada alpha: No diff --git a/tests/notifications_utils/test_letter_timings.py b/tests/notifications_utils/test_letter_timings.py index 2a90fc641..aecc9c744 100644 --- a/tests/notifications_utils/test_letter_timings.py +++ b/tests/notifications_utils/test_letter_timings.py @@ -12,7 +12,14 @@ from notifications_utils.letter_timings import ( @freeze_time("2017-07-14 13:59:59") # Friday, before print deadline (3PM EST) @pytest.mark.parametrize( - "upload_time, expected_print_time, is_printed, first_class, expected_earliest, expected_latest", + ( + "upload_time", + "expected_print_time", + "is_printed", + "first_class", + "expected_earliest", + "expected_latest", + ), [ # EST # ================================================================== diff --git a/tests/notifications_utils/test_logging.py b/tests/notifications_utils/test_logging.py index 1aefb4065..cc7f5ee34 100644 --- a/tests/notifications_utils/test_logging.py +++ b/tests/notifications_utils/test_logging.py @@ -48,4 +48,4 @@ def test_base_json_formatter_contains_service_id(): json.loads(logging.BaseJSONFormatter().format(record))["message"] == "message to log" ) - assert service_id_filter.filter(record).service_id == "notify-admin" + assert service_id_filter.filter(record).service_id == "no-service-id" diff --git a/tests/notifications_utils/test_markdown.py b/tests/notifications_utils/test_markdown.py index 550f56b58..be1053725 100644 --- a/tests/notifications_utils/test_markdown.py +++ b/tests/notifications_utils/test_markdown.py @@ -33,7 +33,7 @@ def test_makes_links_out_of_URLs(url): @pytest.mark.parametrize( - "input, output", + ("input", "output"), [ ( ("this is some text with a link http://example.com in the middle"), @@ -93,7 +93,7 @@ def test_handles_placeholders_in_urls(): @pytest.mark.parametrize( - "url, expected_html, expected_html_in_template", + ("url", "expected_html", "expected_html_in_template"), [ ( """https://example.com"onclick="alert('hi')""", @@ -125,7 +125,7 @@ def test_URLs_get_escaped(url, expected_html, expected_html_in_template): @pytest.mark.parametrize( - "markdown_function, expected_output", + ("markdown_function", "expected_output"), [ ( notify_email_markdown, @@ -154,22 +154,22 @@ def test_preserves_whitespace_when_making_links(markdown_function, expected_outp @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, 'print("hello")'], - [notify_email_markdown, 'print("hello")'], - [notify_plain_text_email_markdown, 'print("hello")'], - ), + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, 'print("hello")'), + (notify_email_markdown, 'print("hello")'), + (notify_plain_text_email_markdown, 'print("hello")'), + ], ) def test_block_code(markdown_function, expected): assert markdown_function('```\nprint("hello")\n```') == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, ("

inset text

")], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, ("

inset text

")), + ( notify_email_markdown, ( "
inset text

' "
" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\ninset text"), - ], - ), + ), + ], ) def test_block_quote(markdown_function, expected): assert markdown_function("^ inset text") == expected @@ -192,16 +192,16 @@ def test_block_quote(markdown_function, expected): @pytest.mark.parametrize( "heading", - ( + [ "# heading", "#heading", - ), + ], ) @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, "

heading

\n"], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, "

heading

\n"), + ( notify_email_markdown, ( '

" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ( "\n" @@ -218,47 +218,47 @@ def test_block_quote(markdown_function, expected): "\nheading" "\n-----------------------------------------------------------------" ), - ], - ), + ), + ], ) def test_level_1_header(markdown_function, heading, expected): assert markdown_function(heading) == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, "

inset text

"], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, "

inset text

"), + ( notify_email_markdown, '

inset text

', - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\ninset text"), - ], - ), + ), + ], ) def test_level_2_header(markdown_function, expected): assert markdown_function("## inset text") == (expected) @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("

a

" '
 
' "

b

"), - ], - [ + ), + ( notify_email_markdown, ( '

a

' '
' '

b

' ), - ], - [ + ), + ( notify_plain_text_email_markdown, ( "\n" @@ -268,8 +268,8 @@ def test_level_2_header(markdown_function, expected): "\n" "\nb" ), - ], - ), + ), + ], ) def test_hrule(markdown_function, expected): assert markdown_function("a\n\n***\n\nb") == expected @@ -277,13 +277,13 @@ def test_hrule(markdown_function, expected): @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("
    \n" "
  1. one
  2. \n" "
  3. two
  4. \n" "
  5. three
  6. \n" "
\n"), - ], - [ + ), + ( notify_email_markdown, ( '' @@ -301,12 +301,12 @@ def test_hrule(markdown_function, expected): "" "
" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\n1. one" "\n2. two" "\n3. three"), - ], - ), + ), + ], ) def test_ordered_list(markdown_function, expected): assert markdown_function("1. one\n" "2. two\n" "3. three\n") == expected @@ -315,27 +315,26 @@ def test_ordered_list(markdown_function, expected): @pytest.mark.parametrize( "markdown", - ( + [ ("*one\n" "*two\n" "*three\n"), # no space ("* one\n" "* two\n" "* three\n"), # single space ("* one\n" "* two\n" "* three\n"), # two spaces - ("* one\n" "* two\n" "* three\n"), # tab ("- one\n" "- two\n" "- three\n"), # dash as bullet pytest.param( ("+ one\n" "+ two\n" "+ three\n"), # plus as bullet marks=pytest.mark.xfail(raises=AssertionError), ), ("• one\n" "• two\n" "• three\n"), # bullet as bullet - ), + ], ) @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("
    \n" "
  • one
  • \n" "
  • two
  • \n" "
  • three
  • \n" "
\n"), - ], - [ + ), + ( notify_email_markdown, ( '' @@ -353,83 +352,83 @@ def test_ordered_list(markdown_function, expected): "" "
" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\n• one" "\n• two" "\n• three"), - ], - ), + ), + ], ) def test_unordered_list(markdown, markdown_function, expected): assert markdown_function(markdown) == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, "

+ one

+ two

+ three

", - ], - [ + ), + ( notify_email_markdown, ( '

+ one

' '

+ two

' '

+ three

' ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n\n+ one" "\n\n+ two" "\n\n+ three"), - ], - ), + ), + ], ) def test_pluses_dont_render_as_lists(markdown_function, expected): assert markdown_function("+ one\n" "+ two\n" "+ three\n") == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("

" "line one
" "line two" "

" "

" "new paragraph" "

"), - ], - [ + ), + ( notify_email_markdown, ( '

line one
' "line two

" '

new paragraph

' ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\nline one" "\nline two" "\n" "\nnew paragraph"), - ], - ), + ), + ], ) def test_paragraphs(markdown_function, expected): assert markdown_function("line one\n" "line two\n" "\n" "new paragraph") == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, ("

before

" "

after

")], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, ("

before

" "

after

")), + ( notify_email_markdown, ( '

before

' '

after

' ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\nbefore" "\n" "\nafter"), - ], - ), + ), + ], ) def test_multiple_newlines_get_truncated(markdown_function, expected): assert markdown_function("before\n\n\n\n\n\nafter") == expected @@ -437,25 +436,25 @@ def test_multiple_newlines_get_truncated(markdown_function, expected): @pytest.mark.parametrize( "markdown_function", - ( + [ notify_letter_preview_markdown, notify_email_markdown, notify_plain_text_email_markdown, - ), + ], ) def test_table(markdown_function): assert markdown_function("col | col\n" "----|----\n" "val | val\n") == ("") @pytest.mark.parametrize( - "markdown_function, link, expected", - ( - [ + ("markdown_function", "link", "expected"), + [ + ( notify_letter_preview_markdown, "http://example.com", "

example.com

", - ], - [ + ), + ( notify_email_markdown, "http://example.com", ( @@ -463,8 +462,8 @@ def test_table(markdown_function): 'http://example.com' "

" ), - ], - [ + ), + ( notify_email_markdown, """https://example.com"onclick="alert('hi')""", ( @@ -475,105 +474,105 @@ def test_table(markdown_function): "')" "

" ), - ], - [ + ), + ( notify_plain_text_email_markdown, "http://example.com", ("\n" "\nhttp://example.com"), - ], - ), + ), + ], ) def test_autolink(markdown_function, link, expected): assert markdown_function(link) == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, "

variable called `thing`

"], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, "

variable called `thing`

"), + ( notify_email_markdown, '

variable called `thing`

', # noqa E501 - ], - [ + ), + ( notify_plain_text_email_markdown, "\n\nvariable called `thing`", - ], - ), + ), + ], ) def test_codespan(markdown_function, expected): assert markdown_function("variable called `thing`") == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, "

something **important**

"], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, "

something **important**

"), + ( notify_email_markdown, '

something **important**

', # noqa E501 - ], - [ + ), + ( notify_plain_text_email_markdown, "\n\nsomething **important**", - ], - ), + ), + ], ) def test_double_emphasis(markdown_function, expected): assert markdown_function("something **important**") == expected @pytest.mark.parametrize( - "markdown_function, text, expected", - ( - [ + ("markdown_function", "text", "expected"), + [ + ( notify_letter_preview_markdown, "something *important*", "

something *important*

", - ], - [ + ), + ( notify_email_markdown, "something *important*", '

something *important*

', # noqa E501 - ], - [ + ), + ( notify_plain_text_email_markdown, "something *important*", "\n\nsomething *important*", - ], - [ + ), + ( notify_plain_text_email_markdown, "something _important_", "\n\nsomething _important_", - ], - [ + ), + ( notify_plain_text_email_markdown, "before*after", "\n\nbefore*after", - ], - [ + ), + ( notify_plain_text_email_markdown, "before_after", "\n\nbefore_after", - ], - ), + ), + ], ) def test_emphasis(markdown_function, text, expected): assert markdown_function(text) == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_email_markdown, '

foo ****** bar

', - ], - [ + ), + ( notify_plain_text_email_markdown, "\n\nfoo ****** bar", - ], - ), + ), + ], ) def test_nested_emphasis(markdown_function, expected): assert markdown_function("foo ****** bar") == expected @@ -581,24 +580,24 @@ def test_nested_emphasis(markdown_function, expected): @pytest.mark.parametrize( "markdown_function", - ( + [ notify_letter_preview_markdown, notify_email_markdown, notify_plain_text_email_markdown, - ), + ], ) def test_image(markdown_function): assert markdown_function("![alt text](http://example.com/image.png)") == ("") @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("

Example: example.com

"), - ], - [ + ), + ( notify_email_markdown, ( '

Example' "

" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\nExample: http://example.com"), - ], - ), + ), + ], ) def test_link(markdown_function, expected): assert markdown_function("[Example](http://example.com)") == expected @pytest.mark.parametrize( - "markdown_function, expected", - ( - [ + ("markdown_function", "expected"), + [ + ( notify_letter_preview_markdown, ("

Example: example.com

"), - ], - [ + ), + ( notify_email_markdown, ( '

" "

" ), - ], - [ + ), + ( notify_plain_text_email_markdown, ("\n" "\nExample (An example URL): http://example.com"), - ], - ), + ), + ], ) def test_link_with_title(markdown_function, expected): assert ( @@ -648,15 +647,15 @@ def test_link_with_title(markdown_function, expected): @pytest.mark.parametrize( - "markdown_function, expected", - ( - [notify_letter_preview_markdown, "

~~Strike~~

"], - [ + ("markdown_function", "expected"), + [ + (notify_letter_preview_markdown, "

~~Strike~~

"), + ( notify_email_markdown, '

~~Strike~~

', - ], - [notify_plain_text_email_markdown, "\n\n~~Strike~~"], - ), + ), + (notify_plain_text_email_markdown, "\n\n~~Strike~~"), + ], ) def test_strikethrough(markdown_function, expected): assert markdown_function("~~Strike~~") == expected diff --git a/tests/notifications_utils/test_placeholders.py b/tests/notifications_utils/test_placeholders.py index 7ac11c3c9..348d2159a 100644 --- a/tests/notifications_utils/test_placeholders.py +++ b/tests/notifications_utils/test_placeholders.py @@ -6,7 +6,7 @@ from notifications_utils.field import Placeholder @pytest.mark.parametrize( - "body, expected", + ("body", "expected"), [ ("((with-brackets))", "with-brackets"), ("without-brackets", "without-brackets"), @@ -17,7 +17,7 @@ def test_placeholder_returns_name(body, expected): @pytest.mark.parametrize( - "body, is_conditional", + ("body", "is_conditional"), [ ("not a conditional", False), ("not? a conditional", False), @@ -29,7 +29,7 @@ def test_placeholder_identifies_conditional(body, is_conditional): @pytest.mark.parametrize( - "body, conditional_text", + ("body", "conditional_text"), [ ("a??b", "b"), ("a?? b ", " b "), @@ -41,12 +41,12 @@ def test_placeholder_gets_conditional_text(body, conditional_text): def test_placeholder_raises_if_accessing_conditional_text_on_non_conditional(): - with pytest.raises(ValueError): + with pytest.raises(ValueError): # noqa, flake8 says ValueError not specific enough Placeholder("hello").conditional_text @pytest.mark.parametrize( - "body, value, result", + ("body", "value", "result"), [ ("a??b", "Yes", "b"), ("a??b", "No", ""), @@ -57,7 +57,7 @@ def test_placeholder_gets_conditional_body(body, value, result): def test_placeholder_raises_if_getting_conditional_body_on_non_conditional(): - with pytest.raises(ValueError): + with pytest.raises(ValueError): # noqa, flake8 says ValueError not specific enough Placeholder("hello").get_conditional_body("Yes") diff --git a/tests/notifications_utils/test_postal_address.py b/tests/notifications_utils/test_postal_address.py index f854944cd..3e29f193f 100644 --- a/tests/notifications_utils/test_postal_address.py +++ b/tests/notifications_utils/test_postal_address.py @@ -17,8 +17,8 @@ def test_raw_address(): @pytest.mark.parametrize( - "address, expected_country", - ( + ("address", "expected_country"), + [ ( """ 123 Example Street @@ -51,15 +51,15 @@ def test_raw_address(): """, Country("Germany"), ), - ), + ], ) def test_country(address, expected_country): assert PostalAddress(address).country == expected_country @pytest.mark.parametrize( - "address, enough_lines_expected", - ( + ("address", "enough_lines_expected"), + [ ( "", False, @@ -102,15 +102,15 @@ def test_country(address, expected_country): """, True, ), - ), + ], ) def test_has_enough_lines(address, enough_lines_expected): assert PostalAddress(address).has_enough_lines is enough_lines_expected @pytest.mark.parametrize( - "address, too_many_lines_expected", - ( + ("address", "too_many_lines_expected"), + [ ( "", False, @@ -171,15 +171,15 @@ def test_has_enough_lines(address, enough_lines_expected): """, True, ), - ), + ], ) def test_has_too_many_lines(address, too_many_lines_expected): assert PostalAddress(address).has_too_many_lines is too_many_lines_expected @pytest.mark.parametrize( - "address, expected_postcode", - ( + ("address", "expected_postcode"), + [ ( "", None, @@ -215,7 +215,7 @@ def test_has_too_many_lines(address, too_many_lines_expected): """, None, ), - ), + ], ) def test_postcode(address, expected_postcode): assert PostalAddress(address).has_valid_postcode is bool(expected_postcode) @@ -223,7 +223,7 @@ def test_postcode(address, expected_postcode): @pytest.mark.parametrize( - "address, expected_result", + ("address", "expected_result"), [ ( "", @@ -276,8 +276,8 @@ def test_has_invalid_characters(address, expected_result): @pytest.mark.parametrize( - "address, expected_international", - ( + ("address", "expected_international"), + [ ( "", False, @@ -313,15 +313,15 @@ def test_has_invalid_characters(address, expected_result): """, True, ), - ), + ], ) def test_international(address, expected_international): assert PostalAddress(address).international is expected_international @pytest.mark.parametrize( - "address, expected_normalised, expected_as_single_line", - ( + ("address", "expected_normalised", "expected_as_single_line"), + [ ( "", "", @@ -357,7 +357,7 @@ def test_international(address, expected_international): ("123 Example Straße\n" "Germany"), ("123 Example Straße, Germany"), ), - ), + ], ) def test_normalised(address, expected_normalised, expected_as_single_line): assert PostalAddress(address).normalised == expected_normalised @@ -365,8 +365,8 @@ def test_normalised(address, expected_normalised, expected_as_single_line): @pytest.mark.parametrize( - "address, expected_postage", - ( + ("address", "expected_postage"), + [ ( "", Postage.UK, @@ -401,7 +401,7 @@ def test_normalised(address, expected_normalised, expected_as_single_line): """, Postage.REST_OF_WORLD, ), - ), + ], ) def test_postage(address, expected_postage): assert PostalAddress(address).postage == expected_postage @@ -409,7 +409,7 @@ def test_postage(address, expected_postage): @pytest.mark.parametrize( "personalisation", - ( + [ { "address_line_1": "123 Example Street", "address_line_3": "City of Town", @@ -440,7 +440,7 @@ def test_postage(address, expected_postage): "Address-Line-7": "Sw1a 1aa", } ), - ), + ], ) def test_from_personalisation(personalisation): assert PostalAddress.from_personalisation(personalisation).normalised == ( @@ -461,8 +461,8 @@ def test_from_personalisation_handles_int(): @pytest.mark.parametrize( - "address, expected_personalisation", - ( + ("address", "expected_personalisation"), + [ ( "", { @@ -515,27 +515,27 @@ def test_from_personalisation_handles_int(): "postcode": "Eight", }, ), - ), + ], ) def test_as_personalisation(address, expected_personalisation): assert PostalAddress(address).as_personalisation == expected_personalisation @pytest.mark.parametrize( - "address, expected_bool", - ( + ("address", "expected_bool"), + [ ("", False), (" ", False), ("\n\n \n", False), ("a", True), - ), + ], ) def test_bool(address, expected_bool): assert bool(PostalAddress(address)) is expected_bool @pytest.mark.parametrize( - "postcode, normalised_postcode", + ("postcode", "normalised_postcode"), [ ("SW1 3EF", "SW13EF"), ("SW13EF", "SW13EF"), @@ -550,7 +550,7 @@ def test_normalise_postcode(postcode, normalised_postcode): @pytest.mark.parametrize( - "postcode, result", + ("postcode", "result"), [ # real standard UK poscodes ("SW1 3EF", True), @@ -592,7 +592,7 @@ def test_if_postcode_is_a_real_uk_postcode_normalises_before_checking_postcode(m @pytest.mark.parametrize( - "postcode, postcode_with_space", + ("postcode", "postcode_with_space"), [ ("SW13EF", "SW1 3EF"), ("SW1 3EF", "SW1 3EF"), @@ -615,8 +615,8 @@ def test_format_postcode_for_printing(postcode, postcode_with_space): @pytest.mark.parametrize( - "address, international, expected_valid", - ( + ("address", "international", "expected_valid"), + [ ( """ UK address @@ -703,7 +703,7 @@ def test_format_postcode_for_printing(postcode, postcode_with_space): False, False, ), - ), + ], ) def test_valid_with_international_parameter(address, international, expected_valid): postal_address = PostalAddress( @@ -716,7 +716,7 @@ def test_valid_with_international_parameter(address, international, expected_val @pytest.mark.parametrize( "address", - ( + [ """ Too short, valid postcode SW1A 1AA @@ -745,7 +745,7 @@ def test_valid_with_international_parameter(address, international, expected_val 7 Bhutan """, - ), + ], ) def test_valid_last_line_too_short_too_long(address): postal_address = PostalAddress(address, allow_international_letters=True) @@ -759,11 +759,11 @@ def test_valid_with_invalid_characters(): @pytest.mark.parametrize( - "international, expected_valid", - ( + ("international", "expected_valid"), + [ (False, False), (True, True), - ), + ], ) def test_valid_from_personalisation_with_international_parameter( international, expected_valid diff --git a/tests/notifications_utils/test_recipient_csv.py b/tests/notifications_utils/test_recipient_csv.py index e11bb14ad..689781f6a 100644 --- a/tests/notifications_utils/test_recipient_csv.py +++ b/tests/notifications_utils/test_recipient_csv.py @@ -43,8 +43,8 @@ def _index_rows(rows): @pytest.mark.parametrize( - "template_type, expected", - ( + ("template_type", "expected"), + [ ("email", ["email address"]), ("sms", ["phone number"]), ( @@ -60,7 +60,7 @@ def _index_rows(rows): "address line 7", ], ), - ), + ], ) def test_recipient_column_headers(template_type, expected): recipients = RecipientCSV("", template=_sample_template(template_type)) @@ -72,7 +72,7 @@ def test_recipient_column_headers(template_type, expected): @pytest.mark.parametrize( - "file_contents,template_type,expected", + ("file_contents", "template_type", "expected"), [ ( "", @@ -275,7 +275,7 @@ def test_get_rows_only_iterates_over_file_once(mocker): @pytest.mark.parametrize( - "file_contents,template_type,expected", + ("file_contents", "template_type", "expected"), [ ( """ @@ -341,7 +341,7 @@ def test_get_rows_with_errors(): @pytest.mark.parametrize( - "template_type, row_count, header, filler, row_with_error", + ("template_type", "row_count", "header", "filler", "row_with_error"), [ ( "email", @@ -370,7 +370,7 @@ def test_big_list_validates_right_through( @pytest.mark.parametrize( - "template_type, row_count, header, filler", + ("template_type", "row_count", "header", "filler"), [ ("email", 50, "email address\n", "test@example.com\n"), ("sms", 50, "phone number\n", "07900900123\n"), @@ -456,7 +456,7 @@ def test_empty_column_names(): @pytest.mark.parametrize( - "file_contents,template,expected_recipients,expected_personalisation", + ("file_contents", "template", "expected_recipients", "expected_personalisation"), [ ( """ @@ -504,7 +504,7 @@ def test_get_recipient( @pytest.mark.parametrize( - "file_contents,template,expected_recipients,expected_personalisation", + ("file_contents", "template", "expected_recipients", "expected_personalisation"), [ ( """ @@ -539,7 +539,7 @@ def test_get_recipient_respects_order( @pytest.mark.parametrize( - "file_contents,template_type,expected,expected_missing", + ("file_contents", "template_type", "expected", "expected_missing"), [ ("", "sms", [], set(["phone number", "name"])), ( @@ -612,7 +612,7 @@ def test_column_headers(file_contents, template_type, expected, expected_missing ], ) @pytest.mark.parametrize( - "file_contents,template_type", + ("file_contents", "template_type"), [ pytest.param("", "sms", marks=pytest.mark.xfail), pytest.param("name", "sms", marks=pytest.mark.xfail), @@ -660,7 +660,12 @@ def test_recipient_column(content, file_contents, template_type): @pytest.mark.parametrize( - "file_contents,template_type,rows_with_bad_recipients,rows_with_missing_data", + ( + "file_contents", + "template_type", + "rows_with_bad_recipients", + "rows_with_missing_data", + ), [ ( """ @@ -761,7 +766,7 @@ def test_bad_or_missing_data( @pytest.mark.parametrize( - "file_contents,rows_with_bad_recipients", + ("file_contents", "rows_with_bad_recipients"), [ ( """ @@ -811,7 +816,7 @@ def test_errors_when_too_many_rows(): @pytest.mark.parametrize( - "file_contents,template_type,guestlist,count_of_rows_with_errors", + ("file_contents", "template_type", "guestlist", "count_of_rows_with_errors"), [ ( """ @@ -938,7 +943,7 @@ def test_detects_rows_which_result_in_empty_messages(): @pytest.mark.parametrize( - "key, expected", + ("key", "expected"), sum( [ [(key, expected) for key in group] @@ -1002,8 +1007,8 @@ def test_ignores_spaces_and_case_in_placeholders(key, expected): @pytest.mark.parametrize( - "character, name", - ( + ("character", "name"), + [ (" ", "SPACE"), # these ones don’t have unicode names ("\n", None), # newline @@ -1017,7 +1022,7 @@ def test_ignores_spaces_and_case_in_placeholders(key, expected): ("\uFEFF", "ZERO WIDTH NO-BREAK SPACE"), # all the things (" \n\r\t\u000A\u000D\u180E\u200B\u200C\u200D\u2060\uFEFF", None), - ), + ], ) def test_ignores_leading_whitespace_in_file(character, name): if name is not None: @@ -1061,7 +1066,7 @@ def test_dont_error_if_too_many_recipients_not_specified(): @pytest.mark.parametrize( - "index, expected_row", + ("index", "expected_row"), [ ( 0, @@ -1106,7 +1111,7 @@ def test_recipients_can_be_accessed_by_index(index, expected_row): assert recipients[index][key].data == value -@pytest.mark.parametrize("international_sms", (True, False)) +@pytest.mark.parametrize("international_sms", [True, False]) def test_multiple_sms_recipient_columns(international_sms): recipients = RecipientCSV( """ @@ -1131,13 +1136,13 @@ def test_multiple_sms_recipient_columns(international_sms): @pytest.mark.parametrize( "column_name", - ( + [ "phone_number", "phonenumber", "phone number", "phone-number", "p h o n e n u m b e r", - ), + ], ) def test_multiple_sms_recipient_columns_with_missing_data(column_name): recipients = RecipientCSV( @@ -1257,12 +1262,12 @@ def test_multi_line_placeholders_work(): @pytest.mark.parametrize( - "extra_args, expected_errors, expected_bad_rows", - ( + ("extra_args", "expected_errors", "expected_bad_rows"), + [ ({}, True, {0}), ({"allow_international_letters": False}, True, {0}), ({"allow_international_letters": True}, False, set()), - ), + ], ) def test_accepts_international_addresses_when_allowed( extra_args, expected_errors, expected_bad_rows diff --git a/tests/notifications_utils/test_recipient_validation.py b/tests/notifications_utils/test_recipient_validation.py index 91360056e..ff48df775 100644 --- a/tests/notifications_utils/test_recipient_validation.py +++ b/tests/notifications_utils/test_recipient_validation.py @@ -153,7 +153,7 @@ def test_detect_us_phone_numbers(phone_number): @pytest.mark.parametrize( - "phone_number, expected_info", + ("phone_number", "expected_info"), [ # ( # "+4407900900123", @@ -285,13 +285,12 @@ def test_valid_us_phone_number_can_be_formatted_consistently(phone_number): @pytest.mark.parametrize( - "phone_number, expected_formatted", + ("phone_number", "expected_formatted"), [ # ("+44071234567890", "+4471234567890"), ("1-202-555-0104", "+12025550104"), ("+12025550104", "+12025550104"), ("12025550104", "+12025550104"), - ("+12025550104", "+12025550104"), # ("+23051234567", "+23051234567"), ], ) @@ -304,7 +303,7 @@ def test_valid_international_phone_number_can_be_formatted_consistently( ) -@pytest.mark.parametrize("phone_number, error_message", invalid_us_phone_numbers) +@pytest.mark.parametrize(("phone_number", "error_message"), invalid_us_phone_numbers) @pytest.mark.parametrize( "extra_args", [ @@ -318,7 +317,7 @@ def test_phone_number_rejects_invalid_values(extra_args, phone_number, error_mes assert error_message == str(e.value) -@pytest.mark.parametrize("phone_number, error_message", invalid_phone_numbers) +@pytest.mark.parametrize(("phone_number", "error_message"), invalid_phone_numbers) def test_phone_number_rejects_invalid_international_values(phone_number, error_message): with pytest.raises(InvalidPhoneError) as e: validate_phone_number(phone_number, international=True) @@ -384,7 +383,7 @@ def test_validates_against_guestlist_of_email_addresses(email_address): @pytest.mark.parametrize( - "phone_number, expected_formatted", + ("phone_number", "expected_formatted"), [ # ("+4407900900123", "+44 7900 900123"), # UK # ("+44(0)7900900123", "+44 7900 900123"), # UK @@ -403,7 +402,7 @@ def test_format_us_and_international_phone_numbers(phone_number, expected_format @pytest.mark.parametrize( - "recipient, expected_formatted", + ("recipient", "expected_formatted"), [ (True, ""), (False, ""), diff --git a/tests/notifications_utils/test_request_header_authentication.py b/tests/notifications_utils/test_request_header_authentication.py index f595619ce..8a5d93cfc 100644 --- a/tests/notifications_utils/test_request_header_authentication.py +++ b/tests/notifications_utils/test_request_header_authentication.py @@ -5,7 +5,7 @@ from notifications_utils.request_helper import NotifyRequest, _check_proxy_heade @pytest.mark.parametrize( - "header,secrets,expected", + ("header", "secrets", "expected"), [ ( {"X-Custom-Forwarder": "right_key"}, @@ -48,7 +48,7 @@ def test_request_header_authorization(header, secrets, expected): @pytest.mark.parametrize( - "secrets,expected", + ("secrets", "expected"), [ (["old_key", "right_key"], (False, "Header missing")), ], diff --git a/tests/notifications_utils/test_safe_string.py b/tests/notifications_utils/test_safe_string.py index a0bf3360f..613dc85af 100644 --- a/tests/notifications_utils/test_safe_string.py +++ b/tests/notifications_utils/test_safe_string.py @@ -7,7 +7,7 @@ from notifications_utils.safe_string import ( @pytest.mark.parametrize( - "unsafe_string, expected_safe", + ("unsafe_string", "expected_safe"), [ ("name with spaces", "name.with.spaces"), ("singleword", "singleword"), @@ -26,7 +26,7 @@ def test_email_safe_return_dot_separated_email_local_part(unsafe_string, expecte @pytest.mark.parametrize( - "unsafe_string, expected_safe", + ("unsafe_string", "expected_safe"), [ ("name with spaces", "name-with-spaces"), ("singleword", "singleword"), diff --git a/tests/notifications_utils/test_sanitise_text.py b/tests/notifications_utils/test_sanitise_text.py index 062f280e0..e8bdf04dd 100644 --- a/tests/notifications_utils/test_sanitise_text.py +++ b/tests/notifications_utils/test_sanitise_text.py @@ -42,7 +42,7 @@ params, ids = zip( ) -@pytest.mark.parametrize("char, expected", params, ids=ids) +@pytest.mark.parametrize(("char", "expected"), params, ids=ids) @pytest.mark.parametrize("cls", [SanitiseSMS, SanitiseASCII]) def test_encode_chars_the_same_for_ascii_and_sms(char, expected, cls): assert cls.encode_char(char) == expected @@ -64,7 +64,7 @@ params, ids = zip( ) -@pytest.mark.parametrize("char, expected_sms, expected_ascii", params, ids=ids) +@pytest.mark.parametrize(("char", "expected_sms", "expected_ascii"), params, ids=ids) def test_encode_chars_different_between_ascii_and_sms( char, expected_sms, expected_ascii ): @@ -73,7 +73,7 @@ def test_encode_chars_different_between_ascii_and_sms( @pytest.mark.parametrize( - "codepoint, char", + ("codepoint", "char"), [ ("0041", "A"), ("0061", "a"), @@ -87,12 +87,12 @@ def test_get_unicode_char_from_codepoint(codepoint, char): "bad_input", ["", "GJ", "00001", '0001";import sys;sys.exit(0)"'] ) def test_get_unicode_char_from_codepoint_rejects_bad_input(bad_input): - with pytest.raises(ValueError): + with pytest.raises(ValueError): # noqa PT011 SanitiseText.get_unicode_char_from_codepoint(bad_input) @pytest.mark.parametrize( - "content, expected", + ("content", "expected"), [ ("Łōdź", "?odz"), ( @@ -107,7 +107,7 @@ def test_encode_string(content, expected): @pytest.mark.parametrize( - "content, cls, expected", + ("content", "cls", "expected"), [ ("The quick brown fox jumps over the lazy dog", SanitiseSMS, set()), ( @@ -132,7 +132,7 @@ def test_sms_encoding_get_non_compatible_characters(content, cls, expected): @pytest.mark.parametrize( - "content, expected", + ("content", "expected"), [ ("이것은 테스트입니다", True), # Korean ("Αυτό είναι ένα τεστ", True), # Greek @@ -285,7 +285,7 @@ def test_sms_supporting_additional_languages(content, expected): @pytest.mark.parametrize( - "content, expected", + ("content", "expected"), [ ("이것은 테스트입니다", set()), # Korean ("Αυτό είναι ένα τεστ", set()), # Greek diff --git a/tests/notifications_utils/test_serialised_model.py b/tests/notifications_utils/test_serialised_model.py index b83a4d1e5..c1344af48 100644 --- a/tests/notifications_utils/test_serialised_model.py +++ b/tests/notifications_utils/test_serialised_model.py @@ -87,10 +87,10 @@ def test_cant_override_custom_property_from_dict(): @pytest.mark.parametrize( "json_response", - ( + [ {}, {"foo": "bar"}, # Should still raise an exception - ), + ], ) def test_model_raises_for_unknown_attributes(json_response): class Custom(SerialisedModel): @@ -118,10 +118,10 @@ def test_model_raises_keyerror_if_item_missing_from_dict(): @pytest.mark.parametrize( "json_response", - ( + [ {}, {"foo": "bar"}, # Should be ignored - ), + ], ) def test_model_doesnt_swallow_attribute_errors(json_response): class Custom(SerialisedModel): diff --git a/tests/notifications_utils/test_template_change.py b/tests/notifications_utils/test_template_change.py index e0b8df892..12e0f85d3 100644 --- a/tests/notifications_utils/test_template_change.py +++ b/tests/notifications_utils/test_template_change.py @@ -6,7 +6,7 @@ from .test_base_template import ConcreteTemplate @pytest.mark.parametrize( - "old_template, new_template, should_differ", + ("old_template", "new_template", "should_differ"), [ ( ConcreteTemplate({"content": "((1)) ((2)) ((3))"}), @@ -50,7 +50,7 @@ def test_checking_for_difference_between_templates( @pytest.mark.parametrize( - "old_template, new_template, placeholders_added", + ("old_template", "new_template", "placeholders_added"), [ ( ConcreteTemplate({"content": "((1)) ((2)) ((3))"}), @@ -87,7 +87,7 @@ def test_placeholders_added(old_template, new_template, placeholders_added): @pytest.mark.parametrize( - "old_template, new_template, placeholders_removed", + ("old_template", "new_template", "placeholders_removed"), [ ( ConcreteTemplate({"content": "((1)) ((2)) ((3))"}), diff --git a/tests/notifications_utils/test_template_types.py b/tests/notifications_utils/test_template_types.py index 1209d4dc9..1b119f216 100644 --- a/tests/notifications_utils/test_template_types.py +++ b/tests/notifications_utils/test_template_types.py @@ -32,8 +32,8 @@ from notifications_utils.template import ( @pytest.mark.parametrize( - "template_class, expected_error", - ( + ("template_class", "expected_error"), + [ pytest.param( Template, ("Can't instantiate abstract class Template with abstract method __str__"), @@ -104,7 +104,7 @@ from notifications_utils.template import ( sys.version_info < (3, 9), reason="‘method’ will be pluralised" ), ), - ), + ], ) def test_abstract_classes_cant_be_instantiated(template_class, expected_error): with pytest.raises(TypeError) as error: @@ -114,8 +114,8 @@ def test_abstract_classes_cant_be_instantiated(template_class, expected_error): @pytest.mark.parametrize( - "template_class, expected_error", - ( + ("template_class", "expected_error"), + [ ( HTMLEmailTemplate, ("Cannot initialise HTMLEmailTemplate with sms template_type"), @@ -128,7 +128,7 @@ def test_abstract_classes_cant_be_instantiated(template_class, expected_error): BroadcastPreviewTemplate, ("Cannot initialise BroadcastPreviewTemplate with sms template_type"), ), - ), + ], ) def test_errors_for_incompatible_template_type(template_class, expected_error): with pytest.raises(TypeError) as error: @@ -149,7 +149,7 @@ def test_html_email_inserts_body(): @pytest.mark.parametrize( - "content", ("DOCTYPE", "html", "body", "beta.notify.gov", "hello world") + "content", ["DOCTYPE", "html", "body", "beta.notify.gov", "hello world"] ) def test_default_template(content): assert content in str( @@ -163,7 +163,7 @@ def test_default_template(content): ) -@pytest.mark.parametrize("show_banner", (True, False)) +@pytest.mark.parametrize("show_banner", [True, False]) def test_govuk_banner(show_banner): email = HTMLEmailTemplate( { @@ -194,7 +194,7 @@ def test_brand_banner_shows(): @pytest.mark.parametrize( - "brand_logo, brand_text, brand_colour", + ("brand_logo", "brand_text", "brand_colour"), [ ("http://example.com/image.png", "Example", "red"), ("http://example.com/image.png", "Example", "#f00"), @@ -255,7 +255,7 @@ def test_alt_text_with_no_brand_text_and_govuk_banner_shown(): @pytest.mark.parametrize( - "brand_banner, brand_text, expected_alt_text", + ("brand_banner", "brand_text", "expected_alt_text"), [ (True, None, 'alt="Notify Logo"'), (True, "Example", 'alt=""'), @@ -278,9 +278,9 @@ def test_alt_text_with_no_govuk_banner(brand_banner, brand_text, expected_alt_te assert expected_alt_text in email -@pytest.mark.parametrize("complete_html", (True, False)) +@pytest.mark.parametrize("complete_html", [True, False]) @pytest.mark.parametrize( - "branding_should_be_present, brand_logo, brand_text, brand_colour", + ("branding_should_be_present", "brand_logo", "brand_text", "brand_colour"), [ (True, "http://example.com/image.png", "Example", "#f00"), (True, "http://example.com/image.png", "Example", None), @@ -289,7 +289,7 @@ def test_alt_text_with_no_govuk_banner(brand_banner, brand_text, expected_alt_te (False, "http://example.com/image.png", None, "#f00"), ], ) -@pytest.mark.parametrize("content", ("DOCTYPE", "html", "body")) +@pytest.mark.parametrize("content", ["DOCTYPE", "html", "body"]) def test_complete_html( complete_html, branding_should_be_present, @@ -351,7 +351,7 @@ def test_preheader_is_at_start_of_html_emails(): @pytest.mark.parametrize( - "content, values, expected_preheader", + ("content", "values", "expected_preheader"), [ ( ( @@ -426,22 +426,22 @@ def test_content_of_preheader_in_html_emails( @pytest.mark.parametrize( - "template_class, template_type, extra_args, result, markdown_renderer", + ("template_class", "template_type", "extra_args", "result", "markdown_renderer"), [ - [ + ( HTMLEmailTemplate, "email", {}, ("the quick brown fox\n" "\n" "jumped over the lazy dog\n"), "notifications_utils.template.notify_email_markdown", - ], - [ + ), + ( LetterPreviewTemplate, "letter", {}, ("the quick brown fox\n" "\n" "jumped over the lazy dog\n"), "notifications_utils.template.notify_letter_preview_markdown", - ], + ), ], ) def test_markdown_in_templates( @@ -471,7 +471,7 @@ def test_markdown_in_templates( @pytest.mark.parametrize( - "template_class, template_type, extra_attributes", + ("template_class", "template_type", "extra_attributes"), [ (HTMLEmailTemplate, "email", 'style="word-wrap: break-word; color: #1D70B8;"'), ( @@ -494,7 +494,7 @@ def test_markdown_in_templates( ], ) @pytest.mark.parametrize( - "url, url_with_entities_replaced", + ("url", "url_with_entities_replaced"), [ ("http://example.com", "http://example.com"), ("http://www.gov.uk/", "http://www.gov.uk/"), @@ -530,15 +530,15 @@ def test_makes_links_out_of_URLs( @pytest.mark.parametrize( - "template_class, template_type", - ( + ("template_class", "template_type"), + [ (SMSPreviewTemplate, "sms"), (BroadcastPreviewTemplate, "broadcast"), - ), + ], ) @pytest.mark.parametrize( - "url, url_with_entities_replaced", - ( + ("url", "url_with_entities_replaced"), + [ ("example.com", "example.com"), ("www.gov.uk/", "www.gov.uk/"), ("service.gov.uk", "service.gov.uk"), @@ -547,7 +547,7 @@ def test_makes_links_out_of_URLs( "service.gov.uk/blah.ext?q=a%20b%20c&order=desc#fragment", "service.gov.uk/blah.ext?q=a%20b%20c&order=desc#fragment", ), - ), + ], ) def test_makes_links_out_of_URLs_without_protocol_in_sms_and_broadcast( template_class, @@ -567,8 +567,8 @@ def test_makes_links_out_of_URLs_without_protocol_in_sms_and_broadcast( @pytest.mark.parametrize( - "content, html_snippet", - ( + ("content", "html_snippet"), + [ ( ( "You've been invited to a service. Click this link:\n" @@ -592,7 +592,7 @@ def test_makes_links_out_of_URLs_without_protocol_in_sms_and_broadcast( "" ), ), - ), + ], ) def test_HTML_template_has_URLs_replaced_with_links(content, html_snippet): assert html_snippet in str( @@ -601,7 +601,7 @@ def test_HTML_template_has_URLs_replaced_with_links(content, html_snippet): @pytest.mark.parametrize( - "template_content,expected", + ("template_content", "expected"), [ ("gov.uk", "gov.\u200Buk"), ("GOV.UK", "GOV.\u200BUK"), @@ -660,7 +660,7 @@ def test_stripping_of_unsupported_characters_in_email_templates(): @mock.patch("notifications_utils.template.add_prefix", return_value="") @pytest.mark.parametrize( - "template_class, prefix, body, expected_call", + ("template_class", "prefix", "body", "expected_call"), [ (SMSMessageTemplate, "a", "b", (Markup("b"), "a")), (SMSPreviewTemplate, "a", "b", (Markup("b"), "a")), @@ -705,7 +705,7 @@ def test_sms_message_adds_prefix( ], ) @pytest.mark.parametrize( - "show_prefix, prefix, body, sender, expected_call", + ("show_prefix", "prefix", "body", "sender", "expected_call"), [ (False, "a", "b", "c", (Markup("b"), None)), (True, "a", "b", None, (Markup("b"), "a")), @@ -761,14 +761,14 @@ def test_sms_message_preview_hides_sender_by_default(): @mock.patch("notifications_utils.template.sms_encode", return_value="downgraded") @pytest.mark.parametrize( - "template_class, extra_args, expected_call", - ( + ("template_class", "extra_args", "expected_call"), + [ (SMSMessageTemplate, {"prefix": "Service name"}, "Service name: Message"), (SMSPreviewTemplate, {"prefix": "Service name"}, "Service name: Message"), (BroadcastMessageTemplate, {}, "Message"), (BroadcastPreviewTemplate, {"prefix": "Service name"}, "Service name: Message"), (SMSBodyPreviewTemplate, {}, "Message"), - ), + ], ) def test_sms_messages_downgrade_non_sms( mock_sms_encode, @@ -788,10 +788,10 @@ def test_sms_messages_downgrade_non_sms( @pytest.mark.parametrize( "template_class", - ( + [ SMSPreviewTemplate, BroadcastPreviewTemplate, - ), + ], ) @mock.patch("notifications_utils.template.sms_encode", return_value="downgraded") def test_sms_messages_dont_downgrade_non_sms_if_setting_is_false( @@ -810,10 +810,10 @@ def test_sms_messages_dont_downgrade_non_sms_if_setting_is_false( @pytest.mark.parametrize( "template_class", - ( + [ SMSPreviewTemplate, BroadcastPreviewTemplate, - ), + ], ) @mock.patch("notifications_utils.template.nl2br") def test_sms_preview_adds_newlines(nl2br, template_class): @@ -862,13 +862,13 @@ def test_broadcast_message_normalises_newlines(content): @pytest.mark.parametrize( "template_class", - ( + [ SMSMessageTemplate, SMSBodyPreviewTemplate, BroadcastMessageTemplate, # Note: SMSPreviewTemplate and BroadcastPreviewTemplate not tested here # as both will render full HTML template, not just the body - ), + ], ) def test_phone_templates_normalise_whitespace(template_class): content = " Hi\u00A0there\u00A0 what's\u200D up\t" @@ -889,7 +889,7 @@ def test_phone_templates_normalise_whitespace(template_class): "notifications_utils.template.notify_letter_preview_markdown", return_value="Bar" ) @pytest.mark.parametrize( - "values, expected_address", + ("values", "expected_address"), [ ( {}, @@ -933,7 +933,7 @@ def test_phone_templates_normalise_whitespace(template_class): ], ) @pytest.mark.parametrize( - "contact_block, expected_rendered_contact_block", + ("contact_block", "expected_rendered_contact_block"), [ (None, ""), ("", ""), @@ -962,14 +962,14 @@ def test_phone_templates_normalise_whitespace(template_class): ], ) @pytest.mark.parametrize( - "extra_args, expected_logo_file_name, expected_logo_class", + ("extra_args", "expected_logo_file_name", "expected_logo_class"), [ ({}, None, None), ({"logo_file_name": "example.foo"}, "example.foo", "foo"), ], ) @pytest.mark.parametrize( - "additional_extra_args, expected_date", + ("additional_extra_args", "expected_date"), [ ({}, "12 December 2012"), ({"date": None}, "12 December 2012"), @@ -1044,7 +1044,7 @@ def test_letter_preview_renderer_without_mocks(jinja_template): @freeze_time("2012-12-12 12:12:12") @mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render") @pytest.mark.parametrize( - "page_count, expected_oversized, expected_page_numbers", + ("page_count", "expected_oversized", "expected_page_numbers"), [ ( 1, @@ -1074,8 +1074,13 @@ def test_letter_preview_renderer_without_mocks(jinja_template): ], ) @pytest.mark.parametrize( - "postage_args, expected_show_postage, expected_postage_class_value, expected_postage_description", ( + "postage_args", + "expected_show_postage", + "expected_postage_class_value", + "expected_postage_description", + ), + [ pytest.param({}, False, None, None), pytest.param({"postage": None}, False, None, None), pytest.param({"postage": "first"}, True, "letter-postage-first", "first class"), @@ -1098,7 +1103,7 @@ def test_letter_preview_renderer_without_mocks(jinja_template): "third class", marks=pytest.mark.xfail(raises=TypeError), ), - ), + ], ) def test_letter_image_renderer( jinja_template, @@ -1147,13 +1152,13 @@ def test_letter_image_renderer( @mock.patch("notifications_utils.template.LetterImageTemplate.jinja_template.render") @pytest.mark.parametrize( "postage_argument", - ( + [ None, "first", "second", "europe", "rest-of-world", - ), + ], ) def test_letter_image_renderer_shows_international_post( jinja_template, @@ -1219,7 +1224,7 @@ def test_letter_image_renderer_pagination(page_image_url): @pytest.mark.parametrize( - "partial_call, expected_exception, expected_message", + ("partial_call", "expected_exception", "expected_message"), [ ( partial(LetterImageTemplate), @@ -1261,8 +1266,8 @@ def test_letter_image_renderer_requires_arguments( @pytest.mark.parametrize( - "postage, expected_attribute_value, expected_postage_text", - ( + ("postage", "expected_attribute_value", "expected_postage_text"), + [ (None, None, None), ( "first", @@ -1284,7 +1289,7 @@ def test_letter_image_renderer_requires_arguments( ["letter-postage", "letter-postage-international"], "Postage: international", ), - ), + ], ) def test_letter_image_renderer_passes_postage_to_html_attribute( postage, @@ -1315,20 +1320,20 @@ def test_letter_image_renderer_passes_postage_to_html_attribute( @pytest.mark.parametrize( "template_class", - ( + [ SMSBodyPreviewTemplate, SMSMessageTemplate, SMSPreviewTemplate, BroadcastMessageTemplate, BroadcastPreviewTemplate, - ), + ], ) @pytest.mark.parametrize( "template_json", - ( + [ {"content": ""}, {"content": "", "subject": "subject"}, - ), + ], ) def test_sms_templates_have_no_subject(template_class, template_json): template_json.update(template_type=template_class.template_type) @@ -1358,8 +1363,8 @@ def test_subject_line_gets_applied_to_correct_template_types(): @pytest.mark.parametrize( - "template_class, template_type, extra_args", - ( + ("template_class", "template_type", "extra_args"), + [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), (PlainTextEmailTemplate, "email", {}), @@ -1373,7 +1378,7 @@ def test_subject_line_gets_applied_to_correct_template_types(): "page_count": 1, }, ), - ), + ], ) def test_subject_line_gets_replaced(template_class, template_type, extra_args): template = template_class( @@ -1386,8 +1391,8 @@ def test_subject_line_gets_replaced(template_class, template_type, extra_args): @pytest.mark.parametrize( - "template_class, template_type, extra_args", - ( + ("template_class", "template_type", "extra_args"), + [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), (PlainTextEmailTemplate, "email", {}), @@ -1401,10 +1406,10 @@ def test_subject_line_gets_replaced(template_class, template_type, extra_args): "page_count": 1, }, ), - ), + ], ) @pytest.mark.parametrize( - "content, values, expected_count", + ("content", "values", "expected_count"), [ ("Content with ((placeholder))", {"placeholder": "something extra"}, 28), ("Content with ((placeholder))", {"placeholder": ""}, 12), @@ -1441,7 +1446,13 @@ def test_character_count_for_non_sms_templates( ], ) @pytest.mark.parametrize( - "content, values, prefix, expected_count_in_template, expected_count_in_notification", + ( + "content", + "values", + "prefix", + "expected_count_in_template", + "expected_count_in_notification", + ), [ # is an unsupported unicode character so should be replaced with a ? ("深", {}, None, 1, 1), @@ -1507,7 +1518,12 @@ def test_character_count_for_sms_templates( ], ) @pytest.mark.parametrize( - "content, values, expected_count_in_template, expected_count_in_notification", + ( + "content", + "values", + "expected_count_in_template", + "expected_count_in_notification", + ), [ # is an unsupported unicode character so should be replaced with a ? ("深", {}, 1, 1), @@ -1546,13 +1562,13 @@ def test_character_count_for_broadcast_templates( @pytest.mark.parametrize( "template_class", - ( + [ SMSMessageTemplate, BroadcastMessageTemplate, - ), + ], ) @pytest.mark.parametrize( - "msg, expected_sms_fragment_count", + ("msg", "expected_sms_fragment_count"), [ ( """This is a very long long long long long long long long long long @@ -1575,13 +1591,13 @@ def test_sms_fragment_count_accounts_for_unicode_and_welsh_characters( @pytest.mark.parametrize( "template_class", - ( + [ SMSMessageTemplate, BroadcastMessageTemplate, - ), + ], ) @pytest.mark.parametrize( - "msg, expected_sms_fragment_count", + ("msg", "expected_sms_fragment_count"), [ # all extended GSM characters ( @@ -1647,7 +1663,7 @@ def test_sms_fragment_count_accounts_for_non_latin_characters( ], ) @pytest.mark.parametrize( - "content, values, prefix, expected_result", + ("content", "values", "prefix", "expected_result"), [ ("", {}, None, True), ("", {}, "GDS", True), @@ -1676,7 +1692,7 @@ def test_is_message_empty_sms_templates( ], ) @pytest.mark.parametrize( - "content, values, expected_result", + ("content", "values", "expected_result"), [ ("", {}, True), ("((placeholder))", {"placeholder": ""}, True), @@ -1696,14 +1712,14 @@ def test_is_message_empty_broadcast_templates( @pytest.mark.parametrize( - "template_class, template_type", - ( + ("template_class", "template_type"), + [ (HTMLEmailTemplate, "email"), (LetterPrintTemplate, "letter"), - ), + ], ) @pytest.mark.parametrize( - "content, values, expected_result", + ("content", "values", "expected_result"), [ ("", {}, True), ("((placeholder))", {"placeholder": ""}, True), @@ -1735,14 +1751,14 @@ def test_is_message_empty_email_and_letter_templates( @pytest.mark.parametrize( - "template_class, template_type", - ( + ("template_class", "template_type"), + [ (HTMLEmailTemplate, "email"), (LetterPrintTemplate, "letter"), - ), + ], ) @pytest.mark.parametrize( - "content, values", + ("content", "values"), [ ("Some content", {}), ("((placeholder)) some content", {"placeholder": ""}), @@ -1781,7 +1797,7 @@ def test_is_message_empty_email_and_letter_templates_tries_not_to_count_chars( @pytest.mark.parametrize( - "template_class, template_type, extra_args, expected_field_calls", + ("template_class", "template_type", "extra_args", "expected_field_calls"), [ ( PlainTextEmailTemplate, @@ -2054,7 +2070,12 @@ def test_templates_handle_html_and_redacting( @pytest.mark.parametrize( - "template_class, template_type, extra_args, expected_remove_whitespace_calls", + ( + "template_class", + "template_type", + "extra_args", + "expected_remove_whitespace_calls", + ), [ ( PlainTextEmailTemplate, @@ -2176,7 +2197,7 @@ def test_templates_remove_whitespace_before_punctuation( @pytest.mark.parametrize( - "template_class, template_type, extra_args, expected_calls", + ("template_class", "template_type", "extra_args", "expected_calls"), [ ( PlainTextEmailTemplate, @@ -2259,18 +2280,18 @@ def test_templates_make_quotes_smart_and_dashes_en( @pytest.mark.parametrize( "content", - ( + [ "first.o'last@example.com", "first.o’last@example.com", - ), + ], ) @pytest.mark.parametrize( "template_class", - ( + [ HTMLEmailTemplate, PlainTextEmailTemplate, EmailPreviewTemplate, - ), + ], ) def test_no_smart_quotes_in_email_addresses(template_class, content): template = template_class( @@ -2302,7 +2323,7 @@ def test_smart_quotes_removed_from_long_template_in_under_a_second(): @pytest.mark.parametrize( - "template_instance, expected_placeholders", + ("template_instance", "expected_placeholders"), [ ( SMSMessageTemplate( @@ -2465,7 +2486,7 @@ def test_email_preview_shows_reply_to_address(extra_args): @pytest.mark.parametrize( - "template_values, expected_content", + ("template_values", "expected_content"), [ ({}, "email address"), ({"email address": "test@example.com"}, "test@example.com"), @@ -2483,7 +2504,7 @@ def test_email_preview_shows_recipient_address( @pytest.mark.parametrize( - "address, expected", + ("address", "expected"), [ ( { @@ -2585,7 +2606,7 @@ def test_email_preview_shows_recipient_address( ), ], ) -@pytest.mark.parametrize("template_class", (LetterPreviewTemplate, LetterPrintTemplate)) +@pytest.mark.parametrize("template_class", [LetterPreviewTemplate, LetterPrintTemplate]) def test_letter_address_format(template_class, address, expected): template = BeautifulSoup( str( @@ -2601,7 +2622,7 @@ def test_letter_address_format(template_class, address, expected): @freeze_time("2001-01-01 12:00:00.000000") @pytest.mark.parametrize( - "markdown, expected", + ("markdown", "expected"), [ ( ( @@ -2691,11 +2712,11 @@ def test_message_is_not_too_long_ignoring_prefix(template_class): @pytest.mark.parametrize( - "extra_characters, expected_too_long", - ( + ("extra_characters", "expected_too_long"), + [ ("cc", True), # content length is 919 characters (more than limit of 918) ("c", False), # content length is 918 characters (not more than limit of 918) - ), + ], ) @pytest.mark.parametrize( "template_class", @@ -2716,7 +2737,7 @@ def test_broadcast_message_too_long( @pytest.mark.parametrize( - "template_class, template_type, kwargs", + ("template_class", "template_type", "kwargs"), [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), @@ -2736,7 +2757,7 @@ def test_message_too_long_limit_bigger_or_nonexistent_for_non_sms_templates( @pytest.mark.parametrize( - "template_class, template_type, kwargs", + ("template_class", "template_type", "kwargs"), [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), @@ -2755,7 +2776,7 @@ def test_content_size_in_bytes_for_email_messages( @pytest.mark.parametrize( - "template_class, template_type, kwargs", + ("template_class", "template_type", "kwargs"), [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), @@ -2774,7 +2795,7 @@ def test_message_too_long_for_a_too_big_email_message( @pytest.mark.parametrize( - "template_class, template_type, kwargs", + ("template_class", "template_type", "kwargs"), [ (EmailPreviewTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), @@ -2792,7 +2813,7 @@ def test_message_too_long_for_an_email_message_within_limits( @pytest.mark.parametrize( - ("content," "expected_preview_markup,"), + ("content", "expected_preview_markup"), [ ( "a\n\n\nb", @@ -2847,7 +2868,7 @@ def test_multiple_newlines_in_letters( ], ) @pytest.mark.parametrize( - "template_class, template_type, extra_args", + ("template_class", "template_type", "extra_args"), [ (PlainTextEmailTemplate, "email", {}), (HTMLEmailTemplate, "email", {}), @@ -2886,7 +2907,7 @@ def test_whitespace_in_subject_placeholders(template_class): @pytest.mark.parametrize( - "template_class, expected_output", + ("template_class", "expected_output"), [ ( PlainTextEmailTemplate, @@ -3041,8 +3062,8 @@ def test_plain_text_email_whitespace(): @pytest.mark.parametrize( - "renderer, template_type, expected_content", - ( + ("renderer", "template_type", "expected_content"), + [ ( PlainTextEmailTemplate, "email", @@ -3071,7 +3092,7 @@ def test_plain_text_email_whitespace(): "letter", ("

Heading link: example.com

"), ), - ), + ], ) def test_heading_only_template_renders(renderer, template_type, expected_content): assert expected_content in str( @@ -3093,7 +3114,7 @@ def test_heading_only_template_renders(renderer, template_type, expected_content ], ) @pytest.mark.parametrize( - "filename, expected_html_class", + ("filename", "expected_html_class"), [ ("example.png", 'class="png"'), ("example.svg", 'class="svg"'), @@ -3127,7 +3148,7 @@ def test_image_not_present_if_no_logo(template_class): @pytest.mark.parametrize( "content", - ( + [ ( "The quick brown fox.\n" "\n\n\n\n" @@ -3140,11 +3161,11 @@ def test_image_not_present_if_no_logo(template_class): " Jumps over the lazy dog . \n" "Single linebreak above. \n \n \n" ), - ), + ], ) @pytest.mark.parametrize( - "template_class, expected", - ( + ("template_class", "expected"), + [ ( SMSBodyPreviewTemplate, ( @@ -3192,7 +3213,7 @@ def test_image_not_present_if_no_logo(template_class): "" ), ), - ), + ], ) def test_text_messages_collapse_consecutive_whitespace( template_class, @@ -3252,14 +3273,14 @@ def test_broadcast_message_from_event(): @pytest.mark.parametrize( "template_class", - ( + [ BroadcastMessageTemplate, BroadcastPreviewTemplate, - ), + ], ) @pytest.mark.parametrize( - "content, expected_non_gsm, expected_max, expected_too_long", - ( + ("content", "expected_non_gsm", "expected_max", "expected_too_long"), + [ ( "a" * 1395, set(), @@ -3304,7 +3325,7 @@ def test_broadcast_message_from_event(): 615, False, ), - ), + ], ) def test_broadcast_message_content_count( content, expected_non_gsm, expected_max, expected_too_long, template_class @@ -3322,10 +3343,10 @@ def test_broadcast_message_content_count( @pytest.mark.parametrize( "template_class", - ( + [ BroadcastMessageTemplate, BroadcastPreviewTemplate, - ), + ], ) @pytest.mark.parametrize("content", ("^{}\\[~]|€")) def test_broadcast_message_double_counts_extended_gsm( @@ -3344,10 +3365,10 @@ def test_broadcast_message_double_counts_extended_gsm( @pytest.mark.parametrize( "template_class", - ( + [ BroadcastMessageTemplate, BroadcastPreviewTemplate, - ), + ], ) @pytest.mark.parametrize( "content", ("ÁÍÓÚẂÝ" "ËÏẄŸ" "ÂÊÎÔÛŴŶ" "ÀÈÌÒẀÙỲ" "áíóúẃý" "ëïẅÿ" "âêîôûŵŷ" "ẁỳ") @@ -3368,10 +3389,10 @@ def test_broadcast_message_single_counts_diacritics_in_extended_gsm( @pytest.mark.parametrize( "template_class", - ( + [ BroadcastMessageTemplate, BroadcastPreviewTemplate, - ), + ], ) @pytest.mark.parametrize("content", ("ÄÖÜ" "É" "äöü" "é" "àèìòù")) def test_broadcast_message_single_counts_diacritics_in_gsm( diff --git a/tests/notifications_utils/test_timezones.py b/tests/notifications_utils/test_timezones.py index bfcf413f6..d92bb54b7 100644 --- a/tests/notifications_utils/test_timezones.py +++ b/tests/notifications_utils/test_timezones.py @@ -5,7 +5,7 @@ from notifications_utils.timezones import utc_string_to_aware_gmt_datetime @pytest.mark.parametrize( - "input_value,expectation", + ("input_value", "expectation"), [ ("foo", pytest.raises(dateutil.parser._parser.ParserError)), (100, pytest.raises(TypeError)), @@ -20,7 +20,7 @@ def test_utc_string_to_aware_gmt_datetime_rejects_bad_input(input_value, expecta @pytest.mark.parametrize( - "naive_time, expected_aware_hour", + ("naive_time", "expected_aware_hour"), [ ("2000-12-1 20:01", "15:01"), ("2000-06-1 20:01", "16:01"), diff --git a/tests/notifications_utils/test_url_safe_tokens.py b/tests/notifications_utils/test_url_safe_tokens.py index 624f80f9c..68eb9c962 100644 --- a/tests/notifications_utils/test_url_safe_tokens.py +++ b/tests/notifications_utils/test_url_safe_tokens.py @@ -1,7 +1,7 @@ import urllib +import pytest from itsdangerous import BadSignature, SignatureExpired -from pytest import fail from notifications_utils.url_safe_token import check_token, generate_token @@ -19,7 +19,7 @@ def test_should_throw_exception_when_token_is_tampered_with(): token = generate_token(str(uuid.uuid4()), "secret-key", "dangerous-salt") try: check_token(token + "qerqwer", "secret-key", "dangerous-salt", 30) - fail() + pytest.fail("Expected a BadSignature") except BadSignature: pass @@ -31,6 +31,6 @@ def test_return_none_when_token_is_expired(): token = urllib.parse.unquote(token) try: assert check_token(token, "secret-key", "dangerous-salt", max_age) is None - fail("Expected a SignatureExpired exception") + pytest.fail("Expected a SignatureExpired exception") except SignatureExpired: pass