]> git.openstreetmap.org Git - nominatim.git/blobdiff - test/python/test_indexing.py
add new replication mode catch-up
[nominatim.git] / test / python / test_indexing.py
index d68769064af9d6ff626ac1e78b9bcd684b9c6c51..4c9d940d09b1c2a0a0cddbe78383c34c9251af53 100644 (file)
@@ -2,7 +2,6 @@
 Tests for running the indexing.
 """
 import itertools
-import psycopg2
 import pytest
 
 from nominatim.indexer import indexer
@@ -20,19 +19,25 @@ class IndexerTestDB:
         with self.conn.cursor() as cur:
             cur.execute('CREATE EXTENSION hstore')
             cur.execute("""CREATE TABLE placex (place_id BIGINT,
+                                                name HSTORE,
                                                 class TEXT,
                                                 type TEXT,
+                                                linked_place_id BIGINT,
                                                 rank_address SMALLINT,
                                                 rank_search SMALLINT,
                                                 indexed_status SMALLINT,
                                                 indexed_date TIMESTAMP,
                                                 partition SMALLINT,
                                                 admin_level SMALLINT,
+                                                country_code TEXT,
                                                 address HSTORE,
                                                 token_info JSONB,
                                                 geometry_sector INTEGER)""")
             cur.execute("""CREATE TABLE location_property_osmline (
                                place_id BIGINT,
+                               osm_id BIGINT,
+                               address HSTORE,
+                               token_info JSONB,
                                indexed_status SMALLINT,
                                indexed_date TIMESTAMP,
                                geometry_sector INTEGER)""")
@@ -50,17 +55,38 @@ class IndexerTestDB:
                              END IF;
                              RETURN NEW;
                            END; $$ LANGUAGE plpgsql;""")
-            cur.execute("""CREATE OR REPLACE FUNCTION placex_prepare_update(p placex,
-                                                      OUT name HSTORE,
-                                                      OUT address HSTORE,
-                                                      OUT country_feature VARCHAR)
+            cur.execute("DROP TYPE IF EXISTS prepare_update_info CASCADE")
+            cur.execute("""CREATE TYPE prepare_update_info AS (
+                             name HSTORE,
+                             address HSTORE,
+                             rank_address SMALLINT,
+                             country_code TEXT,
+                             class TEXT,
+                             type TEXT,
+                             linked_place_id BIGINT
+                           )""")
+            cur.execute("""CREATE OR REPLACE FUNCTION placex_indexing_prepare(p placex,
+                                                     OUT result prepare_update_info)
                            AS $$
                            BEGIN
-                            address := p.address;
-                            name := p.address;
+                             result.address := p.address;
+                             result.name := p.name;
+                             result.class := p.class;
+                             result.type := p.type;
+                             result.country_code := p.country_code;
+                             result.rank_address := p.rank_address;
                            END;
                            $$ LANGUAGE plpgsql STABLE;
                         """)
+            cur.execute("""CREATE OR REPLACE FUNCTION
+                             get_interpolation_address(in_address HSTORE, wayid BIGINT)
+                           RETURNS HSTORE AS $$
+                           BEGIN
+                             RETURN in_address;
+                           END;
+                           $$ LANGUAGE plpgsql STABLE;
+                        """)
+
             for table in ('placex', 'location_property_osmline', 'location_postcode'):
                 cur.execute("""CREATE TRIGGER {0}_update BEFORE UPDATE ON {0}
                                FOR EACH ROW EXECUTE PROCEDURE date_update()
@@ -91,9 +117,9 @@ class IndexerTestDB:
         next_id = next(self.osmline_id)
         with self.conn.cursor() as cur:
             cur.execute("""INSERT INTO location_property_osmline
-                              (place_id, indexed_status, geometry_sector)
-                              VALUES (%s, 1, %s)""",
-                        (next_id, sector))
+                              (place_id, osm_id, indexed_status, geometry_sector)
+                              VALUES (%s, %s, 1, %s)""",
+                        (next_id, next_id, sector))
         return next_id
 
     def add_postcode(self, country, postcode):
@@ -109,7 +135,8 @@ class IndexerTestDB:
         return self.scalar('SELECT count(*) from placex where indexed_status > 0')
 
     def osmline_unindexed(self):
-        return self.scalar('SELECT count(*) from location_property_osmline where indexed_status > 0')
+        return self.scalar("""SELECT count(*) from location_property_osmline
+                              WHERE indexed_status > 0""")
 
 
 @pytest.fixture
@@ -129,37 +156,41 @@ def test_index_all_by_rank(test_db, threads, test_tokenizer):
         test_db.add_place(rank_address=rank, rank_search=rank)
     test_db.add_osmline()
 
-    assert 31 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 31
+    assert test_db.osmline_unindexed() == 1
 
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
     idx.index_by_rank(0, 30)
 
-    assert 0 == test_db.placex_unindexed()
-    assert 0 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 0
+    assert test_db.osmline_unindexed() == 0
 
-    assert 0 == test_db.scalar("""SELECT count(*) from placex
-                               WHERE indexed_status = 0 and indexed_date is null""")
+    assert test_db.scalar("""SELECT count(*) from placex
+                             WHERE indexed_status = 0 and indexed_date is null""") == 0
     # ranks come in order of rank address
-    assert 0 == test_db.scalar("""
+    assert test_db.scalar("""
         SELECT count(*) FROM placex p WHERE rank_address > 0
           AND indexed_date >= (SELECT min(indexed_date) FROM placex o
-                               WHERE p.rank_address < o.rank_address)""")
+                               WHERE p.rank_address < o.rank_address)""") == 0
     # placex rank < 30 objects come before interpolations
-    assert 0 == test_db.scalar(
+    assert test_db.scalar(
         """SELECT count(*) FROM placex WHERE rank_address < 30
