2 Tests for specialised conenction and cursor classes.
6 from nominatim.db.connection import connect, get_pg_env
10 with connect('dbname=' + temp_db) as conn:
14 def test_connection_table_exists(db, temp_db_cursor):
15 assert db.table_exists('foobar') == False
17 temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
19 assert db.table_exists('foobar') == True
22 def test_connection_index_exists(db, temp_db_cursor):
23 assert db.index_exists('some_index') == False
25 temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
26 temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
28 assert db.index_exists('some_index') == True
29 assert db.index_exists('some_index', table='foobar') == True
30 assert db.index_exists('some_index', table='bar') == False
33 def test_connection_server_version_tuple(db):
34 ver = db.server_version_tuple()
36 assert isinstance(ver, tuple)
40 def test_cursor_scalar(db, temp_db_cursor):
41 temp_db_cursor.execute('CREATE TABLE dummy (id INT)')
43 with db.cursor() as cur:
44 assert cur.scalar('SELECT count(*) FROM dummy') == 0
47 def test_cursor_scalar_many_rows(db):
48 with db.cursor() as cur:
49 with pytest.raises(RuntimeError):
50 cur.scalar('SELECT * FROM pg_tables')
53 def test_get_pg_env_add_variable(monkeypatch):
54 monkeypatch.delenv('PGPASSWORD', raising=False)
55 env = get_pg_env('user=fooF')
57 assert env['PGUSER'] == 'fooF'
58 assert 'PGPASSWORD' not in env
61 def test_get_pg_env_overwrite_variable(monkeypatch):
62 monkeypatch.setenv('PGUSER', 'some default')
63 env = get_pg_env('user=overwriter')
65 assert env['PGUSER'] == 'overwriter'
68 def test_get_pg_env_ignore_unknown():
69 env = get_pg_env('tty=stuff', base_env={})