def test_format_ordinal_gaelic():
     gaelic_transforms = PlaceholderTransforms(language="ga")
     assert gaelic_transforms.format_ordinal(1) == "1ú"
     assert gaelic_transforms.format_ordinal(2) == "2ú"
     assert gaelic_transforms.format_ordinal(5) == "5ú"
     assert gaelic_transforms.format_ordinal(7) == "7ú"
     assert gaelic_transforms.format_ordinal(21) == "21ú"
Example #2
0
 def test_calculate_difference(self):
     assert (
         PlaceholderTransforms.calculate_date_difference("2016-06-10", "2019-06-10")
         == "3 years"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2018-06-10", "2019-06-10")
         == "1 year"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2010-01-01", "2018-12-31")
         == "8 years"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2011-01", "2015-04")
         == "4 years"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2019-06-10", "2019-08-11")
         == "2 months"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2019-07-10", "2019-08-11")
         == "1 month"
     )
     assert (
         PlaceholderTransforms.calculate_date_difference("2019-07-10", "2019-07-11")
         == "1 day"
     )
     assert PlaceholderTransforms.calculate_date_difference("now", "now") == "0 days"
     with self.assertRaises(ValueError):
         PlaceholderTransforms.calculate_date_difference("2018", "now")
 def test_format_ordinal_without_determiner_ulster_scots():
     ulster_scots_transforms = PlaceholderTransforms(language="eo")
     assert ulster_scots_transforms.format_ordinal(1) == "1st"
     assert ulster_scots_transforms.format_ordinal(2) == "2nd"
     assert ulster_scots_transforms.format_ordinal(3) == "3rd"
     assert ulster_scots_transforms.format_ordinal(4) == "4th"
     assert ulster_scots_transforms.format_ordinal(21) == "21st"
Example #4
0
    def __init__(
        self,
        language,
        schema=None,
        answer_store=None,
        metadata=None,
        list_item_id=None,
        location=None,
        list_store=None,
    ):

        self._schema = schema
        self._answer_store = answer_store or AnswerStore()
        self._metadata = metadata
        self._list_item_id = list_item_id
        self._list_store = list_store
        self._location = location
        self._transformer = PlaceholderTransforms(language)
        self._placeholder_map = {}

        self._value_source_resolver = ValueSourceResolver(
            answer_store=self._answer_store,
            list_store=self._list_store,
            metadata=self._metadata,
            schema=self._schema,
            location=self._location,
            list_item_id=self._list_item_id,
            escape_answer_values=True,
        )
def test_option_label_from_value_with_placeholder_label(
    placeholder_transform,
    answer_id,
    value,
    expected,
    placeholder_renderer,
    option_label_from_value_schema,
):
    label_renderer = placeholder_renderer
    placeholder_transform = PlaceholderTransforms(
        language="en",
        schema=option_label_from_value_schema,
        renderer=label_renderer)
    actual = placeholder_transform.option_label_from_value(value, answer_id)

    assert actual == expected
def test_calculate_difference():
    assert (PlaceholderTransforms.calculate_date_difference(
        "2016-06-10", "2019-06-10") == "3 years")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2018-06-10", "2019-06-10") == "1 year")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2010-01-01", "2018-12-31") == "8 years")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2011-01", "2015-04") == "4 years")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2019-06-10", "2019-08-11") == "2 months")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2019-07-10", "2019-08-11") == "1 month")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2019-07-10", "2019-07-11") == "1 day")
    assert PlaceholderTransforms.calculate_date_difference("now",
                                                           "now") == "0 days"
    assert (PlaceholderTransforms.calculate_date_difference(
        "2021-07-29T10:53:41.511833+00:00",
        "2021-09-29T10:53:41.511833+00:00") == "2 months")
    assert (PlaceholderTransforms.calculate_date_difference(
        "2021-09-28", "2021-09-29T10:53:41.511833+00:00") == "1 day")
