]> git.openstreetmap.org Git - nominatim.git/blob - test/python/db/test_connection.py
Merge pull request #2770 from lonvia/typed-python
[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_has_column_no_table(db):
30     assert not db.table_has_column('sometable', 'somecolumn')
31
32
33 @pytest.mark.parametrize('name,result', [('tram', True), ('car', False)])
34 def test_has_column(db, table_factory, name, result):
35     table_factory('stuff', 'tram TEXT')
36
37     assert db.table_has_column('stuff', name) == result
38
39 def test_connection_index_exists(db, table_factory, temp_db_cursor):
40     assert not db.index_exists('some_index')
41
42     table_factory('foobar')
43     temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
44
45     assert db.index_exists('some_index')
46     assert db.index_exists('some_index', table='foobar')
47     assert not db.index_exists('some_index', table='bar')
48
49
50 def test_drop_table_existing(db, table_factory):
51     table_factory('dummy')
52     assert db.table_exists('dummy')
53
54     db.drop_table('dummy')
55     assert not db.table_exists('dummy')
56
57
58 def test_drop_table_non_existsing(db):
59     db.drop_table('dfkjgjriogjigjgjrdghehtre')
60
61
62 def test_drop_table_non_existing_force(db):
63     with pytest.raises(psycopg2.ProgrammingError, match='.*does not exist.*'):
64         db.drop_table('dfkjgjriogjigjgjrdghehtre', if_exists=False)
65
66 def test_connection_server_version_tuple(db):
67     ver = db.server_version_tuple()
68
69     assert isinstance(ver, tuple)
70     assert len(ver) == 2
71     assert ver[0] > 8
72
73
74 def test_connection_postgis_version_tuple(db, temp_db_with_extensions):
75     ver = db.postgis_version_tuple()
76
77     assert isinstance(ver, tuple)
78     assert len(ver) == 2
79     assert ver[0] >= 2
80
81
82 def test_cursor_scalar(db, table_factory):
83     table_factory('dummy')
84
85     with db.cursor() as cur:
86         assert cur.scalar('SELECT count(*) FROM dummy') == 0
87
88
89 def test_cursor_scalar_many_rows(db):
90     with db.cursor() as cur:
91         with pytest.raises(RuntimeError):
92             cur.scalar('SELECT * FROM pg_tables')
93
94
95 def test_cursor_scalar_no_rows(db, table_factory):
96     table_factory('dummy')
97
98     with db.cursor() as cur:
99         with pytest.raises(RuntimeError):
100             cur.scalar('SELECT id FROM dummy')
101
102
103 def test_get_pg_env_add_variable(monkeypatch):
104     monkeypatch.delenv('PGPASSWORD', raising=False)
105     env = get_pg_env('user=fooF')
106
107     assert env['PGUSER'] == 'fooF'
108     assert 'PGPASSWORD' not in env
109
110
111 def test_get_pg_env_overwrite_variable(monkeypatch):
112     monkeypatch.setenv('PGUSER', 'some default')
113     env = get_pg_env('user=overwriter')
114
115     assert env['PGUSER'] == 'overwriter'
116
117
118 def test_get_pg_env_ignore_unknown():
119     env = get_pg_env('client_encoding=stuff', base_env={})
120
121     assert env == {}