]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh.py
adapt unit tests to changed function names
[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_secondary_importance_non_existing(dsn):
21     assert refresh.import_secondary_importance(dsn, Path('.')) == 1
22
23 def test_refresh_import_secondary_importance_testdb(dsn, src_dir, temp_db_conn, temp_db_cursor):
24     temp_db_cursor.execute('CREATE EXTENSION postgis; CREATE EXTENSION postgis_raster')
25     assert refresh.import_secondary_importance(dsn, src_dir / 'test' / 'testdb') == 0
26
27     assert temp_db_conn.table_exists('secondary_importance')
28
29
30 @pytest.mark.parametrize("replace", (True, False))
31 def test_refresh_import_wikipedia(dsn, src_dir, table_factory, temp_db_cursor, replace):
32     if replace:
33         table_factory('wikipedia_article')
34         table_factory('wikipedia_redirect')
35
36     # use the small wikipedia file for the API testdb
37     assert refresh.import_wikipedia_articles(dsn, src_dir / 'test' / 'testdb') == 0
38
39     assert temp_db_cursor.table_rows('wikipedia_article') > 0
40     assert temp_db_cursor.table_rows('wikipedia_redirect') > 0
41
42
43 def test_recompute_importance(placex_table, table_factory, temp_db_conn, temp_db_cursor):
44     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION compute_importance(extratags HSTORE,
45                                               country_code varchar(2),
46                                               osm_type varchar(1), osm_id BIGINT,
47                                               centroid GEOMETRY,
48                                               OUT importance FLOAT,
49                                               OUT wikipedia TEXT)
50                                AS $$ SELECT 0.1::float, 'foo'::text $$ LANGUAGE SQL""")
51
52     refresh.recompute_importance(temp_db_conn)
53
54
55 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
56 def test_invalidate_osm_object_simple(placex_table, osm_type, temp_db_conn, temp_db_cursor):
57     placex_table.add(osm_type=osm_type, osm_id=57283)
58
59     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn, recursive=False)
60     temp_db_conn.commit()
61
62     assert 2 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
63                                          WHERE osm_type = %s and osm_id = %s""",
64                                       (osm_type, 57283))
65
66
67 def test_invalidate_osm_object_nonexisting_simple(placex_table, temp_db_conn, temp_db_cursor):
68     placex_table.add(osm_type='W', osm_id=57283)
69
70     refresh.invalidate_osm_object('N', 57283, temp_db_conn, recursive=False)
71     temp_db_conn.commit()
72
73     assert 0 == temp_db_cursor.scalar("""SELECT count(*) FROM placex
74                                          WHERE indexed_status > 0""")
75
76
77 @pytest.mark.parametrize('osm_type', ('N', 'W', 'R'))
78 def test_invalidate_osm_object_recursive(placex_table, osm_type, temp_db_conn, temp_db_cursor):
79     placex_table.add(osm_type=osm_type, osm_id=57283)
80
81     temp_db_cursor.execute("""CREATE OR REPLACE FUNCTION place_force_update(placeid BIGINT)
82                               RETURNS BOOLEAN AS $$
83                               BEGIN
84                                 UPDATE placex SET indexed_status = 522
85                                 WHERE place_id = placeid;
86                                 RETURN TRUE;
87                               END;
88                               $$
89                               LANGUAGE plpgsql;""")
90
91     refresh.invalidate_osm_object(osm_type, 57283, temp_db_conn)
92     temp_db_conn.commit()
93
94     assert 522 == temp_db_cursor.scalar("""SELECT indexed_status FROM placex
95                                            WHERE osm_type = %s and osm_id = %s""",
96                                         (osm_type, 57283))