Files
notifications-admin/tests/notifications_utils/test_placeholders.py
Carlo Costino 9a83ba7475 Localize notification_utils to the admin
This changeset pulls in all of the notification_utils code directly into the admin and removes it as an external dependency. We are doing this to cut down on operational maintenance of the project and will begin removing parts of it no longer needed for the admin.

Signed-off-by: Carlo Costino <carlo.costino@gsa.gov>
2024-05-16 10:37:37 -04:00

67 lines
1.7 KiB
Python

import re
import pytest
from notifications_utils.field import Placeholder
@pytest.mark.parametrize(
"body, expected",
[
("((with-brackets))", "with-brackets"),
("without-brackets", "without-brackets"),
],
)
def test_placeholder_returns_name(body, expected):
assert Placeholder(body).name == expected
@pytest.mark.parametrize(
"body, is_conditional",
[
("not a conditional", False),
("not? a conditional", False),
("a?? conditional", True),
],
)
def test_placeholder_identifies_conditional(body, is_conditional):
assert Placeholder(body).is_conditional() == is_conditional
@pytest.mark.parametrize(
"body, conditional_text",
[
("a??b", "b"),
("a?? b ", " b "),
("a??b??c", "b??c"),
],
)
def test_placeholder_gets_conditional_text(body, conditional_text):
assert Placeholder(body).conditional_text == conditional_text
def test_placeholder_raises_if_accessing_conditional_text_on_non_conditional():
with pytest.raises(ValueError):
Placeholder("hello").conditional_text
@pytest.mark.parametrize(
"body, value, result",
[
("a??b", "Yes", "b"),
("a??b", "No", ""),
],
)
def test_placeholder_gets_conditional_body(body, value, result):
assert Placeholder(body).get_conditional_body(value) == result
def test_placeholder_raises_if_getting_conditional_body_on_non_conditional():
with pytest.raises(ValueError):
Placeholder("hello").get_conditional_body("Yes")
def test_placeholder_can_be_constructed_from_regex_match():
match = re.search(r"\(\(.*\)\)", "foo ((bar)) baz")
assert Placeholder.from_match(match).name == "bar"