]> git.openstreetmap.org Git - nominatim.git/blob - test/python/db/test_connection.py
Revert the removal of new line at the end of the file
[nominatim.git] / test / python / db / test_connection.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for specialised conenction and cursor classes.
9 """
10 import pytest
11 import psycopg2
12
13 from nominatim.db.connection import connect, get_pg_env
14
15 @pytest.fixture
16 def db(dsn):
17     with connect(dsn) as conn:
18         yield conn
19
20
21 def test_connection_table_exists(db, table_factory):
22     assert not db.table_exists('foobar')
23
24     table_factory('foobar')
25
26     assert db.table_exists('foobar')
27
28
29 def test_connection_index_exists(db, table_factory, temp_db_cursor):
30     assert not db.index_exists('some_index')
31
32     table_factory('foobar')
33     temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
34
35     assert db.index_exists('some_index')
36     assert db.index_exists('some_index', table='foobar')
37     assert not db.index_exists('some_index', table='bar')
38
39
40 def test_drop_table_existing(db, table_factory):
41     table_factory('dummy')
42     assert db.table_exists('dummy')
43
44     db.drop_table('dummy')
45     assert not db.table_exists('dummy')
46
47
48 def test_drop_table_non_existsing(db):
49     db.drop_table('dfkjgjriogjigjgjrdghehtre')
50
51
52 def test_drop_table_non_existing_force(db):
53     with pytest.raises(psycopg2.ProgrammingError, match='.*does not exist.*'):
54         db.drop_table('dfkjgjriogjigjgjrdghehtre', if_exists=False)
55
56 def test_connection_server_version_tuple(db):
57     ver = db.server_version_tuple()
58
59     assert isinstance(ver, tuple)
60     assert len(ver) == 2
61     assert ver[0] > 8
62
63
64 def test_connection_postgis_version_tuple(db, temp_db_with_extensions):
65     ver = db.postgis_version_tuple()
66
67     assert isinstance(ver, tuple)
68     assert len(ver) == 2
69     assert ver[0] >= 2
70
71
72 def test_cursor_scalar(db, table_factory):
73     table_factory('dummy')
74
75     with db.cursor() as cur:
76         assert cur.scalar('SELECT count(*) FROM dummy') == 0
77
78
79 def test_cursor_scalar_many_rows(db):
80     with db.cursor() as cur:
81         with pytest.raises(RuntimeError):
82             cur.scalar('SELECT * FROM pg_tables')
83
84
85 def test_cursor_scalar_no_rows(db, table_factory):
86     table_factory('dummy')
87
88     with db.cursor() as cur:
89         with pytest.raises(RuntimeError):
90             cur.scalar('SELECT id FROM dummy')
91
92
93 def test_get_pg_env_add_variable(monkeypatch):
94     monkeypatch.delenv('PGPASSWORD', raising=False)
95     env = get_pg_env('user=fooF')
96
97     assert env['PGUSER'] == 'fooF'
98     assert 'PGPASSWORD' not in env
99
100
101 def test_get_pg_env_overwrite_variable(monkeypatch):
102     monkeypatch.setenv('PGUSER', 'some default')
103     env = get_pg_env('user=overwriter')
104
105     assert env['PGUSER'] == 'overwriter'
106
107
108 def test_get_pg_env_ignore_unknown():
109     env = get_pg_env('client_encoding=stuff', base_env={})
110
111     assert env == {}