]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_db_connection.py
return housenumbers always from address field
[nominatim.git] / test / python / test_db_connection.py
1 """
2 Tests for specialised conenction and cursor classes.
3 """
4 import pytest
5 import psycopg2
6
7 from nominatim.db.connection import connect, get_pg_env
8
9 @pytest.fixture
10 def db(temp_db):
11     with connect('dbname=' + temp_db) as conn:
12         yield conn
13
14
15 def test_connection_table_exists(db, table_factory):
16     assert db.table_exists('foobar') == False
17
18     table_factory('foobar')
19
20     assert db.table_exists('foobar') == True
21
22
23 def test_connection_index_exists(db, temp_db_cursor):
24     assert db.index_exists('some_index') == False
25
26     temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
27     temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
28
29     assert db.index_exists('some_index') == True
30     assert db.index_exists('some_index', table='foobar') == True
31     assert db.index_exists('some_index', table='bar') == False
32
33
34 def test_drop_table_existing(db, table_factory):
35     table_factory('dummy')
36     assert db.table_exists('dummy')
37
38     db.drop_table('dummy')
39     assert not db.table_exists('dummy')
40
41
42 def test_drop_table_non_existsing(db):
43     db.drop_table('dfkjgjriogjigjgjrdghehtre')
44
45
46 def test_drop_table_non_existing_force(db):
47     with pytest.raises(psycopg2.ProgrammingError, match='.*does not exist.*'):
48         db.drop_table('dfkjgjriogjigjgjrdghehtre', if_exists=False)
49
50 def test_connection_server_version_tuple(db):
51     ver = db.server_version_tuple()
52
53     assert isinstance(ver, tuple)
54     assert len(ver) == 2
55     assert ver[0] > 8
56
57
58 def test_connection_postgis_version_tuple(db, temp_db_cursor):
59     temp_db_cursor.execute('CREATE EXTENSION postgis')
60
61     ver = db.postgis_version_tuple()
62
63     assert isinstance(ver, tuple)
64     assert len(ver) == 2
65     assert ver[0] >= 2
66
67
68 def test_cursor_scalar(db, table_factory):
69     table_factory('dummy')
70
71     with db.cursor() as cur:
72         assert cur.scalar('SELECT count(*) FROM dummy') == 0
73
74
75 def test_cursor_scalar_many_rows(db):
76     with db.cursor() as cur:
77         with pytest.raises(RuntimeError):
78             cur.scalar('SELECT * FROM pg_tables')
79
80
81 def test_cursor_scalar_no_rows(db, table_factory):
82     table_factory('dummy')
83
84     with db.cursor() as cur:
85         with pytest.raises(RuntimeError):
86             cur.scalar('SELECT id FROM dummy')
87
88
89 def test_get_pg_env_add_variable(monkeypatch):
90     monkeypatch.delenv('PGPASSWORD', raising=False)
91     env = get_pg_env('user=fooF')
92
93     assert env['PGUSER'] == 'fooF'
94     assert 'PGPASSWORD' not in env
95
96
97 def test_get_pg_env_overwrite_variable(monkeypatch):
98     monkeypatch.setenv('PGUSER', 'some default')
99     env = get_pg_env('user=overwriter')
100
101     assert env['PGUSER'] == 'overwriter'
102
103
104 def test_get_pg_env_ignore_unknown():
105     env = get_pg_env('tty=stuff', base_env={})
106
107     assert env == {}