Example #7
0
 def test_format_ordinal_with_determiner_ulster_scots():
     ulster_scots_transforms = PlaceholderTransforms(language="eo")
     assert ulster_scots_transforms.format_ordinal(1, "a_or_an") == "a 1st"
     assert ulster_scots_transforms.format_ordinal(2, "a_or_an") == "a 2nd"
     assert ulster_scots_transforms.format_ordinal(3, "a_or_an") == "a 3rd"
     assert ulster_scots_transforms.format_ordinal(4, "a_or_an") == "a 4th"
     assert ulster_scots_transforms.format_ordinal(8, "a_or_an") == "an 8th"
     assert ulster_scots_transforms.format_ordinal(11, "a_or_an") == "an 11th"
 def test_format_possessive_non_english_does_nothing():
     welsh_transforms = PlaceholderTransforms(language="cy")
     assert welsh_transforms.format_possessive(
         "Alice Aardvark") == "Alice Aardvark"
     assert (welsh_transforms.format_possessive("Dave Dixon Davies") ==
             "Dave Dixon Davies")
     assert (welsh_transforms.format_possessive("Alice Aardvark's") ==
             "Alice Aardvark's")
     assert (welsh_transforms.format_possessive("Alice Aardvark’s") ==
             "Alice Aardvark’s")
 def test_format_ordinal_welsh():
     welsh_transforms = PlaceholderTransforms(language="cy")
     assert welsh_transforms.format_ordinal(1) == "1af"
     assert welsh_transforms.format_ordinal(2) == "2il"
     assert welsh_transforms.format_ordinal(3) == "3ydd"
     assert welsh_transforms.format_ordinal(7) == "7fed"
     assert welsh_transforms.format_ordinal(13) == "13eg"
     assert welsh_transforms.format_ordinal(18) == "18fed"
     assert welsh_transforms.format_ordinal(21) == "21ain"
     assert welsh_transforms.format_ordinal(40) == "40ain"
    def __init__(
        self,
        language,
        schema=None,
        answer_store=None,
        metadata=None,
        list_item_id=None,
        location=None,
        list_store=None,
    ):

        self._schema = schema
        self._answer_store = answer_store or AnswerStore()
        self._metadata = metadata
        self._list_item_id = list_item_id
        self._list_store = list_store
        self._location = location
        self._transformer = PlaceholderTransforms(language)
        self._placeholder_map = {}
Example #11
0
    def __init__(
        self,
        language: str,
        answer_store: AnswerStore,
        list_store: ListStore,
        metadata: Mapping,
        response_metadata: Mapping,
        schema: QuestionnaireSchema,
        renderer: "PlaceholderRenderer",
        list_item_id: Optional[str] = None,
        location: Union[Location, RelationshipLocation, None] = None,
    ):

        self._answer_store = answer_store
        self._list_store = list_store
        self._metadata = metadata
        self._response_metadata = response_metadata
        self._schema = schema
        self._list_item_id = list_item_id
        self._location = location
        self._transformer = PlaceholderTransforms(language, schema, renderer)
        self._placeholder_map: MutableMapping[str,
                                              Union[ValueSourceEscapedTypes,
                                                    ValueSourceTypes,
                                                    None]] = {}

        self._value_source_resolver = ValueSourceResolver(
            answer_store=self._answer_store,
            list_store=self._list_store,
            metadata=self._metadata,
            schema=self._schema,
            location=self._location,
            list_item_id=self._list_item_id,
            escape_answer_values=True,
            response_metadata=self._response_metadata,
            use_default_answer=True,
        )
 def setUp(self):
     self.transforms = PlaceholderTransforms(language="en")
