1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2023 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Test functions for adapting results to the user's locale.
12 from nominatim.api import Locales
14 def test_display_name_empty_names():
15 l = Locales(['en', 'de'])
17 assert l.display_name(None) == ''
18 assert l.display_name({}) == ''
20 def test_display_name_none_localized():
23 assert l.display_name({}) == ''
24 assert l.display_name({'name:de': 'DE', 'name': 'ALL'}) == 'ALL'
25 assert l.display_name({'ref': '34', 'name:de': 'DE'}) == '34'
28 def test_display_name_localized():
29 l = Locales(['en', 'de'])
31 assert l.display_name({}) == ''
32 assert l.display_name({'name:de': 'DE', 'name': 'ALL'}) == 'DE'
33 assert l.display_name({'ref': '34', 'name:de': 'DE'}) == 'DE'
36 def test_display_name_preference():
37 l = Locales(['en', 'de'])
39 assert l.display_name({}) == ''
40 assert l.display_name({'name:de': 'DE', 'name:en': 'EN'}) == 'EN'
41 assert l.display_name({'official_name:en': 'EN', 'name:de': 'DE'}) == 'DE'
44 @pytest.mark.parametrize('langstr,langlist',
46 ('fr-FR', ['fr-FR', 'fr']),
47 ('de,fr-FR', ['de', 'fr-FR', 'fr']),
48 ('fr,de,fr-FR', ['fr', 'de', 'fr-FR']),
49 ('en;q=0.5,fr', ['fr', 'en']),
50 ('en;q=0.5,fr,en-US', ['fr', 'en-US', 'en']),
51 ('en,fr;garbage,de', ['en', 'de'])])
52 def test_from_language_preferences(langstr, langlist):
53 assert Locales.from_accept_languages(langstr).languages == langlist