]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_db_connection.py
Merge pull request #2401 from lonvia/port-add-data-to-python
[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(dsn):
11     with connect(dsn) as conn:
12         yield conn
13
14
15 def test_connection_table_exists(db, table_factory):
16     assert not db.table_exists('foobar')
17
18     table_factory('foobar')
19
20     assert db.table_exists('foobar')
21
22
23 def test_connection_index_exists(db, table_factory, temp_db_cursor):
24     assert not db.index_exists('some_index')
25
26     table_factory('foobar')
27     temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
28
29     assert db.index_exists('some_index')
30     assert db.index_exists('some_index', table='foobar')
31     assert not db.index_exists('some_index', table='bar')
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_with_extensions):
59     ver = db.postgis_version_tuple()
60
61     assert isinstance(ver, tuple)
62     assert len(ver) == 2
63     assert ver[0] >= 2
64
65
66 def test_cursor_scalar(db, table_factory):
67     table_factory('dummy')
68
69     with db.cursor() as cur:
70         assert cur.scalar('SELECT count(*) FROM dummy') == 0
71
72
73 def test_cursor_scalar_many_rows(db):
74     with db.cursor() as cur:
75         with pytest.raises(RuntimeError):
76             cur.scalar('SELECT * FROM pg_tables')
77
78
79 def test_cursor_scalar_no_rows(db, table_factory):
80     table_factory('dummy')
81
82     with db.cursor() as cur:
83         with pytest.raises(RuntimeError):
84             cur.scalar('SELECT id FROM dummy')
85
86
87 def test_get_pg_env_add_variable(monkeypatch):
88     monkeypatch.delenv('PGPASSWORD', raising=False)
89     env = get_pg_env('user=fooF')
90
91     assert env['PGUSER'] == 'fooF'
92     assert 'PGPASSWORD' not in env
93
94
95 def test_get_pg_env_overwrite_variable(monkeypatch):
96     monkeypatch.setenv('PGUSER', 'some default')
97     env = get_pg_env('user=overwriter')
98
99     assert env['PGUSER'] == 'overwriter'
100
101
102 def test_get_pg_env_ignore_unknown():
103     env = get_pg_env('tty=stuff', base_env={})
104
105     assert env == {}