]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh.py
c8ebdab8734eb94d0900811bd20d52ee28d1b3cb
[nominatim.git] / test / python / tools / test_refresh.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 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.tools import refresh
15
16 def test_refresh_import_wikipedia_not_existing(dsn):
17     assert refresh.import_wikipedia_articles(dsn, Path('.')) == 1
18
19
20 def test_refresh_import_osm_views_geotiff_not_existing(dsn):
21     assert refresh.import_osm_views_geotiff(dsn, Path('.')) == 1
22
23
24 @pytest.mark.parametrize("replace", (True, False))
25 def test_refresh_import_wikipedia(dsn, src_dir, table_factory, temp_db_cursor, replace):
26     if replace:
27         table_factory('wikipedia_article')
28         table_factory('wikipedia_redirect')
29
30     # use the small wikipedia file for the API testdb
31     assert refresh.import_wikipedia_articles(dsn, src_dir / 'test' / 'testdb') == 0
32
33     assert temp_db_cursor.table_rows('wikipedia_article') > 0
34     assert temp_db_cursor.table_rows('wikipedia_redirect') > 0
35
36
37 @pytest.mark.parametrize("replace", (True, False))
38 def test_refresh_import_osm_views_geotiff(dsn, src_dir, table_factory, temp_db_cursor, replace):
39     if replace:
40         table_factory('osmviews')
41
42     # use the small osm views GeoTIFF file for the API testdb
43     assert refresh.import_osm_views_geotiff(dsn, src_dir / 'test' / 'testdb') == 0
44
45     assert temp_db_cursor.table_rows('osmviews') > 0
46
47
48 def test_recompute_importance(placex_table, table_factory, temp_db_conn, temp_db_cursor):
49     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION compute_importance(extratags HSTORE,
50                                               country_code varchar(2),
51                                               osm_type varchar(1), osm_id BIGINT,
52                                               OUT importance FLOAT,
53                                               OUT wikipedia TEXT)
54                                AS $$ SELECT 0.1::float, 'foo'::text $$ LANGUAGE SQL""")
55
56     refresh.recompute_importance(temp_db_conn)
57
58
59 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
60 def test_invalidate_osm_object_simple(placex_table, osm_type, temp_db_conn, temp_db_cursor):
61     placex_table.add(osm_type=osm_type, osm_id=57283)
62
63     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn, recursive=False)
64     temp_db_conn.commit()
65
66     assert 2 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
67                                          WHERE osm_type = %s and osm_id = %s""",
68                                       (osm_type, 57283))
69
70
71 def test_invalidate_osm_object_nonexisting_simple(placex_table, temp_db_conn, temp_db_cursor):
72     placex_table.add(osm_type='W', osm_id=57283)
73
74     refresh.invalidate_osm_object('N', 57283, temp_db_conn, recursive=False)
75     temp_db_conn.commit()
76
77     assert 0 == temp_db_cursor.scalar("""SELECT count(*) FROM placex
78                                          WHERE indexed_status > 0""")
79
80
81 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
82 def test_invalidate_osm_object_recursive(placex_table, osm_type, temp_db_conn, temp_db_cursor):
83     placex_table.add(osm_type=osm_type, osm_id=57283)
84
85     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
86                               RETURNS BOOLEAN AS $$
87                               BEGIN
88                                 UPDATE placex SET indexed_status = 522
89                                 WHERE place_id = placeid;
90                                 RETURN TRUE;
91                               END;
92                               $$
93                               LANGUAGE plpgsql;""")
94
95     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn)
96     temp_db_conn.commit()
97
98     assert 522 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
99                                            WHERE osm_type = %s and osm_id = %s""",
100                                         (osm_type, 57283))