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):
41 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:
71 def test_init_new(tokenizer_factory, test_config, monkeypatch,
72 temp_db_conn, sql_preprocessor):
73 monkeypatch.setenv('NOMINATIM_TERM_NORMALIZATION', 'xxvv')
74 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
76 tok = tokenizer_factory()
77 tok.init_new_db(test_config)
79 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) == 'xxvv'
81 outfile = test_config.project_dir / 'module' / 'nominatim.so'
83 assert outfile.exists()
84 assert outfile.read_text() == 'TEST nomiantim.so'
85 assert outfile.stat().st_mode == 33261
88 def test_init_module_load_failed(tokenizer_factory, test_config,
89 monkeypatch, temp_db_conn):
90 tok = tokenizer_factory()
92 with pytest.raises(UsageError):
93 tok.init_new_db(test_config)
96 def test_init_module_custom(tokenizer_factory, test_config,
97 monkeypatch, tmp_path, sql_preprocessor):
98 module_dir = (tmp_path / 'custom').resolve()
100 (module_dir/ 'nominatim.so').write_text('CUSTOM nomiantim.so')
102 monkeypatch.setenv('NOMINATIM_DATABASE_MODULE_PATH', str(module_dir))
103 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
105 tok = tokenizer_factory()
106 tok.init_new_db(test_config)
108 assert not (test_config.project_dir / 'module').exists()
111 def test_init_from_project(tokenizer_setup, tokenizer_factory):
112 tok = tokenizer_factory()
114 tok.init_from_project()
116 assert tok.normalization is not None
119 def test_update_sql_functions(sql_preprocessor, temp_db_conn,
120 tokenizer_factory, test_config, table_factory,
121 monkeypatch, temp_db_cursor):
122 monkeypatch.setenv('NOMINATIM_MAX_WORD_FREQUENCY', '1133')
123 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
124 tok = tokenizer_factory()
125 tok.init_new_db(test_config)
128 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) == '1133'
130 table_factory('test', 'txt TEXT')
132 func_file = test_config.lib_dir.sql / 'tokenizer' / 'legacy_tokenizer.sql'
133 func_file.write_text("""INSERT INTO test VALUES ('{{max_word_freq}}'),
134 ('{{modulepath}}')""")
136 tok.update_sql_functions(test_config)
138 test_content = temp_db_cursor.row_set('SELECT * FROM test')
139 assert test_content == set((('1133', ), (str(test_config.project_dir / 'module'), )))
142 def test_migrate_database(tokenizer_factory, test_config, temp_db_conn, monkeypatch):
143 monkeypatch.setattr(legacy_tokenizer, '_check_module' , lambda m, c: None)
144 tok = tokenizer_factory()
145 tok.migrate_database(test_config)
147 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_MAXWORDFREQ) is not None
148 assert properties.get_property(temp_db_conn, legacy_tokenizer.DBCFG_NORMALIZATION) is not None
150 outfile = test_config.project_dir / 'module' / 'nominatim.so'
152 assert outfile.exists()
153 assert outfile.read_text() == 'TEST nomiantim.so'
154 assert outfile.stat().st_mode == 33261
157 def test_normalize(analyzer):
158 assert analyzer.normalize('TEsT') == 'test'