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