]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_indexing.py
adapt tests for ICU tokenizer
[nominatim.git] / test / python / test_indexing.py
1 """
2 Tests for running the indexing.
3 """
4 import itertools
5 import pytest
6
7 from nominatim.indexer import indexer
8 from nominatim.tokenizer import factory
9
10 class IndexerTestDB:
11
12     def __init__(self, conn):
13         self.placex_id = itertools.count(100000)
14         self.osmline_id = itertools.count(500000)
15         self.postcode_id = itertools.count(700000)
16
17         self.conn = conn
18         self.conn.set_isolation_level(0)
19         with self.conn.cursor() as cur:
20             cur.execute('CREATE EXTENSION hstore')
21             cur.execute("""CREATE TABLE placex (place_id BIGINT,
22                                                 class TEXT,
23                                                 type TEXT,
24                                                 rank_address SMALLINT,
25                                                 rank_search SMALLINT,
26                                                 indexed_status SMALLINT,
27                                                 indexed_date TIMESTAMP,
28                                                 partition SMALLINT,
29                                                 admin_level SMALLINT,
30                                                 address HSTORE,
31                                                 token_info JSONB,
32                                                 geometry_sector INTEGER)""")
33             cur.execute("""CREATE TABLE location_property_osmline (
34                                place_id BIGINT,
35                                osm_id BIGINT,
36                                address HSTORE,
37                                token_info JSONB,
38                                indexed_status SMALLINT,
39                                indexed_date TIMESTAMP,
40                                geometry_sector INTEGER)""")
41             cur.execute("""CREATE TABLE location_postcode (
42                                place_id BIGINT,
43                                indexed_status SMALLINT,
44                                indexed_date TIMESTAMP,
45                                country_code varchar(2),
46                                postcode TEXT)""")
47             cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
48                            AS $$
49                            BEGIN
50                              IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
51                                NEW.indexed_date = now();
52                              END IF;
53                              RETURN NEW;
54                            END; $$ LANGUAGE plpgsql;""")
55             cur.execute("""CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
56                                                       OUT name HSTORE,
57                                                       OUT address HSTORE,
58                                                       OUT country_feature VARCHAR)
59                            AS $$
60                            BEGIN
61                             address := p.address;
62                             name := p.address;
63                            END;
64                            $$ LANGUAGE plpgsql STABLE;
65                         """)
66             cur.execute("""CREATE OR REPLACE FUNCTION
67                              get_interpolation_address(in_address HSTORE, wayid BIGINT)
68                            RETURNS HSTORE AS $$
69                            BEGIN
70                              RETURN in_address;
71                            END;
72                            $$ LANGUAGE plpgsql STABLE;
73                         """)
74
75             for table in ('placex', 'location_property_osmline', 'location_postcode'):
76                 cur.execute("""CREATE TRIGGER {0}_update BEFORE UPDATE ON {0}
77                                FOR EACH ROW EXECUTE PROCEDURE date_update()
78                             """.format(table))
79
80     def scalar(self, query):
81         with self.conn.cursor() as cur:
82             cur.execute(query)
83             return cur.fetchone()[0]
84
85     def add_place(self, cls='place', typ='locality',
86                   rank_search=30, rank_address=30, sector=20):
87         next_id = next(self.placex_id)
88         with self.conn.cursor() as cur:
89             cur.execute("""INSERT INTO placex
90                               (place_id, class, type, rank_search, rank_address,
91                                indexed_status, geometry_sector)
92                               VALUES (%s, %s, %s, %s, %s, 1, %s)""",
93                         (next_id, cls, typ, rank_search, rank_address, sector))
94         return next_id
95
96     def add_admin(self, **kwargs):
97         kwargs['cls'] = 'boundary'
98         kwargs['typ'] = 'administrative'
99         return self.add_place(**kwargs)
100
101     def add_osmline(self, sector=20):
102         next_id = next(self.osmline_id)
103         with self.conn.cursor() as cur:
104             cur.execute("""INSERT INTO location_property_osmline
105                               (place_id, osm_id, indexed_status, geometry_sector)
106                               VALUES (%s, %s, 1, %s)""",
107                         (next_id, next_id, sector))
108         return next_id
109
110     def add_postcode(self, country, postcode):
111         next_id = next(self.postcode_id)
112         with self.conn.cursor() as cur:
113             cur.execute("""INSERT INTO location_postcode
114                             (place_id, indexed_status, country_code, postcode)
115                             VALUES (%s, 1, %s, %s)""",
116                         (next_id, country, postcode))
117         return next_id
118
119     def placex_unindexed(self):
120         return self.scalar('SELECT count(*) from placex where indexed_status > 0')
121
122     def osmline_unindexed(self):
123         return self.scalar("""SELECT count(*) from location_property_osmline
124                               WHERE indexed_status > 0""")
125
126
127 @pytest.fixture
128 def test_db(temp_db_conn):
129     yield IndexerTestDB(temp_db_conn)
130
131
132 @pytest.fixture
133 def test_tokenizer(tokenizer_mock, def_config, tmp_path):
134     def_config.project_dir = tmp_path
135     return factory.create_tokenizer(def_config)
136
137
138 @pytest.mark.parametrize("threads", [1, 15])
139 def test_index_all_by_rank(test_db, threads, test_tokenizer):
140     for rank in range(31):
141         test_db.add_place(rank_address=rank, rank_search=rank)
142     test_db.add_osmline()
143
144     assert test_db.placex_unindexed() == 31
145     assert test_db.osmline_unindexed() == 1
146
147     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
148     idx.index_by_rank(0, 30)
149
150     assert test_db.placex_unindexed() == 0
151     assert test_db.osmline_unindexed() == 0
152
153     assert test_db.scalar("""SELECT count(*) from placex
154                              WHERE indexed_status = 0 and indexed_date is null""") == 0
155     # ranks come in order of rank address
156     assert test_db.scalar("""
157         SELECT count(*) FROM placex p WHERE rank_address > 0
158           AND indexed_date >= (SELECT min(indexed_date) FROM placex o
159                                WHERE p.rank_address < o.rank_address)""") == 0
160     # placex rank < 30 objects come before interpolations
161     assert test_db.scalar(
162         """SELECT count(*) FROM placex WHERE rank_address < 30
163              AND indexed_date >
164                    (SELECT min(indexed_date) FROM location_property_osmline)""") == 0
165     # placex rank = 30 objects come after interpolations
166     assert test_db.scalar(
167         """SELECT count(*) FROM placex WHERE rank_address = 30
168              AND indexed_date <
169                    (SELECT max(indexed_date) FROM location_property_osmline)""") == 0
170     # rank 0 comes after rank 29 and before rank 30
171     assert test_db.scalar(
172         """SELECT count(*) FROM placex WHERE rank_address < 30
173              AND indexed_date >
174                    (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
175     assert test_db.scalar(
176         """SELECT count(*) FROM placex WHERE rank_address = 30
177              AND indexed_date <
178                    (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
179
180
181 @pytest.mark.parametrize("threads", [1, 15])
182 def test_index_partial_without_30(test_db, threads, test_tokenizer):
183     for rank in range(31):
184         test_db.add_place(rank_address=rank, rank_search=rank)
185     test_db.add_osmline()
186
187     assert test_db.placex_unindexed() == 31
188     assert test_db.osmline_unindexed() == 1
189
190     idx = indexer.Indexer('dbname=test_nominatim_python_unittest',
191                           test_tokenizer, threads)
192     idx.index_by_rank(4, 15)
193
194     assert test_db.placex_unindexed() == 19
195     assert test_db.osmline_unindexed() == 1
196
197     assert test_db.scalar("""
198                     SELECT count(*) FROM placex
199                       WHERE indexed_status = 0 AND not rank_address between 4 and 15""") == 0
200
201
202 @pytest.mark.parametrize("threads", [1, 15])
203 def test_index_partial_with_30(test_db, threads, test_tokenizer):
204     for rank in range(31):
205         test_db.add_place(rank_address=rank, rank_search=rank)
206     test_db.add_osmline()
207
208     assert test_db.placex_unindexed() == 31
209     assert test_db.osmline_unindexed() == 1
210
211     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
212     idx.index_by_rank(28, 30)
213
214     assert test_db.placex_unindexed() == 27
215     assert test_db.osmline_unindexed() == 0
216
217     assert test_db.scalar("""
218                     SELECT count(*) FROM placex
219                       WHERE indexed_status = 0 AND rank_address between 1 and 27""") == 0
220
221 @pytest.mark.parametrize("threads", [1, 15])
222 def test_index_boundaries(test_db, threads, test_tokenizer):
223     for rank in range(4, 10):
224         test_db.add_admin(rank_address=rank, rank_search=rank)
225     for rank in range(31):
226         test_db.add_place(rank_address=rank, rank_search=rank)
227     test_db.add_osmline()
228
229     assert test_db.placex_unindexed() == 37
230     assert test_db.osmline_unindexed() == 1
231
232     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
233     idx.index_boundaries(0, 30)
234
235     assert test_db.placex_unindexed() == 31
236     assert test_db.osmline_unindexed() == 1
237
238     assert test_db.scalar("""
239                     SELECT count(*) FROM placex
240                       WHERE indexed_status = 0 AND class != 'boundary'""") == 0
241
242
243 @pytest.mark.parametrize("threads", [1, 15])
244 def test_index_postcodes(test_db, threads, test_tokenizer):
245     for postcode in range(1000):
246         test_db.add_postcode('de', postcode)
247     for postcode in range(32000, 33000):
248         test_db.add_postcode('us', postcode)
249
250     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
251     idx.index_postcodes()
252
253     assert test_db.scalar("""SELECT count(*) FROM location_postcode
254                                   WHERE indexed_status != 0""") == 0
255
256
257 @pytest.mark.parametrize("analyse", [True, False])
258 def test_index_full(test_db, analyse, test_tokenizer):
259     for rank in range(4, 10):
260         test_db.add_admin(rank_address=rank, rank_search=rank)
261     for rank in range(31):
262         test_db.add_place(rank_address=rank, rank_search=rank)
263     test_db.add_osmline()
264     for postcode in range(1000):
265         test_db.add_postcode('de', postcode)
266
267     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, 4)
268     idx.index_full(analyse=analyse)
269
270     assert test_db.placex_unindexed() == 0
271     assert test_db.osmline_unindexed() == 0
272     assert test_db.scalar("""SELECT count(*) FROM location_postcode
273                              WHERE indexed_status != 0""") == 0
274
275
276 @pytest.mark.parametrize("threads", [1, 15])
277 def test_index_reopen_connection(test_db, threads, monkeypatch, test_tokenizer):
278     monkeypatch.setattr(indexer.WorkerPool, "REOPEN_CONNECTIONS_AFTER", 15)
279
280     for _ in range(1000):
281         test_db.add_place(rank_address=30, rank_search=30)
282
283     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
284     idx.index_by_rank(28, 30)
285
286     assert test_db.placex_unindexed() == 0