1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for correctly assigning wikipedia pages to places.
15 from nominatim_db.tools.refresh import import_wikipedia_articles, recompute_importance, create_functions
18 def wiki_csv(tmp_path, sql_preprocessor):
20 with gzip.open(tmp_path / 'wikimedia-importance.csv.gz', mode='wt') as fd:
21 writer = csv.DictWriter(fd, fieldnames=['language', 'type', 'title',
22 'importance', 'wikidata_id'],
23 delimiter='\t', quotechar='|')
25 for lang, title, importance, wd in data:
26 writer.writerow({'language': lang, 'type': 'a',
27 'title': title, 'importance': str(importance),
34 @pytest.mark.parametrize('extra', [{'wikipedia:en': 'Test'},
35 {'wikipedia': 'en:Test'},
36 {'wikidata': 'Q123'}])
37 def test_wikipedia(dsn, temp_db_conn, temp_db_cursor, def_config, wiki_csv, placex_table, extra):
38 import_wikipedia_articles(dsn, wiki_csv([('en', 'Test', 0.3, 'Q123')]))
39 create_functions(temp_db_conn, def_config)
41 content = temp_db_cursor.row_set(
42 'SELECT language, title, importance, wikidata FROM wikimedia_importance')
43 assert content == set([('en', 'Test', 0.3, 'Q123')])
45 placex_table.add(osm_id=12, extratags=extra)
47 recompute_importance(temp_db_conn)
49 content = temp_db_cursor.row_set('SELECT wikipedia, importance FROM placex')
50 assert content == set([('en:Test', 0.3)])
53 def test_wikipedia_no_match(dsn, temp_db_conn, temp_db_cursor, def_config, wiki_csv,
55 import_wikipedia_articles(dsn, wiki_csv([('de', 'Test', 0.3, 'Q123')]))
56 create_functions(temp_db_conn, def_config)
58 placex_table.add(osm_id=12, extratags={'wikipedia': 'en:Test'}, rank_search=10)
60 recompute_importance(temp_db_conn)
62 content = temp_db_cursor.row_set('SELECT wikipedia, importance FROM placex')
63 assert list(content) == [(None, pytest.approx(0.26667666))]