2022-01-03 18:23:58 +03:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
|
|
|
# Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
# For a full list of authors see the git log.
|
2021-01-24 16:35:35 +03:00
|
|
|
"""
|
|
|
|
Tests for specialised conenction and cursor classes.
|
|
|
|
"""
|
|
|
|
import pytest
|
2021-02-24 19:21:45 +03:00
|
|
|
import psycopg2
|
2021-01-24 16:35:35 +03:00
|
|
|
|
2021-02-23 16:11:11 +03:00
|
|
|
from nominatim.db.connection import connect, get_pg_env
|
2021-01-24 16:35:35 +03:00
|
|
|
|
|
|
|
@pytest.fixture
|
2021-05-20 00:07:39 +03:00
|
|
|
def db(dsn):
|
|
|
|
with connect(dsn) as conn:
|
2021-02-23 12:11:21 +03:00
|
|
|
yield conn
|
2021-01-24 16:35:35 +03:00
|
|
|
|
|
|
|
|
2021-02-25 00:02:13 +03:00
|
|
|
def test_connection_table_exists(db, table_factory):
|
2021-05-20 00:07:39 +03:00
|
|
|
assert not db.table_exists('foobar')
|
2021-01-24 16:35:35 +03:00
|
|
|
|
2021-02-25 00:02:13 +03:00
|
|
|
table_factory('foobar')
|
2021-01-24 16:35:35 +03:00
|
|
|
|
2021-05-20 00:07:39 +03:00
|
|
|
assert db.table_exists('foobar')
|
2021-01-24 16:35:35 +03:00
|
|
|
|
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
def test_connection_index_exists(db, table_factory, temp_db_cursor):
|
2021-05-20 00:07:39 +03:00
|
|
|
assert not db.index_exists('some_index')
|
2021-02-18 22:36:11 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory('foobar')
|
2021-02-18 22:36:11 +03:00
|
|
|
temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
|
|
|
|
|
2021-05-20 00:07:39 +03:00
|
|
|
assert db.index_exists('some_index')
|
|
|
|
assert db.index_exists('some_index', table='foobar')
|
|
|
|
assert not db.index_exists('some_index', table='bar')
|
2021-02-18 22:36:11 +03:00
|
|
|
|
|
|
|
|
2021-02-25 00:02:13 +03:00
|
|
|
def test_drop_table_existing(db, table_factory):
|
|
|
|
table_factory('dummy')
|
2021-02-24 19:21:45 +03:00
|
|
|
assert db.table_exists('dummy')
|
2021-02-25 00:02:13 +03:00
|
|
|
|
2021-02-24 19:21:45 +03:00
|
|
|
db.drop_table('dummy')
|
|
|
|
assert not db.table_exists('dummy')
|
|
|
|
|
|
|
|
|
|
|
|
def test_drop_table_non_existsing(db):
|
|
|
|
db.drop_table('dfkjgjriogjigjgjrdghehtre')
|
|
|
|
|
|
|
|
|
|
|
|
def test_drop_table_non_existing_force(db):
|
|
|
|
with pytest.raises(psycopg2.ProgrammingError, match='.*does not exist.*'):
|
|
|
|
db.drop_table('dfkjgjriogjigjgjrdghehtre', if_exists=False)
|
|
|
|
|
2021-02-18 22:36:11 +03:00
|
|
|
def test_connection_server_version_tuple(db):
|
|
|
|
ver = db.server_version_tuple()
|
|
|
|
|
|
|
|
assert isinstance(ver, tuple)
|
|
|
|
assert len(ver) == 2
|
|
|
|
assert ver[0] > 8
|
|
|
|
|
2021-02-24 00:50:23 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
def test_connection_postgis_version_tuple(db, temp_db_with_extensions):
|
2021-02-24 00:50:23 +03:00
|
|
|
ver = db.postgis_version_tuple()
|
|
|
|
|
|
|
|
assert isinstance(ver, tuple)
|
|
|
|
assert len(ver) == 2
|
|
|
|
assert ver[0] >= 2
|
|
|
|
|
|
|
|
|
2021-02-25 00:02:13 +03:00
|
|
|
def test_cursor_scalar(db, table_factory):
|
|
|
|
table_factory('dummy')
|
2021-01-24 16:35:35 +03:00
|
|
|
|
|
|
|
with db.cursor() as cur:
|
|
|
|
assert cur.scalar('SELECT count(*) FROM dummy') == 0
|
|
|
|
|
2021-02-18 22:36:11 +03:00
|
|
|
|
2021-01-24 16:35:35 +03:00
|
|
|
def test_cursor_scalar_many_rows(db):
|
|
|
|
with db.cursor() as cur:
|
2021-01-30 18:20:10 +03:00
|
|
|
with pytest.raises(RuntimeError):
|
2021-01-24 16:35:35 +03:00
|
|
|
cur.scalar('SELECT * FROM pg_tables')
|
2021-02-23 16:11:11 +03:00
|
|
|
|
|
|
|
|
2021-03-01 22:35:15 +03:00
|
|
|
def test_cursor_scalar_no_rows(db, table_factory):
|
|
|
|
table_factory('dummy')
|
|
|
|
|
|
|
|
with db.cursor() as cur:
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
cur.scalar('SELECT id FROM dummy')
|
|
|
|
|
|
|
|
|
2021-02-23 16:11:11 +03:00
|
|
|
def test_get_pg_env_add_variable(monkeypatch):
|
|
|
|
monkeypatch.delenv('PGPASSWORD', raising=False)
|
|
|
|
env = get_pg_env('user=fooF')
|
|
|
|
|
|
|
|
assert env['PGUSER'] == 'fooF'
|
|
|
|
assert 'PGPASSWORD' not in env
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_pg_env_overwrite_variable(monkeypatch):
|
|
|
|
monkeypatch.setenv('PGUSER', 'some default')
|
|
|
|
env = get_pg_env('user=overwriter')
|
|
|
|
|
|
|
|
assert env['PGUSER'] == 'overwriter'
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_pg_env_ignore_unknown():
|
2021-10-01 11:51:41 +03:00
|
|
|
env = get_pg_env('client_encoding=stuff', base_env={})
|
2021-02-23 16:11:11 +03:00
|
|
|
|
|
|
|
assert env == {}
|