]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_config.py
move SearchDescription building into tokens
[nominatim.git] / test / python / test_config.py
1 """
2 Test for loading dotenv configuration.
3 """
4 import pytest
5
6 from nominatim.config import Configuration
7 from nominatim.errors import UsageError
8
9 @pytest.fixture
10 def make_config(src_dir):
11     """ Create a configuration object from the given project directory.
12     """
13     def _mk_config(project_dir=None):
14         return Configuration(project_dir, src_dir / 'settings')
15
16     return _mk_config
17
18
19 def test_no_project_dir(make_config):
20     config = make_config()
21
22     assert config.DATABASE_WEBUSER == 'www-data'
23
24
25 @pytest.mark.parametrize("val", ('apache', '"apache"'))
26 def test_prefer_project_setting_over_default(make_config, val, tmp_path):
27     envfile = tmp_path / '.env'
28     envfile.write_text('NOMINATIM_DATABASE_WEBUSER={}\n'.format(val))
29
30     config = make_config(tmp_path)
31
32     assert config.DATABASE_WEBUSER == 'apache'
33
34
35 def test_prefer_os_environ_over_project_setting(make_config, monkeypatch, tmp_path):
36     envfile = tmp_path / '.env'
37     envfile.write_text('NOMINATIM_DATABASE_WEBUSER=apache\n')
38
39     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
40
41     config = make_config(tmp_path)
42
43     assert config.DATABASE_WEBUSER == 'nobody'
44
45
46 def test_get_os_env_add_defaults(make_config, monkeypatch):
47     config = make_config()
48
49     monkeypatch.delenv('NOMINATIM_DATABASE_WEBUSER', raising=False)
50
51     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'www-data'
52
53
54 def test_get_os_env_prefer_os_environ(make_config, monkeypatch):
55     config = make_config()
56
57     monkeypatch.setenv('NOMINATIM_DATABASE_WEBUSER', 'nobody')
58
59     assert config.get_os_env()['NOMINATIM_DATABASE_WEBUSER'] == 'nobody'
60
61
62 def test_get_libpq_dsn_convert_default(make_config):
63     config = make_config()
64
65     assert config.get_libpq_dsn() == 'dbname=nominatim'
66
67
68 def test_get_libpq_dsn_convert_php(make_config, monkeypatch):
69     config = make_config()
70
71     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
72                        'pgsql:dbname=gis;password=foo;host=localhost')
73
74     assert config.get_libpq_dsn() == 'dbname=gis password=foo host=localhost'
75
76
77 @pytest.mark.parametrize("val,expect", [('foo bar', "'foo bar'"),
78                                         ("xy'z", "xy\\'z"),
79                                        ])
80 def test_get_libpq_dsn_convert_php_special_chars(make_config, monkeypatch, val, expect):
81     config = make_config()
82
83     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
84                        'pgsql:dbname=gis;password={}'.format(val))
85
86     assert config.get_libpq_dsn() == "dbname=gis password={}".format(expect)
87
88
89 def test_get_libpq_dsn_convert_libpq(make_config, monkeypatch):
90     config = make_config()
91
92     monkeypatch.setenv('NOMINATIM_DATABASE_DSN',
93                        'host=localhost dbname=gis password=foo')
94
95     assert config.get_libpq_dsn() == 'host=localhost dbname=gis password=foo'
96
97
98 @pytest.mark.parametrize("value,result",
99                          [(x, True) for x in ('1', 'true', 'True', 'yes', 'YES')] +
100                          [(x, False) for x in ('0', 'false', 'no', 'NO', 'x')])
101 def test_get_bool(make_config, monkeypatch, value, result):
102     config = make_config()
103
104     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
105
106     assert config.get_bool('FOOBAR') == result
107
108 def test_get_bool_empty(make_config):
109     config = make_config()
110
111     assert config.DATABASE_MODULE_PATH == ''
112     assert not config.get_bool('DATABASE_MODULE_PATH')
113
114
115 @pytest.mark.parametrize("value,result", [('0', 0), ('1', 1),
116                                           ('85762513444', 85762513444)])
117 def test_get_int_success(make_config, monkeypatch, value, result):
118     config = make_config()
119
120     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
121
122     assert config.get_int('FOOBAR') == result
123
124
125 @pytest.mark.parametrize("value", ['1b', 'fg', '0x23'])
126 def test_get_int_bad_values(make_config, monkeypatch, value):
127     config = make_config()
128
129     monkeypatch.setenv('NOMINATIM_FOOBAR', value)
130
131     with pytest.raises(UsageError):
132         config.get_int('FOOBAR')
133
134
135 def test_get_int_empty(make_config):
136     config = make_config()
137
138     assert config.DATABASE_MODULE_PATH == ''
139
140     with pytest.raises(UsageError):
141         config.get_int('DATABASE_MODULE_PATH')
142
143
144 def test_get_import_style_intern(make_config, src_dir, monkeypatch):
145     config = make_config()
146
147     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', 'street')
148
149     expected = src_dir / 'settings' / 'import-street.style'
150
151     assert config.get_import_style_file() == expected
152
153
154 @pytest.mark.parametrize("value", ['custom', '/foo/bar.stye'])
155 def test_get_import_style_extern(make_config, monkeypatch, value):
156     config = make_config()
157
158     monkeypatch.setenv('NOMINATIM_IMPORT_STYLE', value)
159
160     assert str(config.get_import_style_file()) == value