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)
17 self.conn.set_isolation_level(0)
18 with self.conn.cursor() as cur:
19 cur.execute("""CREATE TABLE placex (place_id BIGINT,
22 rank_address SMALLINT,
24 indexed_status SMALLINT,
25 indexed_date TIMESTAMP,
28 geometry_sector INTEGER)""")
29 cur.execute("""CREATE TABLE location_property_osmline (
31 indexed_status SMALLINT,
32 indexed_date TIMESTAMP,
33 geometry_sector INTEGER)""")
34 cur.execute("""CREATE OR REPLACE FUNCTION date_update() RETURNS TRIGGER
37 IF NEW.indexed_status = 0 and OLD.indexed_status != 0 THEN
38 NEW.indexed_date = now();
41 END; $$ LANGUAGE plpgsql;""")
42 cur.execute("""CREATE TRIGGER placex_update BEFORE UPDATE ON placex
43 FOR EACH ROW EXECUTE PROCEDURE date_update()""")
44 cur.execute("""CREATE TRIGGER osmline_update BEFORE UPDATE ON location_property_osmline
45 FOR EACH ROW EXECUTE PROCEDURE date_update()""")
47 def scalar(self, query):
48 with self.conn.cursor() as cur:
50 return cur.fetchone()[0]
52 def add_place(self, cls='place', typ='locality',
53 rank_search=30, rank_address=30, sector=20):
54 next_id = next(self.placex_id)
55 with self.conn.cursor() as cur:
56 cur.execute("""INSERT INTO placex
57 (place_id, class, type, rank_search, rank_address,
58 indexed_status, geometry_sector)
59 VALUES (%s, %s, %s, %s, %s, 1, %s)""",
60 (next_id, cls, typ, rank_search, rank_address, sector))
63 def add_admin(self, **kwargs):
64 kwargs['cls'] = 'boundary'
65 kwargs['typ'] = 'administrative'
66 return self.add_place(**kwargs)
68 def add_osmline(self, sector=20):
69 next_id = next(self.osmline_id)
70 with self.conn.cursor() as cur:
71 cur.execute("""INSERT INTO location_property_osmline
72 (place_id, indexed_status, geometry_sector)
73 VALUES (%s, 1, %s)""",
77 def placex_unindexed(self):
78 return self.scalar('SELECT count(*) from placex where indexed_status > 0')
80 def osmline_unindexed(self):
81 return self.scalar('SELECT count(*) from location_property_osmline where indexed_status > 0')
85 def test_db(temp_db_conn):
86 yield IndexerTestDB(temp_db_conn)
89 @pytest.mark.parametrize("threads", [1, 15])
90 def test_index_full(test_db, threads):
91 for rank in range(31):
92 test_db.add_place(rank_address=rank, rank_search=rank)
95 assert 31 == test_db.placex_unindexed()
96 assert 1 == test_db.osmline_unindexed()
98 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
99 idx.index_by_rank(0, 30)
101 assert 0 == test_db.placex_unindexed()
102 assert 0 == test_db.osmline_unindexed()
104 assert 0 == test_db.scalar("""SELECT count(*) from placex
105 WHERE indexed_status = 0 and indexed_date is null""")
106 # ranks come in order of rank address
107 assert 0 == test_db.scalar("""
108 SELECT count(*) FROM placex p WHERE rank_address > 0
109 AND indexed_date >= (SELECT min(indexed_date) FROM placex o
110 WHERE p.rank_address < o.rank_address)""")
111 # placex rank < 30 objects come before interpolations
112 assert 0 == test_db.scalar(
113 """SELECT count(*) FROM placex WHERE rank_address < 30
114 AND indexed_date > (SELECT min(indexed_date) FROM location_property_osmline)""")
115 # placex rank = 30 objects come after interpolations
116 assert 0 == test_db.scalar(
117 """SELECT count(*) FROM placex WHERE rank_address = 30
118 AND indexed_date < (SELECT max(indexed_date) FROM location_property_osmline)""")
119 # rank 0 comes after rank 29 and before rank 30
120 assert 0 == test_db.scalar(
121 """SELECT count(*) FROM placex WHERE rank_address < 30
122 AND indexed_date > (SELECT min(indexed_date) FROM placex WHERE rank_address = 0)""")
123 assert 0 == test_db.scalar(
124 """SELECT count(*) FROM placex WHERE rank_address = 30
125 AND indexed_date < (SELECT max(indexed_date) FROM placex WHERE rank_address = 0)""")
128 @pytest.mark.parametrize("threads", [1, 15])
129 def test_index_partial_without_30(test_db, threads):
130 for rank in range(31):
131 test_db.add_place(rank_address=rank, rank_search=rank)
132 test_db.add_osmline()
134 assert 31 == test_db.placex_unindexed()
135 assert 1 == test_db.osmline_unindexed()
137 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
138 idx.index_by_rank(4, 15)
140 assert 19 == test_db.placex_unindexed()
141 assert 1 == test_db.osmline_unindexed()
143 assert 0 == test_db.scalar("""
144 SELECT count(*) FROM placex
145 WHERE indexed_status = 0 AND not rank_address between 4 and 15""")
148 @pytest.mark.parametrize("threads", [1, 15])
149 def test_index_partial_with_30(test_db, threads):
150 for rank in range(31):
151 test_db.add_place(rank_address=rank, rank_search=rank)
152 test_db.add_osmline()
154 assert 31 == test_db.placex_unindexed()
155 assert 1 == test_db.osmline_unindexed()
157 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
158 idx.index_by_rank(28, 30)
160 assert 27 == test_db.placex_unindexed()
161 assert 0 == test_db.osmline_unindexed()
163 assert 0 == test_db.scalar("""
164 SELECT count(*) FROM placex
165 WHERE indexed_status = 0 AND rank_address between 1 and 27""")
167 @pytest.mark.parametrize("threads", [1, 15])
168 def test_index_boundaries(test_db, threads):
169 for rank in range(4, 10):
170 test_db.add_admin(rank_address=rank, rank_search=rank)
171 for rank in range(31):
172 test_db.add_place(rank_address=rank, rank_search=rank)
173 test_db.add_osmline()
175 assert 37 == test_db.placex_unindexed()
176 assert 1 == test_db.osmline_unindexed()
178 idx = Indexer('dbname=test_nominatim_python_unittest', threads)
179 idx.index_boundaries(0, 30)
181 assert 31 == test_db.placex_unindexed()
182 assert 1 == test_db.osmline_unindexed()
184 assert 0 == test_db.scalar("""
185 SELECT count(*) FROM placex
186 WHERE indexed_status = 0 AND class != 'boundary'""")