|
| 1 | +from xml.etree import ElementTree |
| 2 | +from xml.etree.ElementTree import ParseError |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pytest_bdd import scenario, given, when, then |
| 6 | + |
| 7 | +from ted_sws.core.model.metadata import LanguageTaggedString |
| 8 | +from ted_sws.core.model.notice import Notice |
| 9 | +from ted_sws.notice_metadata_processor.adapters.notice_metadata_normaliser import get_html_compatible_string |
| 10 | +from ted_sws.notice_metadata_processor.services.metadata_normalizer import normalise_notice |
| 11 | + |
| 12 | + |
| 13 | +def html_str(content: str) -> str: |
| 14 | + return f"""<?xml version="1.0" encoding="UTF-8"?> <body>{content}</body>""" |
| 15 | + |
| 16 | + |
| 17 | +@scenario('notice_normalisation.feature', 'Normalising notice with spaces in notice ID') |
| 18 | +def test_normalising_notice_with_spaces(): |
| 19 | + """Test normalisation of notices with spaces in ID.""" |
| 20 | + |
| 21 | + |
| 22 | +@scenario('notice_normalisation.feature', 'Processing HTML incompatible string') |
| 23 | +def test_processing_html_incompatible_string(): |
| 24 | + """Test processing of HTML incompatible strings.""" |
| 25 | + |
| 26 | + |
| 27 | +@scenario('notice_normalisation.feature', 'Normalising notice with HTML incompatible title') |
| 28 | +def test_normalising_notice_with_html_incompatible_title(): |
| 29 | + """Test normalisation of notices with HTML incompatible titles.""" |
| 30 | + |
| 31 | + |
| 32 | +# Shared fixtures and steps |
| 33 | +@given('an EF notice with spaces in notice ID') |
| 34 | +def an_ef_notice_with_spaces_in_notice_id(sample_indexed_ef_html_unsafe_notice: Notice) -> Notice: |
| 35 | + """Provide EF notice fixture.""" |
| 36 | + return sample_indexed_ef_html_unsafe_notice |
| 37 | + |
| 38 | + |
| 39 | +@given('an SF notice with spaces in notice ID') |
| 40 | +def an_sf_notice_with_spaces_in_notice_id(sample_indexed_sf_html_unsafe_notice: Notice) -> Notice: |
| 41 | + """Provide SF notice fixture.""" |
| 42 | + return sample_indexed_sf_html_unsafe_notice |
| 43 | + |
| 44 | + |
| 45 | +@when('the EF notice is normalised', target_fixture='normalised_ef_notice') |
| 46 | +def the_ef_notice_is_normalised(sample_indexed_ef_html_unsafe_notice: Notice) -> Notice: |
| 47 | + """Normalize EF notice.""" |
| 48 | + return normalise_notice(sample_indexed_ef_html_unsafe_notice) |
| 49 | + |
| 50 | + |
| 51 | +@when('the SF notice is normalised', target_fixture='normalised_sf_notice') |
| 52 | +def the_sf_notice_is_normalised(sample_indexed_sf_html_unsafe_notice: Notice) -> Notice: |
| 53 | + """Normalize SF notice.""" |
| 54 | + return normalise_notice(sample_indexed_sf_html_unsafe_notice) |
| 55 | + |
| 56 | + |
| 57 | +@then('the EF notice ID should not contain leading or trailing spaces') |
| 58 | +def the_ef_notice_id_should_not_contain_leading_or_trailing_spaces(normalised_ef_notice: Notice): |
| 59 | + """Verify EF notice ID has no extra spaces.""" |
| 60 | + assert normalised_ef_notice.normalised_metadata.notice_publication_number.strip() == \ |
| 61 | + normalised_ef_notice.normalised_metadata.notice_publication_number |
| 62 | + |
| 63 | + |
| 64 | +@then('the SF notice ID should not contain leading or trailing spaces') |
| 65 | +def the_sf_notice_id_should_not_contain_leading_or_trailing_spaces(normalised_sf_notice: Notice): |
| 66 | + """Verify SF notice ID has no extra spaces.""" |
| 67 | + assert normalised_sf_notice.normalised_metadata.notice_publication_number.strip() == \ |
| 68 | + normalised_sf_notice.normalised_metadata.notice_publication_number |
| 69 | + |
| 70 | + |
| 71 | +# HTML incompatible string scenario steps |
| 72 | +@given('an HTML incompatible string', target_fixture='incompatible_string') |
| 73 | +def an_html_incompatible_string(html_incompatible_str: str) -> str: |
| 74 | + """Provide HTML incompatible string fixture.""" |
| 75 | + return html_incompatible_str |
| 76 | + |
| 77 | + |
| 78 | +@when('the string cannot be parsed as XML') |
| 79 | +def the_string_cannot_be_parsed_as_xml(html_incompatible_str: str): |
| 80 | + """Verify string cannot be parsed as XML.""" |
| 81 | + with pytest.raises(ParseError): |
| 82 | + ElementTree.fromstring(html_incompatible_str) |
| 83 | + |
| 84 | + |
| 85 | +@when('the string is converted to HTML compatible format', target_fixture='compatible_string') |
| 86 | +def the_string_is_converted_to_html_compatible_format(html_incompatible_str: str) -> LanguageTaggedString: |
| 87 | + """Convert string to HTML compatible format.""" |
| 88 | + return get_html_compatible_string(LanguageTaggedString(text=html_incompatible_str)) |
| 89 | + |
| 90 | + |
| 91 | +@then('the resulting string should be well-formed XML') |
| 92 | +def the_resulting_string_should_be_well_formed_xml(compatible_string: LanguageTaggedString): |
| 93 | + """Verify string is well-formed XML.""" |
| 94 | + ElementTree.fromstring(html_str(compatible_string.text)) |
| 95 | + |
| 96 | + |
| 97 | +# HTML incompatible title scenario steps |
| 98 | +@given('an EF notice with HTML incompatible title') |
| 99 | +def an_ef_notice_with_html_incompatible_title(sample_indexed_ef_html_unsafe_notice: Notice) -> Notice: |
| 100 | + """Provide EF notice with incompatible title fixture.""" |
| 101 | + return sample_indexed_ef_html_unsafe_notice |
| 102 | + |
| 103 | + |
| 104 | +@given('an SF notice with HTML incompatible title') |
| 105 | +def an_sf_notice_with_html_incompatible_title(sample_indexed_sf_html_unsafe_notice: Notice) -> Notice: |
| 106 | + """Provide SF notice with incompatible title fixture.""" |
| 107 | + return sample_indexed_sf_html_unsafe_notice |
| 108 | + |
| 109 | + |
| 110 | +@then('all EF notice titles should be valid XML') |
| 111 | +def all_ef_notice_titles_should_be_valid_xml(normalised_ef_notice: Notice): |
| 112 | + """Verify all EF notice titles are valid XML.""" |
| 113 | + [ElementTree.fromstring(html_str(title.text)) for title in normalised_ef_notice.normalised_metadata.title] |
| 114 | + |
| 115 | + |
| 116 | +@then('all SF notice titles should be valid XML') |
| 117 | +def all_sf_notice_titles_should_be_valid_xml(normalised_sf_notice: Notice): |
| 118 | + """Verify all SF notice titles are valid XML.""" |
| 119 | + [ElementTree.fromstring(html_str(title.text)) for title in normalised_sf_notice.normalised_metadata.title] |
0 commit comments