1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for specialised conenction and cursor classes.
13 from nominatim.db.connection import connect, get_pg_env
17 with connect(dsn) as conn:
21 def test_connection_table_exists(db, table_factory):
22 assert not db.table_exists('foobar')
24 table_factory('foobar')
26 assert db.table_exists('foobar')
29 def test_connection_index_exists(db, table_factory, temp_db_cursor):
30 assert not db.index_exists('some_index')
32 table_factory('foobar')
33 temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
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')
40 def test_drop_table_existing(db, table_factory):
41 table_factory('dummy')
42 assert db.table_exists('dummy')
44 db.drop_table('dummy')
45 assert not db.table_exists('dummy')
48 def test_drop_table_non_existsing(db):
49 db.drop_table('dfkjgjriogjigjgjrdghehtre')
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)
56 def test_connection_server_version_tuple(db):
57 ver = db.server_version_tuple()
59 assert isinstance(ver, tuple)
64 def test_connection_postgis_version_tuple(db, temp_db_with_extensions):
65 ver = db.postgis_version_tuple()
67 assert isinstance(ver, tuple)
72 def test_cursor_scalar(db, table_factory):
73 table_factory('dummy')
75 with db.cursor() as cur:
76 assert cur.scalar('SELECT count(*) FROM dummy') == 0
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')
85 def test_cursor_scalar_no_rows(db, table_factory):
86 table_factory('dummy')
88 with db.cursor() as cur:
89 with pytest.raises(RuntimeError):
90 cur.scalar('SELECT id FROM dummy')
93 def test_get_pg_env_add_variable(monkeypatch):
94 monkeypatch.delenv('PGPASSWORD', raising=False)
95 env = get_pg_env('user=fooF')
97 assert env['PGUSER'] == 'fooF'
98 assert 'PGPASSWORD' not in env
101 def test_get_pg_env_overwrite_variable(monkeypatch):
102 monkeypatch.setenv('PGUSER', 'some default')
103 env = get_pg_env('user=overwriter')
105 assert env['PGUSER'] == 'overwriter'
108 def test_get_pg_env_ignore_unknown():
109 env = get_pg_env('client_encoding=stuff', base_env={})