|
| 1 | +from ftmq.util import make_entity |
| 2 | + |
1 | 3 | from openaleph_search.index.entities import index_bulk |
2 | 4 | from openaleph_search.parse.parser import SearchQueryParser |
3 | 5 | from openaleph_search.query.queries import EntitiesQuery |
@@ -78,3 +80,92 @@ def test_highlighting_pages(fixture_pages, cleanup_after): |
78 | 80 | parent_id="f61295777cf69f423855655f1614794ce22086d8.b154e50f50c8c8133168767d78bbd1dff067f308", |
79 | 81 | ) |
80 | 82 | assert "<em>MIT license</em>" in highlight |
| 83 | + |
| 84 | + |
| 85 | +def test_highlighting_translation_plaintext(cleanup_after): |
| 86 | + """Test that translation highlights are returned under the 'translation' key, |
| 87 | + separate from 'content' highlights.""" |
| 88 | + entity = make_entity( |
| 89 | + { |
| 90 | + "id": "plaintext-highlight-translation", |
| 91 | + "schema": "PlainText", |
| 92 | + "properties": { |
| 93 | + "fileName": ["report.txt"], |
| 94 | + "bodyText": ["Original text about financial regulations"], |
| 95 | + "translatedText": ["Übersetzter Text über Finanzvorschriften"], |
| 96 | + }, |
| 97 | + } |
| 98 | + ) |
| 99 | + |
| 100 | + index_bulk("test_highlight_translation", [entity], sync=True) |
| 101 | + |
| 102 | + args = [ |
| 103 | + ("q", "Finanzvorschriften"), |
| 104 | + ("highlight", "true"), |
| 105 | + ("filter:dataset", "test_highlight_translation"), |
| 106 | + ] |
| 107 | + query = EntitiesQuery(SearchQueryParser(args, None)) |
| 108 | + result = query.search() |
| 109 | + |
| 110 | + assert result["hits"]["total"]["value"] == 1 |
| 111 | + hit = result["hits"]["hits"][0] |
| 112 | + assert "highlight" in hit |
| 113 | + # Translation highlights should be under the 'translation' key |
| 114 | + assert "translation" in hit["highlight"] |
| 115 | + assert any( |
| 116 | + "<em>Finanzvorschriften</em>" in fragment |
| 117 | + for fragment in hit["highlight"]["translation"] |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +def test_highlighting_translation_pages(cleanup_after): |
| 122 | + """Test that Pages translation highlights are returned under 'translation' key, |
| 123 | + while original content highlights come under 'content'.""" |
| 124 | + entity = make_entity( |
| 125 | + { |
| 126 | + "id": "pages-highlight-translation", |
| 127 | + "schema": "Pages", |
| 128 | + "properties": { |
| 129 | + "fileName": ["bericht.pdf"], |
| 130 | + "indexText": [ |
| 131 | + "Original German text about environmental policies", |
| 132 | + "__translation__ Translated text about Umweltpolitik", |
| 133 | + ], |
| 134 | + }, |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + index_bulk("test_highlight_pages_translation", [entity], sync=True) |
| 139 | + |
| 140 | + # Search for a term in the translation |
| 141 | + args = [ |
| 142 | + ("q", "Umweltpolitik"), |
| 143 | + ("highlight", "true"), |
| 144 | + ("filter:dataset", "test_highlight_pages_translation"), |
| 145 | + ] |
| 146 | + query = EntitiesQuery(SearchQueryParser(args, None)) |
| 147 | + result = query.search() |
| 148 | + |
| 149 | + assert result["hits"]["total"]["value"] == 1 |
| 150 | + hit = result["hits"]["hits"][0] |
| 151 | + assert "highlight" in hit |
| 152 | + assert "translation" in hit["highlight"] |
| 153 | + assert any( |
| 154 | + "<em>Umweltpolitik</em>" in fragment |
| 155 | + for fragment in hit["highlight"]["translation"] |
| 156 | + ) |
| 157 | + |
| 158 | + # Search for a term in the original content — should highlight under 'content' |
| 159 | + args = [ |
| 160 | + ("q", "environmental policies"), |
| 161 | + ("highlight", "true"), |
| 162 | + ("filter:dataset", "test_highlight_pages_translation"), |
| 163 | + ] |
| 164 | + query = EntitiesQuery(SearchQueryParser(args, None)) |
| 165 | + result = query.search() |
| 166 | + |
| 167 | + assert result["hits"]["total"]["value"] == 1 |
| 168 | + hit = result["hits"]["hits"][0] |
| 169 | + assert "highlight" in hit |
| 170 | + assert "content" in hit["highlight"] |
| 171 | + assert "translation" not in hit["highlight"] |
0 commit comments