-             AND indexed_date > (SELECT min(indexed_date) FROM location_property_osmline)""")
+             AND indexed_date >
+                   (SELECT min(indexed_date) FROM location_property_osmline)""") == 0
     # placex rank = 30 objects come after interpolations
-    assert 0 == test_db.scalar(
+    assert test_db.scalar(
         """SELECT count(*) FROM placex WHERE rank_address = 30
-             AND indexed_date < (SELECT max(indexed_date) FROM location_property_osmline)""")
+             AND indexed_date <
+                   (SELECT max(indexed_date) FROM location_property_osmline)""") == 0
     # rank 0 comes after rank 29 and before rank 30
-    assert 0 == test_db.scalar(
+    assert test_db.scalar(
         """SELECT count(*) FROM placex WHERE rank_address < 30
-             AND indexed_date > (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""")
-    assert 0 == test_db.scalar(
+             AND indexed_date >
+                   (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
+    assert test_db.scalar(
         """SELECT count(*) FROM placex WHERE rank_address = 30
-             AND indexed_date < (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""")
+             AND indexed_date <
+                   (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""") == 0
 
 
 @pytest.mark.parametrize("threads", [1, 15])
@@ -168,19 +199,19 @@ def test_index_partial_without_30(test_db, threads, test_tokenizer):
         test_db.add_place(rank_address=rank, rank_search=rank)
     test_db.add_osmline()
 
-    assert 31 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 31
+    assert test_db.osmline_unindexed() == 1
 
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest',
                           test_tokenizer, threads)
     idx.index_by_rank(4, 15)
 
-    assert 19 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 19
+    assert test_db.osmline_unindexed() == 1
 
-    assert 0 == test_db.scalar("""
+    assert test_db.scalar("""
                     SELECT count(*) FROM placex
-                      WHERE indexed_status = 0 AND not rank_address between 4 and 15""")
+                      WHERE indexed_status = 0 AND not rank_address between 4 and 15""") == 0
 
 
 @pytest.mark.parametrize("threads", [1, 15])
@@ -189,18 +220,18 @@ def test_index_partial_with_30(test_db, threads, test_tokenizer):
         test_db.add_place(rank_address=rank, rank_search=rank)
     test_db.add_osmline()
 
-    assert 31 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 31
+    assert test_db.osmline_unindexed() == 1
 
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
     idx.index_by_rank(28, 30)
 
-    assert 27 == test_db.placex_unindexed()
-    assert 0 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 27
+    assert test_db.osmline_unindexed() == 0
 
-    assert 0 == test_db.scalar("""
+    assert test_db.scalar("""
                     SELECT count(*) FROM placex
-                      WHERE indexed_status = 0 AND rank_address between 1 and 27""")
+                      WHERE indexed_status = 0 AND rank_address between 1 and 27""") == 0
 
 @pytest.mark.parametrize("threads", [1, 15])
 def test_index_boundaries(test_db, threads, test_tokenizer):
@@ -210,18 +241,18 @@ def test_index_boundaries(test_db, threads, test_tokenizer):
         test_db.add_place(rank_address=rank, rank_search=rank)
     test_db.add_osmline()
 
-    assert 37 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 37
+    assert test_db.osmline_unindexed() == 1
 
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
     idx.index_boundaries(0, 30)
 
-    assert 31 == test_db.placex_unindexed()
-    assert 1 == test_db.osmline_unindexed()
+    assert test_db.placex_unindexed() == 31
+    assert test_db.osmline_unindexed() == 1
 
-    assert 0 == test_db.scalar("""
+    assert test_db.scalar("""
                     SELECT count(*) FROM placex
-                      WHERE indexed_status = 0 AND class != 'boundary'""")
+                      WHERE indexed_status = 0 AND class != 'boundary'""") == 0
 
 
 @pytest.mark.parametrize("threads", [1, 15])
@@ -234,8 +265,8 @@ def test_index_postcodes(test_db, threads, test_tokenizer):
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
     idx.index_postcodes()
 
-    assert 0 == test_db.scalar("""SELECT count(*) FROM location_postcode
-                                  WHERE indexed_status != 0""")
+    assert test_db.scalar("""SELECT count(*) FROM location_postcode
+                                  WHERE indexed_status != 0""") == 0
 
 
 @pytest.mark.parametrize("analyse", [True, False])
@@ -251,10 +282,10 @@ def test_index_full(test_db, analyse, test_tokenizer):
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, 4)
     idx.index_full(analyse=analyse)
 
-    assert 0 == test_db.placex_unindexed()
-    assert 0 == test_db.osmline_unindexed()
-    assert 0 == test_db.scalar("""SELECT count(*) FROM location_postcode
-                                  WHERE indexed_status != 0""")
+    assert test_db.placex_unindexed() == 0
+    assert test_db.osmline_unindexed() == 0
+    assert test_db.scalar("""SELECT count(*) FROM location_postcode
+                             WHERE indexed_status != 0""") == 0
 
 
 @pytest.mark.parametrize("threads", [1, 15])
@@ -267,4 +298,4 @@ def test_index_reopen_connection(test_db, threads, monkeypatch, test_tokenizer):
     idx = indexer.Indexer('dbname=test_nominatim_python_unittest', test_tokenizer, threads)
     idx.index_by_rank(28, 30)
 
-    assert 0 == test_db.placex_unindexed()
+    assert test_db.placex_unindexed() == 0