class TestPlaceholderParser(unittest.TestCase):
    def setUp(self):
        self.transforms = PlaceholderTransforms(language="en")

    def test_format_currency(self):
        assert self.transforms.format_currency("11", "GBP") == "£11.00"
        assert self.transforms.format_currency("11.99", "GBP") == "£11.99"
        assert self.transforms.format_currency("11000",
                                               "USD") == "US$11,000.00"
        assert self.transforms.format_currency(0) == "£0.00"
        assert self.transforms.format_currency(0.00) == "£0.00"

    def test_format_number(self):
        assert self.transforms.format_number(123) == "123"
        assert self.transforms.format_number("123.4") == "123.4"
        assert self.transforms.format_number("123.40") == "123.4"
        assert self.transforms.format_number("1000") == "1,000"
        assert self.transforms.format_number("10000") == "10,000"
        assert self.transforms.format_number("100000000") == "100,000,000"
        assert self.transforms.format_number(0) == "0"
        assert self.transforms.format_number(0.00) == "0"
        assert self.transforms.format_number("") == ""
        assert self.transforms.format_number(None) == ""

    def test_format_list(self):
        names = ["Alice Aardvark", "Bob Berty Brown", "Dave Dixon Davies"]

        format_value = self.transforms.format_list(names)

        expected_result = ("<ul>"
                           "<li>Alice Aardvark</li>"
                           "<li>Bob Berty Brown</li>"
                           "<li>Dave Dixon Davies</li>"
                           "</ul>")

        assert expected_result == format_value

    def test_format_possessive(self):
        assert self.transforms.format_possessive(
            "Alice Aardvark") == "Alice Aardvark’s"
        assert (self.transforms.format_possessive("Dave Dixon Davies") ==
                "Dave Dixon Davies’")
        assert (self.transforms.format_possessive("Alice Aardvark's") ==
                "Alice Aardvark’s")
        assert (self.transforms.format_possessive("Alice Aardvark’s") ==
                "Alice Aardvark’s")

    @staticmethod
    def test_format_possessive_non_english_does_nothing():
        welsh_transforms = PlaceholderTransforms(language="cy")
        assert welsh_transforms.format_possessive(
            "Alice Aardvark") == "Alice Aardvark"
        assert (welsh_transforms.format_possessive("Dave Dixon Davies") ==
                "Dave Dixon Davies")
        assert (welsh_transforms.format_possessive("Alice Aardvark's") ==
                "Alice Aardvark's")
        assert (welsh_transforms.format_possessive("Alice Aardvark’s") ==
                "Alice Aardvark’s")

    def test_calculate_difference(self):
        assert (PlaceholderTransforms.calculate_date_difference(
            "2016-06-10", "2019-06-10") == "3 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2018-06-10", "2019-06-10") == "1 year")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2010-01-01", "2018-12-31") == "8 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2011-01", "2015-04") == "4 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-06-10", "2019-08-11") == "2 months")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-07-10", "2019-08-11") == "1 month")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-07-10", "2019-07-11") == "1 day")
        assert PlaceholderTransforms.calculate_date_difference(
            "now", "now") == "0 days"
        with self.assertRaises(ValueError):
            PlaceholderTransforms.calculate_date_difference("2018", "now")

    def test_concatenate_list(self):
        list_to_concatenate = ["Milk", "Eggs", "Flour", "Water"]

        assert (self.transforms.concatenate_list(list_to_concatenate,
                                                 "") == "MilkEggsFlourWater")
        assert (self.transforms.concatenate_list(
            list_to_concatenate, " ") == "Milk Eggs Flour Water")
        assert (self.transforms.concatenate_list(
            list_to_concatenate, ", ") == "Milk, Eggs, Flour, Water")

    def test_add(self):
        assert self.transforms.add(1, 2) == 3

    def test_format_ordinal_with_determiner(self):
        assert self.transforms.format_ordinal(1, "a_or_an") == "a 1st"
        assert self.transforms.format_ordinal(2, "a_or_an") == "a 2nd"
        assert self.transforms.format_ordinal(3, "a_or_an") == "a 3rd"
        assert self.transforms.format_ordinal(4, "a_or_an") == "a 4th"
        assert self.transforms.format_ordinal(8, "a_or_an") == "an 8th"
        assert self.transforms.format_ordinal(11, "a_or_an") == "an 11th"
        assert self.transforms.format_ordinal(12, "a_or_an") == "a 12th"
        assert self.transforms.format_ordinal(13, "a_or_an") == "a 13th"
        assert self.transforms.format_ordinal(18, "a_or_an") == "an 18th"
        assert self.transforms.format_ordinal(21, "a_or_an") == "a 21st"
        assert self.transforms.format_ordinal(22, "a_or_an") == "a 22nd"
        assert self.transforms.format_ordinal(23, "a_or_an") == "a 23rd"
        assert self.transforms.format_ordinal(111, "a_or_an") == "a 111th"
        assert self.transforms.format_ordinal(112, "a_or_an") == "a 112th"
        assert self.transforms.format_ordinal(113, "a_or_an") == "a 113th"

    def test_format_ordinal_without_determiner(self):
        assert self.transforms.format_ordinal(1) == "1st"
        assert self.transforms.format_ordinal(2) == "2nd"
        assert self.transforms.format_ordinal(3) == "3rd"
        assert self.transforms.format_ordinal(4) == "4th"
        assert self.transforms.format_ordinal(21) == "21st"

    @staticmethod
    def test_format_ordinal_with_determiner_ulster_scots():
        ulster_scots_transforms = PlaceholderTransforms(language="eo")
        assert ulster_scots_transforms.format_ordinal(1, "a_or_an") == "a 1st"
        assert ulster_scots_transforms.format_ordinal(2, "a_or_an") == "a 2nd"
        assert ulster_scots_transforms.format_ordinal(3, "a_or_an") == "a 3rd"
        assert ulster_scots_transforms.format_ordinal(4, "a_or_an") == "a 4th"
        assert ulster_scots_transforms.format_ordinal(8, "a_or_an") == "an 8th"
        assert ulster_scots_transforms.format_ordinal(11,
                                                      "a_or_an") == "an 11th"

    @staticmethod
    def test_format_ordinal_without_determiner_ulster_scots():
        ulster_scots_transforms = PlaceholderTransforms(language="eo")
        assert ulster_scots_transforms.format_ordinal(1) == "1st"
        assert ulster_scots_transforms.format_ordinal(2) == "2nd"
        assert ulster_scots_transforms.format_ordinal(3) == "3rd"
        assert ulster_scots_transforms.format_ordinal(4) == "4th"
        assert ulster_scots_transforms.format_ordinal(21) == "21st"

    @staticmethod
    def test_format_ordinal_gaelic():
        gaelic_transforms = PlaceholderTransforms(language="ga")
        assert gaelic_transforms.format_ordinal(1) == "1ú"
        assert gaelic_transforms.format_ordinal(2) == "2ú"
        assert gaelic_transforms.format_ordinal(5) == "5ú"
        assert gaelic_transforms.format_ordinal(7) == "7ú"
        assert gaelic_transforms.format_ordinal(21) == "21ú"

    @staticmethod
    def test_format_ordinal_welsh():
        welsh_transforms = PlaceholderTransforms(language="cy")
        assert welsh_transforms.format_ordinal(1) == "1af"
        assert welsh_transforms.format_ordinal(2) == "2il"
        assert welsh_transforms.format_ordinal(3) == "3ydd"
        assert welsh_transforms.format_ordinal(7) == "7fed"
        assert welsh_transforms.format_ordinal(13) == "13eg"
        assert welsh_transforms.format_ordinal(18) == "18fed"
        assert welsh_transforms.format_ordinal(21) == "21ain"
        assert welsh_transforms.format_ordinal(40) == "40ain"

    def test_remove_empty_from_list(self):
        list_to_filter = [None, 0, False, "", "String"]

        assert self.transforms.remove_empty_from_list(list_to_filter) == [
            0,
            False,
            "String",
        ]

    def test_first_non_empty_item(self):
        list_to_filter = [None, 0, False, "", "String"]

        assert self.transforms.first_non_empty_item(list_to_filter) == 0

    def test_first_non_empty_item_no_valid(self):
        list_to_filter = [None, None]

        assert self.transforms.first_non_empty_item(list_to_filter) == ""

    def test_contains(self):
        list_to_check = ["abc123", "fgh789"]

        assert self.transforms.contains(list_to_check, "abc123")
        assert not self.transforms.contains(list_to_check, "def456")

    def test_list_has_items(self):
        assert self.transforms.list_has_items(["abc123", "fgh789"])
        assert not self.transforms.list_has_items([])

    def test_format_name(self):
        assert self.transforms.format_name("Joe", None,
                                           "Bloggs") == "Joe Bloggs"
        assert (self.transforms.format_name(
            "Joe", None, "Bloggs", include_middle_names=True) == "Joe Bloggs")
        assert self.transforms.format_name("Joe", "Michael",
                                           "Bloggs") == "Joe Bloggs"
        assert (self.transforms.format_name(
            "Joe", "Michael", "Bloggs",
            include_middle_names=True) == "Joe Michael Bloggs")

    def test_email_link(self):

        assert (self.transforms.email_link("*****@*****.**") ==
                '<a href="mailto:[email protected]">[email protected]</a>')

    def test_email_link_with_subject(self):

        assert (
            self.transforms.email_link("*****@*****.**", "test subject") ==
            '<a href="mailto:[email protected]?subject=test%20subject">[email protected]</a>'
        )

    def test_email_link_with_subject_and_reference(self):
        assert (
            self.transforms.email_link("*****@*****.**", "test subject",
                                       "12345") ==
            '<a href="mailto:[email protected]?subject=test%20subject%2012345">[email protected]</a>'
        )

    def test_telephone_number_link(self):

        assert (self.transforms.telephone_number_link("012345 67890") ==
                '<a href="tel:01234567890">012345 67890</a>')
 def _transform(language="en"):
     return PlaceholderTransforms(
         language=language, schema=mock_schema, renderer=mock_renderer
     )
