2 Test for legacy tokenizer.
8 from nominatim.tokenizer import legacy_tokenizer
9 from nominatim.db import properties
10 from nominatim.errors import UsageError
13 def test_config(def_config, tmp_path):
14 def_config.project_dir = tmp_path / 'project'
15 def_config.project_dir.mkdir()
17 module_dir = tmp_path / 'module_src'
19 (module_dir / 'nominatim.so').write_text('TEST nomiantim.so')
21 def_config.lib_dir.module = module_dir
23 sqldir = tmp_path / 'sql'
25 (sqldir / 'tokenizer').mkdir()
26 (sqldir / 'tokenizer' / 'legacy_tokenizer.sql').write_text("SELECT 'a'")
27 (sqldir / 'words.sql').write_text("SELECT 'a'")
28 shutil.copy(str(def_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer_tables.sql'),
29 str(sqldir / 'tokenizer' / 'legacy_tokenizer_tables.sql'))
31 def_config.lib_dir.sql = sqldir
32 def_config.lib_dir.data = sqldir
38 def tokenizer_factory(dsn, tmp_path, monkeypatch, property_table):
39 (tmp_path / 'tokenizer').mkdir()
42 return legacy_tokenizer.create(dsn, tmp_path / 'tokenizer')
47 def tokenizer_setup(tokenizer_factory, test_config, monkeypatch, sql_preprocessor):
48 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
49 tok = tokenizer_factory()
50 tok.init_new_db(test_config)
54 def analyzer(tokenizer_factory, test_config, monkeypatch, sql_preprocessor,
55 word_table, temp_db_with_extensions, tmp_path):
56 sql = tmp_path / 'sql' / 'tokenizer' / 'legacy_tokenizer.sql'
58 CREATE OR REPLACE FUNCTION getorcreate_housenumber_id(lookup_word TEXT)
59 RETURNS INTEGER AS $$ SELECT 342; $$ LANGUAGE SQL;
62 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
63 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', ':: lower();')
64 tok = tokenizer_factory()
65 tok.init_new_db(test_config)
68 with tok.name_analyzer() as analyzer:
73 def make_standard_name(temp_db_cursor):
74 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION make_standard_name(name TEXT)
75 RETURNS TEXT AS $$ SELECT ' ' || name; $$ LANGUAGE SQL""")
79 def create_postcode_id(table_factory, temp_db_cursor):
80 table_factory('out_postcode_table', 'postcode TEXT')
82 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION create_postcode_id(postcode TEXT)
84 INSERT INTO out_postcode_table VALUES (postcode) RETURNING True;
89 def create_housenumbers(temp_db_cursor):
90 temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION create_housenumbers(
92 OUT tokens TEXT, OUT normtext TEXT)
94 SELECT housenumbers::TEXT, array_to_string(housenumbers, ';')
99 def make_keywords(temp_db_cursor, temp_db_with_extensions):
100 temp_db_cursor.execute(
101 """CREATE OR REPLACE FUNCTION make_keywords(names HSTORE)
102 RETURNS INTEGER[] AS $$ SELECT ARRAY[1, 2, 3] $$ LANGUAGE SQL""")
104 def test_init_new(tokenizer_factory, test_config, monkeypatch,
105 temp_db_conn, sql_preprocessor):
106 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', 'xxvv')
107 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
109 tok = tokenizer_factory()
110 tok.init_new_db(test_config)
112 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) == 'xxvv'
114 outfile = test_config.project_dir / 'module' / 'nominatim.so'
116 assert outfile.exists()
117 assert outfile.read_text() == 'TEST nomiantim.so'
118 assert outfile.stat().st_mode == 33261
121 def test_init_module_load_failed(tokenizer_factory, test_config,
122 monkeypatch, temp_db_conn):
123 tok = tokenizer_factory()
125 with pytest.raises(UsageError):
126 tok.init_new_db(test_config)
129 def test_init_module_custom(tokenizer_factory, test_config,
130 monkeypatch, tmp_path, sql_preprocessor):
131 module_dir = (tmp_path / 'custom').resolve()
133 (module_dir/ 'nominatim.so').write_text('CUSTOM nomiantim.so')
135 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', str(module_dir))
136 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
138 tok = tokenizer_factory()
139 tok.init_new_db(test_config)
141 assert not (test_config.project_dir / 'module').exists()
144 def test_init_from_project(tokenizer_setup, tokenizer_factory):
145 tok = tokenizer_factory()
147 tok.init_from_project()
149 assert tok.normalization is not None
152 def test_update_sql_functions(sql_preprocessor, temp_db_conn,
153 tokenizer_factory, test_config, table_factory,
154 monkeypatch, temp_db_cursor):
155 monkeypatch.setenv('NOMINATIM_MAX_WORD_FREQUENCY', '1133')
156 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
157 tok = tokenizer_factory()
158 tok.init_new_db(test_config)
161 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) == '1133'
163 table_factory('test', 'txt TEXT')
165 func_file = test_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer.sql'
166 func_file.write_text("""INSERT INTO test VALUES ('{{max_word_freq}}'),
167 ('{{modulepath}}')""")
169 tok.update_sql_functions(test_config)
171 test_content = temp_db_cursor.row_set('SELECT * FROM test')
172 assert test_content == set((('1133', ), (str(test_config.project_dir / 'module'), )))
175 def test_migrate_database(tokenizer_factory, test_config, temp_db_conn, monkeypatch):
176 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
177 tok = tokenizer_factory()
178 tok.migrate_database(test_config)
180 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) is not None
181 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) is not None
183 outfile = test_config.project_dir / 'module' / 'nominatim.so'
185 assert outfile.exists()
186 assert outfile.read_text() == 'TEST nomiantim.so'
187 assert outfile.stat().st_mode == 33261
190 def test_normalize(analyzer):
191 assert analyzer.normalize('TEsT') == 'test'
194 def test_add_postcodes_from_db(analyzer, table_factory, temp_db_cursor,
196 table_factory('location_postcode', 'postcode TEXT',
197 content=(('1234',), ('12 34',), ('AB23',), ('1234',)))
199 analyzer.add_postcodes_from_db()
201 assert temp_db_cursor.row_set("SELECT * from out_postcode_table") \
202 == set((('1234', ), ('12 34', ), ('AB23',)))
205 def test_update_special_phrase_empty_table(analyzer, word_table, temp_db_cursor,
207 analyzer.update_special_phrases([
208 ("König bei", "amenity", "royal", "near"),
209 ("Könige", "amenity", "royal", "-"),
210 ("strasse", "highway", "primary", "in")
213 assert temp_db_cursor.row_set("""SELECT word_token, word, class, type, operator
214 FROM word WHERE class != 'place'""") \
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, temp_db_cursor,
222 temp_db_cursor.execute("""INSERT INTO word (word_token, word, class, type, operator)
223 VALUES (' foo', 'foo', 'amenity', 'prison', 'in'),
224 (' bar', 'bar', 'highway', 'road', null)""")
226 assert 2 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
228 analyzer.update_special_phrases([])
230 assert 0 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
233 def test_update_special_phrase_modify(analyzer, word_table, temp_db_cursor,
235 temp_db_cursor.execute("""INSERT INTO word (word_token, word, class, type, operator)
236 VALUES (' foo', 'foo', 'amenity', 'prison', 'in'),
237 (' bar', 'bar', 'highway', 'road', null)""")
239 assert 2 == temp_db_cursor.scalar("SELECT count(*) FROM word WHERE class != 'place'""")
241 analyzer.update_special_phrases([
242 ('prison', 'amenity', 'prison', 'in'),
243 ('bar', 'highway', 'road', '-'),
244 ('garden', 'leisure', 'garden', 'near')
247 assert temp_db_cursor.row_set("""SELECT word_token, word, class, type, operator
248 FROM word WHERE class != 'place'""") \
249 == set(((' prison', 'prison', 'amenity', 'prison', 'in'),
250 (' bar', 'bar', 'highway', 'road', None),
251 (' garden', 'garden', 'leisure', 'garden', 'near')))
254 def test_process_place_names(analyzer, make_keywords):
256 info = analyzer.process_place({'name' : {'name' : 'Soft bAr', 'ref': '34'}})
258 assert info['names'] == '{1,2,3}'
261 @pytest.mark.parametrize('pc', ['12345', 'AB 123', '34-345'])
262 def test_process_place_postcode(analyzer, temp_db_cursor, create_postcode_id, pc):
264 info = analyzer.process_place({'address': {'postcode' : pc}})
266 assert temp_db_cursor.row_set("SELECT * from out_postcode_table") \
270 @pytest.mark.parametrize('pc', ['12:23', 'ab;cd;f', '123;836'])
271 def test_process_place_bad_postcode(analyzer, temp_db_cursor, create_postcode_id,
274 info = analyzer.process_place({'address': {'postcode' : pc}})
276 assert 0 == temp_db_cursor.scalar("SELECT count(*) from out_postcode_table")
279 @pytest.mark.parametrize('hnr', ['123a', '1', '101'])
280 def test_process_place_housenumbers_simple(analyzer, create_housenumbers, hnr):
281 info = analyzer.process_place({'address': {'housenumber' : hnr}})
283 assert info['hnr'] == hnr
284 assert info['hnr_tokens'].startswith("{")
287 def test_process_place_housenumbers_lists(analyzer, create_housenumbers):
288 info = analyzer.process_place({'address': {'conscriptionnumber' : '1; 2;3'}})
290 assert set(info['hnr'].split(';')) == set(('1', '2', '3'))
293 def test_process_place_housenumbers_duplicates(analyzer, create_housenumbers):
294 info = analyzer.process_place({'address': {'housenumber' : '134',
295 'conscriptionnumber' : '134',
296 'streetnumber' : '99a'}})
298 assert set(info['hnr'].split(';')) == set(('134', '99a'))