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,
 
  24                                                 rank_address SMALLINT,
 
  26                                                 indexed_status SMALLINT,
 
  27                                                 indexed_date TIMESTAMP,
 
  32                                                 geometry_sector INTEGER)""")
 
  33             cur.execute("""CREATE TABLE location_property_osmline (
 
  38                                indexed_status SMALLINT,
 
  39                                indexed_date TIMESTAMP,
 
  40                                geometry_sector INTEGER)""")
 
  41             cur.execute("""CREATE TABLE location_postcode (
 
  43                                indexed_status SMALLINT,
 
  44                                indexed_date TIMESTAMP,
 
  45                                country_code varchar(2),
 
  47             cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
 
  50                              IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
 
  51                                NEW.indexed_date = now();
 
  54                            END; $$ LANGUAGE plpgsql;""")
 
  55             cur.execute("""CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
 
  58                                                       OUT country_feature VARCHAR)
 
  64                            $$ LANGUAGE plpgsql STABLE;
 
  66             cur.execute("""CREATE OR REPLACE FUNCTION
 
  67                              get_interpolation_address(in_address HSTORE, wayid BIGINT)
 
  72                            $$ LANGUAGE plpgsql STABLE;
 
  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()
 
  80     def scalar(self, query):
 
  81         with self.conn.cursor() as cur:
 
  83             return cur.fetchone()[0]
 
  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))
 
  96     def add_admin(self, **kwargs):
 
  97         kwargs['cls'] = 'boundary'
 
  98         kwargs['typ'] = 'administrative'
 
  99         return self.add_place(**kwargs)
 
 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))
 
 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))
 
 119     def placex_unindexed(self):
 
 120         return self.scalar('SELECT count(*) from placex where indexed_status > 0')
 
 122     def osmline_unindexed(self):
 
 123         return self.scalar("""SELECT count(*) from location_property_osmline
 
 124                               WHERE indexed_status > 0""")
 
 128 def test_db(temp_db_conn):
 
 129     yield IndexerTestDB(temp_db_conn)
 
 133 def test_tokenizer(tokenizer_mock, def_config, tmp_path):
 
 134     def_config.project_dir = tmp_path
 
 135     return factory.create_tokenizer(def_config)
 
 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()
 
 144     assert test_db.placex_unindexed() == 31
 
 145     assert test_db.osmline_unindexed() == 1
 
 147     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
 
 148     idx.index_by_rank(0, 30)
 
 150     assert test_db.placex_unindexed() == 0
 
 151     assert test_db.osmline_unindexed() == 0
 
 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
 
 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
 
 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
 
 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
 
 178                    (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
 
 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()
 
 187     assert test_db.placex_unindexed() == 31
 
 188     assert test_db.osmline_unindexed() == 1
 
 190     idx = indexer.Indexer('dbname=test_nominatim_python_unittest',
 
 191                           test_tokenizer, threads)
 
 192     idx.index_by_rank(4, 15)
 
 194     assert test_db.placex_unindexed() == 19
 
 195     assert test_db.osmline_unindexed() == 1
 
 197     assert test_db.scalar("""
 
 198                     SELECT count(*) FROM placex
 
 199                       WHERE indexed_status = 0 AND not rank_address between 4 and 15""") == 0
 
 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()
 
 208     assert test_db.placex_unindexed() == 31
 
 209     assert test_db.osmline_unindexed() == 1
 
 211     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
 
 212     idx.index_by_rank(28, 30)
 
 214     assert test_db.placex_unindexed() == 27
 
 215     assert test_db.osmline_unindexed() == 0
 
 217     assert test_db.scalar("""
 
 218                     SELECT count(*) FROM placex
 
 219                       WHERE indexed_status = 0 AND rank_address between 1 and 27""") == 0
 
 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()
 
 229     assert test_db.placex_unindexed() == 37
 
 230     assert test_db.osmline_unindexed() == 1
 
 232     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
 
 233     idx.index_boundaries(0, 30)
 
 235     assert test_db.placex_unindexed() == 31
 
 236     assert test_db.osmline_unindexed() == 1
 
 238     assert test_db.scalar("""
 
 239                     SELECT count(*) FROM placex
 
 240                       WHERE indexed_status = 0 AND class != 'boundary'""") == 0
 
 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)
 
 250     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
 
 251     idx.index_postcodes()
 
 253     assert test_db.scalar("""SELECT count(*) FROM location_postcode
 
 254                                   WHERE indexed_status != 0""") == 0
 
 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)
 
 267     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, 4)
 
 268     idx.index_full(analyse=analyse)
 
 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
 
 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)
 
 280     for _ in range(1000):
 
 281         test_db.add_place(rank_address=30, rank_search=30)
 
 283     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
 
 284     idx.index_by_rank(28, 30)
 
 286     assert test_db.placex_unindexed() == 0