def placeholder_transform(mock_schema, mock_renderer):
    return PlaceholderTransforms(
        language="en", schema=mock_schema, renderer=mock_renderer
    )
class TestPlaceholderParser(unittest.TestCase):
    def setUp(self):
        self.transforms = PlaceholderTransforms(language="en")

    def test_format_currency(self):
        assert self.transforms.format_currency("11", "GBP") == "£11.00"
        assert self.transforms.format_currency("11.99", "GBP") == "£11.99"
        assert self.transforms.format_currency("11000",
                                               "USD") == "US$11,000.00"
        assert self.transforms.format_currency(0) == "£0.00"
        assert self.transforms.format_currency(0.00) == "£0.00"

    def test_format_number(self):
        assert self.transforms.format_number(123) == "123"
        assert self.transforms.format_number("123.4") == "123.4"
        assert self.transforms.format_number("123.40") == "123.4"
        assert self.transforms.format_number("1000") == "1,000"
        assert self.transforms.format_number("10000") == "10,000"
        assert self.transforms.format_number("100000000") == "100,000,000"
        assert self.transforms.format_number(0) == "0"
        assert self.transforms.format_number(0.00) == "0"
        assert self.transforms.format_number("") == ""
        assert self.transforms.format_number(None) == ""

    def test_format_list(self):
        names = ["Alice Aardvark", "Bob Berty Brown", "Dave Dixon Davies"]

        format_value = self.transforms.format_list(names)

        expected_result = ("<ul>"
                           "<li>Alice Aardvark</li>"
                           "<li>Bob Berty Brown</li>"
                           "<li>Dave Dixon Davies</li>"
                           "</ul>")

        assert expected_result == format_value

    def test_format_possessive(self):
        assert self.transforms.format_possessive(
            "Alice Aardvark") == "Alice Aardvark’s"
        assert (self.transforms.format_possessive("Dave Dixon Davies") ==
                "Dave Dixon Davies’")
        assert (self.transforms.format_possessive("Alice Aardvark's") ==
                "Alice Aardvark’s")
        assert (self.transforms.format_possessive("Alice Aardvark’s") ==
                "Alice Aardvark’s")

    @staticmethod
    def test_format_possessive_non_english_does_nothing():
        welsh_transforms = PlaceholderTransforms(language="cy")
        assert welsh_transforms.format_possessive(
            "Alice Aardvark") == "Alice Aardvark"
        assert (welsh_transforms.format_possessive("Dave Dixon Davies") ==
                "Dave Dixon Davies")
        assert (welsh_transforms.format_possessive("Alice Aardvark's") ==
                "Alice Aardvark's")
        assert (welsh_transforms.format_possessive("Alice Aardvark’s") ==
                "Alice Aardvark’s")

    def test_calculate_difference(self):
        assert (PlaceholderTransforms.calculate_date_difference(
            "2016-06-10", "2019-06-10") == "3 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2018-06-10", "2019-06-10") == "1 year")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2010-01-01", "2018-12-31") == "8 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2011-01", "2015-04") == "4 years")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-06-10", "2019-08-11") == "2 months")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-07-10", "2019-08-11") == "1 month")
        assert (PlaceholderTransforms.calculate_date_difference(
            "2019-07-10", "2019-07-11") == "1 day")
        assert PlaceholderTransforms.calculate_date_difference(
            "now", "now") == "0 days"
        with self.assertRaises(ValueError):
            PlaceholderTransforms.calculate_date_difference("2018", "now")

    def test_concatenate_list(self):
        list_to_concatenate = ["Milk", "Eggs", "Flour", "Water"]

        assert (self.transforms.concatenate_list(list_to_concatenate,
                                                 "") == "MilkEggsFlourWater")
        assert (self.transforms.concatenate_list(
            list_to_concatenate, " ") == "Milk Eggs Flour Water")
        assert (self.transforms.concatenate_list(
            list_to_concatenate, ", ") == "Milk, Eggs, Flour, Water")

    def test_add(self):
        assert self.transforms.add(1, 2) == 3
        assert self.transforms.add("1", 2) == 3
        assert self.transforms.add("1", "2") == 3

    def test_format_ordinal_with_determiner(self):
        assert self.transforms.format_ordinal(1, "a_or_an") == "a 1st"
        assert self.transforms.format_ordinal(2, "a_or_an") == "a 2nd"
        assert self.transforms.format_ordinal(3, "a_or_an") == "a 3rd"
        assert self.transforms.format_ordinal(4, "a_or_an") == "a 4th"
        assert self.transforms.format_ordinal(8, "a_or_an") == "an 8th"
        assert self.transforms.format_ordinal(11, "a_or_an") == "an 11th"
        assert self.transforms.format_ordinal(12, "a_or_an") == "a 12th"
        assert self.transforms.format_ordinal(13, "a_or_an") == "a 13th"
        assert self.transforms.format_ordinal(18, "a_or_an") == "an 18th"
        assert self.transforms.format_ordinal(21, "a_or_an") == "a 21st"
        assert self.transforms.format_ordinal(22, "a_or_an") == "a 22nd"
        assert self.transforms.format_ordinal(23, "a_or_an") == "a 23rd"
        assert self.transforms.format_ordinal(111, "a_or_an") == "a 111th"
        assert self.transforms.format_ordinal(112, "a_or_an") == "a 112th"
        assert self.transforms.format_ordinal(113, "a_or_an") == "a 113th"

    def test_format_ordinal_without_determiner(self):
        assert self.transforms.format_ordinal(1) == "1st"
        assert self.transforms.format_ordinal(2) == "2nd"
        assert self.transforms.format_ordinal(3) == "3rd"
        assert self.transforms.format_ordinal(4) == "4th"
        assert self.transforms.format_ordinal(21) == "21st"

    def test_remove_empty_from_list(self):
        list_to_filter = [None, 0, False, "", "String"]

        assert self.transforms.remove_empty_from_list(list_to_filter) == [
            0,
            False,
            "String",
        ]

    def test_first_non_empty_item(self):
        list_to_filter = [None, 0, False, "", "String"]

        assert self.transforms.first_non_empty_item(list_to_filter) == 0

    def test_first_non_empty_item_no_valid(self):
        list_to_filter = [None, None]

        assert self.transforms.first_non_empty_item(list_to_filter) == ""