2 Test for legacy tokenizer.
8 from nominatim.indexer.place_info import PlaceInfo
9 from nominatim.tokenizer import legacy_tokenizer
10 from nominatim.db import properties
11 from nominatim.errors import UsageError
14 def test_config(project_env, tmp_path):
15 module_dir = tmp_path / 'module_src'
17 (module_dir / 'nominatim.so').write_text('TEST nomiantim.so')
19 project_env.lib_dir.module = module_dir
21 sqldir = tmp_path / 'sql'
23 (sqldir / 'tokenizer').mkdir()
24 (sqldir / 'tokenizer' / 'legacy_tokenizer.sql').write_text("SELECT 'a'")
25 (sqldir / 'words.sql').write_text("SELECT 'a'")
26 shutil.copy(str(project_env.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer_tables.sql'),
27 str(sqldir / 'tokenizer' / 'legacy_tokenizer_tables.sql'))
29 project_env.lib_dir.sql = sqldir
30 project_env.lib_dir.data = sqldir
36 def tokenizer_factory(dsn, tmp_path, property_table):
37 (tmp_path / 'tokenizer').mkdir()
40 return legacy_tokenizer.create(dsn, tmp_path / 'tokenizer')
46 def tokenizer_setup(tokenizer_factory, test_config, monkeypatch, sql_preprocessor):
47 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
48 tok = tokenizer_factory()
49 tok.init_new_db(test_config)
53 def analyzer(tokenizer_factory, test_config, monkeypatch, sql_preprocessor,
54 word_table, temp_db_with_extensions, tmp_path):
55 sql = tmp_path / 'sql' / 'tokenizer' / 'legacy_tokenizer.sql'
57 CREATE OR REPLACE FUNCTION getorcreate_housenumber_id(lookup_word TEXT)
58 RETURNS INTEGER AS $$ SELECT 342; $$ LANGUAGE SQL;
61 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
62 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', ':: lower();')
63 tok = tokenizer_factory()
64 tok.init_new_db(test_config)
67 with tok.name_analyzer() as analyzer:
72 def make_standard_name(temp_db_cursor):
73 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
74 RETURNS TEXT AS $$ SELECT '#' || lower(name) || '#'; $$ LANGUAGE SQL""")
78 def create_postcode_id(temp_db_cursor):
79 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION create_postcode_id(postcode TEXT)
81 INSERT INTO word (word_token, word, class, type)
82 VALUES (' ' || postcode, postcode, 'place', 'postcode')
88 def make_keywords(temp_db_cursor, temp_db_with_extensions):
89 temp_db_cursor.execute(
90 """CREATE OR REPLACE FUNCTION make_keywords(names HSTORE)
91 RETURNS INTEGER[] AS $$ SELECT ARRAY[1, 2, 3] $$ LANGUAGE SQL""")
93 def test_init_new(tokenizer_factory, test_config, monkeypatch,
94 temp_db_conn, sql_preprocessor):
95 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', 'xxvv')
96 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
98 tok = tokenizer_factory()
99 tok.init_new_db(test_config)
101 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) == 'xxvv'
103 outfile = test_config.project_dir / 'module' / 'nominatim.so'
105 assert outfile.exists()
106 assert outfile.read_text() == 'TEST nomiantim.so'
107 assert outfile.stat().st_mode == 33261
110 def test_init_module_load_failed(tokenizer_factory, test_config):
111 tok = tokenizer_factory()
113 with pytest.raises(UsageError):
114 tok.init_new_db(test_config)
117 def test_init_module_custom(tokenizer_factory, test_config,
118 monkeypatch, tmp_path, sql_preprocessor):
119 module_dir = (tmp_path / 'custom').resolve()
121 (module_dir/ 'nominatim.so').write_text('CUSTOM nomiantim.so')
123 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', str(module_dir))
124 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
126 tok = tokenizer_factory()
127 tok.init_new_db(test_config)
129 assert not (test_config.project_dir / 'module').exists()
132 def test_init_from_project(tokenizer_setup, tokenizer_factory, test_config):
133 tok = tokenizer_factory()
135 tok.init_from_project(test_config)
137 assert tok.normalization is not None
140 def test_update_sql_functions(sql_preprocessor, temp_db_conn,
141 tokenizer_factory, test_config, table_factory,
142 monkeypatch, temp_db_cursor):
143 monkeypatch.setenv('NOMINATIM_MAX_WORD_FREQUENCY', '1133')
144 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
145 tok = tokenizer_factory()
146 tok.init_new_db(test_config)
149 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) == '1133'
151 table_factory('test', 'txt TEXT')
153 func_file = test_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer.sql'
154 func_file.write_text("""INSERT INTO test VALUES ('{{max_word_freq}}'),
155 ('{{modulepath}}')""")
157 tok.update_sql_functions(test_config)
159 test_content = temp_db_cursor.row_set('SELECT * FROM test')
160 assert test_content == set((('1133', ), (str(test_config.project_dir / 'module'), )))
163 def test_migrate_database(tokenizer_factory, test_config, temp_db_conn, monkeypatch):
164 monkeypatch.setattr(legacy_tokenizer, '_check_module', lambda m, c: None)
165 tok = tokenizer_factory()
166 tok.migrate_database(test_config)
168 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) is not None
169 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) is not None
171 outfile = test_config.project_dir / 'module' / 'nominatim.so'
173 assert outfile.exists()
174 assert outfile.read_text() == 'TEST nomiantim.so'
175 assert outfile.stat().st_mode == 33261
178 def test_normalize(analyzer):
179 assert analyzer.normalize('TEsT') == 'test'
182 def test_update_postcodes_from_db_empty(analyzer, table_factory, word_table,
184 table_factory('location_postcode', 'postcode TEXT',
185 content=(('1234',), ('12 34',), ('AB23',), ('1234',)))
187 analyzer.update_postcodes_from_db()
189 assert word_table.count() == 3
190 assert word_table.get_postcodes() == {'1234', '12 34', 'AB23'}
193 def test_update_postcodes_from_db_add_and_remove(analyzer, table_factory, word_table,
195 table_factory('location_postcode', 'postcode TEXT',
196 content=(('1234',), ('45BC', ), ('XX45', )))
197 word_table.add_postcode(' 1234', '1234')
198 word_table.add_postcode(' 5678', '5678')
200 analyzer.update_postcodes_from_db()
202 assert word_table.count() == 3
203 assert word_table.get_postcodes() == {'1234', '45BC', 'XX45'}
206 def test_update_special_phrase_empty_table(analyzer, word_table, make_standard_name):
207 analyzer.update_special_phrases([
208 ("König bei", "amenity", "royal", "near"),
209 ("Könige", "amenity", "royal", "-"),
210 ("könige", "amenity", "royal", "-"),
211 ("strasse", "highway", "primary", "in")
214 assert word_table.get_special() \
215 == set(((' #könig bei#', 'könig bei', 'amenity', 'royal', 'near'),
216 (' #könige#', 'könige', 'amenity', 'royal', None),
217 (' #strasse#', 'strasse', 'highway', 'primary', 'in')))
220 def test_update_special_phrase_delete_all(analyzer, word_table, make_standard_name):
221 word_table.add_special(' #foo#', 'foo', 'amenity', 'prison', 'in')
222 word_table.add_special(' #bar#', 'bar', 'highway', 'road', None)
224 assert word_table.count_special() == 2
226 analyzer.update_special_phrases([], True)
228 assert word_table.count_special() == 0
231 def test_update_special_phrases_no_replace(analyzer, word_table, make_standard_name):
232 word_table.add_special(' #foo#', 'foo', 'amenity', 'prison', 'in')
233 word_table.add_special(' #bar#', 'bar', 'highway', 'road', None)
235 assert word_table.count_special() == 2
237 analyzer.update_special_phrases([], False)
239 assert word_table.count_special() == 2
242 def test_update_special_phrase_modify(analyzer, word_table, make_standard_name):
243 word_table.add_special(' #foo#', 'foo', 'amenity', 'prison', 'in')
244 word_table.add_special(' #bar#', 'bar', 'highway', 'road', None)
246 assert word_table.count_special() == 2
248 analyzer.update_special_phrases([
249 ('prison', 'amenity', 'prison', 'in'),
250 ('bar', 'highway', 'road', '-'),
251 ('garden', 'leisure', 'garden', 'near')
254 assert word_table.get_special() \
255 == set(((' #prison#', 'prison', 'amenity', 'prison', 'in'),
256 (' #bar#', 'bar', 'highway', 'road', None),
257 (' #garden#', 'garden', 'leisure', 'garden', 'near')))
260 def test_add_country_names(analyzer, word_table, make_standard_name):
261 analyzer.add_country_names('de', {'name': 'Germany',
262 'name:de': 'Deutschland',
263 'short_name': 'germany'})
265 assert word_table.get_country() \
266 == {('de', ' #germany#'),
267 ('de', ' #deutschland#')}
270 def test_add_more_country_names(analyzer, word_table, make_standard_name):
271 word_table.add_country('fr', ' #france#')
272 word_table.add_country('it', ' #italy#')
273 word_table.add_country('it', ' #itala#')
275 analyzer.add_country_names('it', {'name': 'Italy', 'ref': 'IT'})
277 assert word_table.get_country() \
278 == {('fr', ' #france#'),
284 def test_process_place_names(analyzer, make_keywords):
285 info = analyzer.process_place(PlaceInfo({'name' : {'name' : 'Soft bAr', 'ref': '34'}}))
287 assert info['names'] == '{1,2,3}'
290 @pytest.mark.parametrize('pcode', ['12345', 'AB 123', '34-345'])
291 def test_process_place_postcode(analyzer, create_postcode_id, word_table, pcode):
292 analyzer.process_place(PlaceInfo({'address': {'postcode' : pcode}}))
294 assert word_table.get_postcodes() == {pcode, }
297 @pytest.mark.parametrize('pcode', ['12:23', 'ab;cd;f', '123;836'])
298 def test_process_place_bad_postcode(analyzer, create_postcode_id, word_table, pcode):
299 analyzer.process_place(PlaceInfo({'address': {'postcode' : pcode}}))
301 assert not word_table.get_postcodes()
304 class TestHousenumberName:
307 @pytest.fixture(autouse=True)
308 def setup_create_housenumbers(temp_db_cursor):
309 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION create_housenumbers(
311 OUT tokens TEXT, OUT normtext TEXT)
313 SELECT housenumbers::TEXT, array_to_string(housenumbers, ';')
318 @pytest.mark.parametrize('hnr', ['123a', '1', '101'])
319 def test_process_place_housenumbers_simple(analyzer, hnr):
320 info = analyzer.process_place(PlaceInfo({'address': {'housenumber' : hnr}}))
322 assert info['hnr'] == hnr
323 assert info['hnr_tokens'].startswith("{")
327 def test_process_place_housenumbers_lists(analyzer):
328 info = analyzer.process_place(PlaceInfo({'address': {'conscriptionnumber' : '1; 2;3'}}))
330 assert set(info['hnr'].split(';')) == set(('1', '2', '3'))
334 def test_process_place_housenumbers_duplicates(analyzer):
335 info = analyzer.process_place(PlaceInfo({'address': {'housenumber' : '134',
336 'conscriptionnumber' : '134',
337 'streetnumber' : '99a'}}))
339 assert set(info['hnr'].split(';')) == set(('134', '99a'))