]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / tools / test_refresh.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Test for various refresh functions.
9 """
10 from pathlib import Path
11
12 import pytest
13
14 from nominatim_db.tools import refresh
15
16
17 def test_refresh_import_wikipedia_not_existing(dsn):
18     assert refresh.import_wikipedia_articles(dsn, Path('.')) == 1
19
20
21 def test_refresh_import_secondary_importance_non_existing(dsn):
22     assert refresh.import_secondary_importance(dsn, Path('.')) == 1
23
24
25 def test_refresh_import_secondary_importance_testdb(dsn, src_dir, temp_db_conn, temp_db_cursor):
26     temp_db_cursor.execute('CREATE EXTENSION postgis')
27     temp_db_cursor.execute('CREATE EXTENSION postgis_raster')
28     assert refresh.import_secondary_importance(dsn, src_dir / 'test' / 'testdb') == 0
29
30     assert temp_db_cursor.table_exists('secondary_importance')
31
32
33 @pytest.mark.parametrize("replace", (True, False))
34 def test_refresh_import_wikipedia(dsn, src_dir, table_factory, temp_db_cursor, replace):
35     if replace:
36         table_factory('wikimedia_importance')
37
38     # use the small wikipedia file for the API testdb
39     assert refresh.import_wikipedia_articles(dsn, src_dir / 'test' / 'testdb') == 0
40
41     assert temp_db_cursor.table_rows('wikimedia_importance') > 0
42
43
44 def test_recompute_importance(placex_table, table_factory, temp_db_conn, temp_db_cursor):
45     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION compute_importance(extratags HSTORE,
46                                               country_code varchar(2),
47                                               rank_search SMALLINT,
48                                               centroid GEOMETRY,
49                                               OUT importance FLOAT,
50                                               OUT wikipedia TEXT)
51                                AS $$ SELECT 0.1::float, 'foo'::text $$ LANGUAGE SQL""")
52
53     refresh.recompute_importance(temp_db_conn)
54
55
56 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
57 def test_invalidate_osm_object_simple(placex_table, osm_type, temp_db_conn, temp_db_cursor):
58     placex_table.add(osm_type=osm_type, osm_id=57283)
59
60     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn, recursive=False)
61     temp_db_conn.commit()
62
63     assert 2 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
64                                          WHERE osm_type = %s and osm_id = %s""",
65                                       (osm_type, 57283))
66
67
68 def test_invalidate_osm_object_nonexisting_simple(placex_table, temp_db_conn, temp_db_cursor):
69     placex_table.add(osm_type='W', osm_id=57283)
70
71     refresh.invalidate_osm_object('N', 57283, temp_db_conn, recursive=False)
72     temp_db_conn.commit()
73
74     assert 0 == temp_db_cursor.scalar("""SELECT count(*) FROM placex
75                                          WHERE indexed_status > 0""")
76
77
78 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
79 def test_invalidate_osm_object_recursive(placex_table, osm_type, temp_db_conn, temp_db_cursor):
80     placex_table.add(osm_type=osm_type, osm_id=57283)
81
82     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
83                               RETURNS BOOLEAN AS $$
84                               BEGIN
85                                 UPDATE placex SET indexed_status = 522
86                                 WHERE place_id = placeid;
87                                 RETURN TRUE;
88                               END;
89                               $$
90                               LANGUAGE plpgsql;""")
91
92     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn)
93     temp_db_conn.commit()
94
95     assert 522 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
96                                            WHERE osm_type = %s and osm_id = %s""",
97                                         (osm_type, 57283))