2 Tests for running the indexing.
7 from nominatim.indexer import indexer
8 from nominatim.tokenizer import factory
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)
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,
25 linked_place_id BIGINT,
26 rank_address SMALLINT,
28 indexed_status SMALLINT,
29 indexed_date TIMESTAMP,
35 geometry_sector INTEGER)""")
36 cur.execute("""CREATE TABLE location_property_osmline (
41 indexed_status SMALLINT,
42 indexed_date TIMESTAMP,
43 geometry_sector INTEGER)""")
44 cur.execute("""CREATE TABLE location_postcode (
46 indexed_status SMALLINT,
47 indexed_date TIMESTAMP,
48 country_code varchar(2),
50 cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
53 IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
54 NEW.indexed_date = now();
57 END; $$ LANGUAGE plpgsql;""")
58 cur.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
59 cur.execute("""CREATE TYPE prepare_update_info AS (
62 rank_address SMALLINT,
66 linked_place_id BIGINT
68 cur.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
69 OUT result prepare_update_info)
72 result.address := p.address;
73 result.name := p.name;
74 result.class := p.class;
75 result.type := p.type;
76 result.country_code := p.country_code;
77 result.rank_address := p.rank_address;
79 $$ LANGUAGE plpgsql STABLE;
81 cur.execute("""CREATE OR REPLACE FUNCTION
82 get_interpolation_address(in_address HSTORE, wayid BIGINT)
87 $$ LANGUAGE plpgsql STABLE;
90 for table in ('placex', 'location_property_osmline', 'location_postcode'):
91 cur.execute("""CREATE TRIGGER {0}_update BEFORE UPDATE ON {0}
92 FOR EACH ROW EXECUTE PROCEDURE date_update()
95 def scalar(self, query):
96 with self.conn.cursor() as cur:
98 return cur.fetchone()[0]
100 def add_place(self, cls='place', typ='locality',
101 rank_search=30, rank_address=30, sector=20):
102 next_id = next(self.placex_id)
103 with self.conn.cursor() as cur:
104 cur.execute("""INSERT INTO placex
105 (place_id, class, type, rank_search, rank_address,
106 indexed_status, geometry_sector)
107 VALUES (%s, %s, %s, %s, %s, 1, %s)""",
108 (next_id, cls, typ, rank_search, rank_address, sector))
111 def add_admin(self, **kwargs):
112 kwargs['cls'] = 'boundary'
113 kwargs['typ'] = 'administrative'
114 return self.add_place(**kwargs)
116 def add_osmline(self, sector=20):
117 next_id = next(self.osmline_id)
118 with self.conn.cursor() as cur:
119 cur.execute("""INSERT INTO location_property_osmline
120 (place_id, osm_id, indexed_status, geometry_sector)
121 VALUES (%s, %s, 1, %s)""",
122 (next_id, next_id, sector))
125 def add_postcode(self, country, postcode):
126 next_id = next(self.postcode_id)
127 with self.conn.cursor() as cur:
128 cur.execute("""INSERT INTO location_postcode
129 (place_id, indexed_status, country_code, postcode)
130 VALUES (%s, 1, %s, %s)""",
131 (next_id, country, postcode))
134 def placex_unindexed(self):
135 return self.scalar('SELECT count(*) from placex where indexed_status > 0')
137 def osmline_unindexed(self):
138 return self.scalar("""SELECT count(*) from location_property_osmline
139 WHERE indexed_status > 0""")
143 def test_db(temp_db_conn):
144 yield IndexerTestDB(temp_db_conn)
148 def test_tokenizer(tokenizer_mock, project_env):
149 return factory.create_tokenizer(project_env)
152 @pytest.mark.parametrize("threads", [1, 15])
153 def test_index_all_by_rank(test_db, threads, test_tokenizer):
154 for rank in range(31):
155 test_db.add_place(rank_address=rank, rank_search=rank)
156 test_db.add_osmline()
158 assert test_db.placex_unindexed() == 31
159 assert test_db.osmline_unindexed() == 1
161 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
162 idx.index_by_rank(0, 30)
164 assert test_db.placex_unindexed() == 0
165 assert test_db.osmline_unindexed() == 0
167 assert test_db.scalar("""SELECT count(*) from placex
168 WHERE indexed_status = 0 and indexed_date is null""") == 0
169 # ranks come in order of rank address
170 assert test_db.scalar("""
171 SELECT count(*) FROM placex p WHERE rank_address > 0
172 AND indexed_date >= (SELECT min(indexed_date) FROM placex o
173 WHERE p.rank_address < o.rank_address)""") == 0
174 # placex rank < 30 objects come before interpolations
175 assert test_db.scalar(
176 """SELECT count(*) FROM placex WHERE rank_address < 30
178 (SELECT min(indexed_date) FROM location_property_osmline)""") == 0
179 # placex rank = 30 objects come after interpolations
180 assert test_db.scalar(
181 """SELECT count(*) FROM placex WHERE rank_address = 30
183 (SELECT max(indexed_date) FROM location_property_osmline)""") == 0
184 # rank 0 comes after rank 29 and before rank 30
185 assert test_db.scalar(
186 """SELECT count(*) FROM placex WHERE rank_address < 30
188 (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
189 assert test_db.scalar(
190 """SELECT count(*) FROM placex WHERE rank_address = 30
192 (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
195 @pytest.mark.parametrize("threads", [1, 15])
196 def test_index_partial_without_30(test_db, threads, test_tokenizer):
197 for rank in range(31):
198 test_db.add_place(rank_address=rank, rank_search=rank)
199 test_db.add_osmline()
201 assert test_db.placex_unindexed() == 31
202 assert test_db.osmline_unindexed() == 1
204 idx = indexer.Indexer('dbname=test_nominatim_python_unittest',
205 test_tokenizer, threads)
206 idx.index_by_rank(4, 15)
208 assert test_db.placex_unindexed() == 19
209 assert test_db.osmline_unindexed() == 1
211 assert test_db.scalar("""
212 SELECT count(*) FROM placex
213 WHERE indexed_status = 0 AND not rank_address between 4 and 15""") == 0
216 @pytest.mark.parametrize("threads", [1, 15])
217 def test_index_partial_with_30(test_db, threads, test_tokenizer):
218 for rank in range(31):
219 test_db.add_place(rank_address=rank, rank_search=rank)
220 test_db.add_osmline()
222 assert test_db.placex_unindexed() == 31
223 assert test_db.osmline_unindexed() == 1
225 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
226 idx.index_by_rank(28, 30)
228 assert test_db.placex_unindexed() == 27
229 assert test_db.osmline_unindexed() == 0
231 assert test_db.scalar("""
232 SELECT count(*) FROM placex
233 WHERE indexed_status = 0 AND rank_address between 1 and 27""") == 0
235 @pytest.mark.parametrize("threads", [1, 15])
236 def test_index_boundaries(test_db, threads, test_tokenizer):
237 for rank in range(4, 10):
238 test_db.add_admin(rank_address=rank, rank_search=rank)
239 for rank in range(31):
240 test_db.add_place(rank_address=rank, rank_search=rank)
241 test_db.add_osmline()
243 assert test_db.placex_unindexed() == 37
244 assert test_db.osmline_unindexed() == 1
246 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
247 idx.index_boundaries(0, 30)
249 assert test_db.placex_unindexed() == 31
250 assert test_db.osmline_unindexed() == 1
252 assert test_db.scalar("""
253 SELECT count(*) FROM placex
254 WHERE indexed_status = 0 AND class != 'boundary'""") == 0
257 @pytest.mark.parametrize("threads", [1, 15])
258 def test_index_postcodes(test_db, threads, test_tokenizer):
259 for postcode in range(1000):
260 test_db.add_postcode('de', postcode)
261 for postcode in range(32000, 33000):
262 test_db.add_postcode('us', postcode)
264 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
265 idx.index_postcodes()
267 assert test_db.scalar("""SELECT count(*) FROM location_postcode
268 WHERE indexed_status != 0""") == 0
271 @pytest.mark.parametrize("analyse", [True, False])
272 def test_index_full(test_db, analyse, test_tokenizer):
273 for rank in range(4, 10):
274 test_db.add_admin(rank_address=rank, rank_search=rank)
275 for rank in range(31):
276 test_db.add_place(rank_address=rank, rank_search=rank)
277 test_db.add_osmline()
278 for postcode in range(1000):
279 test_db.add_postcode('de', postcode)
281 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, 4)
282 idx.index_full(analyse=analyse)
284 assert test_db.placex_unindexed() == 0
285 assert test_db.osmline_unindexed() == 0
286 assert test_db.scalar("""SELECT count(*) FROM location_postcode
287 WHERE indexed_status != 0""") == 0
290 @pytest.mark.parametrize("threads", [1, 15])
291 def test_index_reopen_connection(test_db, threads, monkeypatch, test_tokenizer):
292 monkeypatch.setattr(indexer.WorkerPool, "REOPEN_CONNECTIONS_AFTER", 15)
294 for _ in range(1000):
295 test_db.add_place(rank_address=30, rank_search=30)
297 idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
298 idx.index_by_rank(28, 30)
300 assert test_db.placex_unindexed() == 0