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_has_column_no_table(db):
 
  30     assert not db.table_has_column('sometable', 'somecolumn')
 
  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')
 
  37     assert db.table_has_column('stuff', name) == result
 
  39 def test_connection_index_exists(db, table_factory, temp_db_cursor):
 
  40     assert not db.index_exists('some_index')
 
  42     table_factory('foobar')
 
  43     temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
 
  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')
 
  50 def test_drop_table_existing(db, table_factory):
 
  51     table_factory('dummy')
 
  52     assert db.table_exists('dummy')
 
  54     db.drop_table('dummy')
 
  55     assert not db.table_exists('dummy')
 
  58 def test_drop_table_non_existsing(db):
 
  59     db.drop_table('dfkjgjriogjigjgjrdghehtre')
 
  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)
 
  66 def test_connection_server_version_tuple(db):
 
  67     ver = db.server_version_tuple()
 
  69     assert isinstance(ver, tuple)
 
  74 def test_connection_postgis_version_tuple(db, temp_db_with_extensions):
 
  75     ver = db.postgis_version_tuple()
 
  77     assert isinstance(ver, tuple)
 
  82 def test_cursor_scalar(db, table_factory):
 
  83     table_factory('dummy')
 
  85     with db.cursor() as cur:
 
  86         assert cur.scalar('SELECT count(*) FROM dummy') == 0
 
  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')
 
  95 def test_cursor_scalar_no_rows(db, table_factory):
 
  96     table_factory('dummy')
 
  98     with db.cursor() as cur:
 
  99         with pytest.raises(RuntimeError):
 
 100             cur.scalar('SELECT id FROM dummy')
 
 103 def test_get_pg_env_add_variable(monkeypatch):
 
 104     monkeypatch.delenv('PGPASSWORD', raising=False)
 
 105     env = get_pg_env('user=fooF')
 
 107     assert env['PGUSER'] == 'fooF'
 
 108     assert 'PGPASSWORD' not in env
 
 111 def test_get_pg_env_overwrite_variable(monkeypatch):
 
 112     monkeypatch.setenv('PGUSER', 'some default')
 
 113     env = get_pg_env('user=overwriter')
 
 115     assert env['PGUSER'] == 'overwriter'
 
 118 def test_get_pg_env_ignore_unknown():
 
 119     env = get_pg_env('client_encoding=stuff', base_env={})