2 Tests for running the indexing.
8 from nominatim.indexer.indexer import Indexer
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 TABLE placex (place_id BIGINT,
23 rank_address SMALLINT,
25 indexed_status SMALLINT,
26 indexed_date TIMESTAMP,
29 geometry_sector INTEGER)""")
30 cur.execute("""CREATE TABLE location_property_osmline (
32 indexed_status SMALLINT,
33 indexed_date TIMESTAMP,
34 geometry_sector INTEGER)""")
35 cur.execute("""CREATE TABLE location_postcode (
37 indexed_status SMALLINT,
38 indexed_date TIMESTAMP,
39 country_code varchar(2),
41 cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
44 IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
45 NEW.indexed_date = now();
48 END; $$ LANGUAGE plpgsql;""")
49 for table in ('placex', 'location_property_osmline', 'location_postcode'):
50 cur.execute("""CREATE TRIGGER {0}_update BEFORE UPDATE ON {0}
51 FOR EACH ROW EXECUTE PROCEDURE date_update()
54 def scalar(self, query):
55 with self.conn.cursor() as cur:
57 return cur.fetchone()[0]
59 def add_place(self, cls='place', typ='locality',
60 rank_search=30, rank_address=30, sector=20):
61 next_id = next(self.placex_id)
62 with self.conn.cursor() as cur:
63 cur.execute("""INSERT INTO placex
64 (place_id, class, type, rank_search, rank_address,
65 indexed_status, geometry_sector)
66 VALUES (%s, %s, %s, %s, %s, 1, %s)""",
67 (next_id, cls, typ, rank_search, rank_address, sector))
70 def add_admin(self, **kwargs):
71 kwargs['cls'] = 'boundary'
72 kwargs['typ'] = 'administrative'
73 return self.add_place(**kwargs)
75 def add_osmline(self, sector=20):
76 next_id = next(self.osmline_id)
77 with self.conn.cursor() as cur:
78 cur.execute("""INSERT INTO location_property_osmline
79 (place_id, indexed_status, geometry_sector)
80 VALUES (%s, 1, %s)""",
84 def add_postcode(self, country, postcode):
85 next_id = next(self.postcode_id)
86 with self.conn.cursor() as cur:
87 cur.execute("""INSERT INTO location_postcode
88 (place_id, indexed_status, country_code, postcode)
89 VALUES (%s, 1, %s, %s)""",
90 (next_id, country, postcode))
93 def placex_unindexed(self):
94 return self.scalar('SELECT count(*) from placex where indexed_status > 0')
96 def osmline_unindexed(self):
97 return self.scalar('SELECT count(*) from location_property_osmline where indexed_status > 0')
101 def test_db(temp_db_conn):
102 yield IndexerTestDB(temp_db_conn)
105 @pytest.mark.parametrize("threads", [1, 15])
106 def test_index_all_by_rank(test_db, threads):
107 for rank in range(31):
108 test_db.add_place(rank_address=rank, rank_search=rank)
109 test_db.add_osmline()
111 assert 31 == test_db.placex_unindexed()
112 assert 1 == test_db.osmline_unindexed()
114 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
115 idx.index_by_rank(0, 30)
117 assert 0 == test_db.placex_unindexed()
118 assert 0 == test_db.osmline_unindexed()
120 assert 0 == test_db.scalar("""SELECT count(*) from placex
121 WHERE indexed_status = 0 and indexed_date is null""")
122 # ranks come in order of rank address
123 assert 0 == test_db.scalar("""
124 SELECT count(*) FROM placex p WHERE rank_address > 0
125 AND indexed_date >= (SELECT min(indexed_date) FROM placex o
126 WHERE p.rank_address < o.rank_address)""")
127 # placex rank < 30 objects come before interpolations
128 assert 0 == test_db.scalar(
129 """SELECT count(*) FROM placex WHERE rank_address < 30
130 AND indexed_date > (SELECT min(indexed_date) FROM location_property_osmline)""")
131 # placex rank = 30 objects come after interpolations
132 assert 0 == test_db.scalar(
133 """SELECT count(*) FROM placex WHERE rank_address = 30
134 AND indexed_date < (SELECT max(indexed_date) FROM location_property_osmline)""")
135 # rank 0 comes after rank 29 and before rank 30
136 assert 0 == test_db.scalar(
137 """SELECT count(*) FROM placex WHERE rank_address < 30
138 AND indexed_date > (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""")
139 assert 0 == test_db.scalar(
140 """SELECT count(*) FROM placex WHERE rank_address = 30
141 AND indexed_date < (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""")
144 @pytest.mark.parametrize("threads", [1, 15])
145 def test_index_partial_without_30(test_db, threads):
146 for rank in range(31):
147 test_db.add_place(rank_address=rank, rank_search=rank)
148 test_db.add_osmline()
150 assert 31 == test_db.placex_unindexed()
151 assert 1 == test_db.osmline_unindexed()
153 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
154 idx.index_by_rank(4, 15)
156 assert 19 == test_db.placex_unindexed()
157 assert 1 == test_db.osmline_unindexed()
159 assert 0 == test_db.scalar("""
160 SELECT count(*) FROM placex
161 WHERE indexed_status = 0 AND not rank_address between 4 and 15""")
164 @pytest.mark.parametrize("threads", [1, 15])
165 def test_index_partial_with_30(test_db, threads):
166 for rank in range(31):
167 test_db.add_place(rank_address=rank, rank_search=rank)
168 test_db.add_osmline()
170 assert 31 == test_db.placex_unindexed()
171 assert 1 == test_db.osmline_unindexed()
173 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
174 idx.index_by_rank(28, 30)
176 assert 27 == test_db.placex_unindexed()
177 assert 0 == test_db.osmline_unindexed()
179 assert 0 == test_db.scalar("""
180 SELECT count(*) FROM placex
181 WHERE indexed_status = 0 AND rank_address between 1 and 27""")
183 @pytest.mark.parametrize("threads", [1, 15])
184 def test_index_boundaries(test_db, threads):
185 for rank in range(4, 10):
186 test_db.add_admin(rank_address=rank, rank_search=rank)
187 for rank in range(31):
188 test_db.add_place(rank_address=rank, rank_search=rank)
189 test_db.add_osmline()
191 assert 37 == test_db.placex_unindexed()
192 assert 1 == test_db.osmline_unindexed()
194 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
195 idx.index_boundaries(0, 30)
197 assert 31 == test_db.placex_unindexed()
198 assert 1 == test_db.osmline_unindexed()
200 assert 0 == test_db.scalar("""
201 SELECT count(*) FROM placex
202 WHERE indexed_status = 0 AND class != 'boundary'""")
205 @pytest.mark.parametrize("threads", [1, 15])
206 def test_index_postcodes(test_db, threads):
207 for postcode in range(1000):
208 test_db.add_postcode('de', postcode)
209 for postcode in range(32000, 33000):
210 test_db.add_postcode('us', postcode)
212 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
213 idx.index_postcodes()
215 assert 0 == test_db.scalar("""SELECT count(*) FROM location_postcode
216 WHERE indexed_status != 0""")
219 def test_index_full(test_db):
220 for rank in range(4, 10):
221 test_db.add_admin(rank_address=rank, rank_search=rank)
222 for rank in range(31):
223 test_db.add_place(rank_address=rank, rank_search=rank)
224 test_db.add_osmline()
225 for postcode in range(1000):
226 test_db.add_postcode('de', postcode)
228 idx = Indexer('dbname=test_nominatim_python_unittest', 4)
231 assert 0 == test_db.placex_unindexed()
232 assert 0 == test_db.osmline_unindexed()
233 assert 0 == test_db.scalar("""SELECT count(*) FROM location_postcode
234 WHERE indexed_status != 0""")