2 Tests for specialised conenction and cursor classes.
7 from nominatim.db.connection import connect, get_pg_env
11 with connect('dbname=' + temp_db) as conn:
15 def test_connection_table_exists(db, temp_db_cursor):
16 assert db.table_exists('foobar') == False
18 temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
20 assert db.table_exists('foobar') == True
23 def test_connection_index_exists(db, temp_db_cursor):
24 assert db.index_exists('some_index') == False
26 temp_db_cursor.execute('CREATE TABLE foobar (id INT)')
27 temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
29 assert db.index_exists('some_index') == True
30 assert db.index_exists('some_index', table='foobar') == True
31 assert db.index_exists('some_index', table='bar') == False
34 def test_drop_table_existing(db, temp_db_cursor):
35 temp_db_cursor.execute('CREATE TABLE dummy (id INT)')
37 assert db.table_exists('dummy')
38 db.drop_table('dummy')
39 assert not db.table_exists('dummy')
42 def test_drop_table_non_existsing(db):
43 db.drop_table('dfkjgjriogjigjgjrdghehtre')
46 def test_drop_table_non_existing_force(db):
47 with pytest.raises(psycopg2.ProgrammingError, match='.*does not exist.*'):
48 db.drop_table('dfkjgjriogjigjgjrdghehtre', if_exists=False)
50 def test_connection_server_version_tuple(db):
51 ver = db.server_version_tuple()
53 assert isinstance(ver, tuple)
58 def test_connection_postgis_version_tuple(db, temp_db_cursor):
59 temp_db_cursor.execute('CREATE EXTENSION postgis')
61 ver = db.postgis_version_tuple()
63 assert isinstance(ver, tuple)
68 def test_cursor_scalar(db, temp_db_cursor):
69 temp_db_cursor.execute('CREATE TABLE dummy (id INT)')
71 with db.cursor() as cur:
72 assert cur.scalar('SELECT count(*) FROM dummy') == 0
75 def test_cursor_scalar_many_rows(db):
76 with db.cursor() as cur:
77 with pytest.raises(RuntimeError):
78 cur.scalar('SELECT * FROM pg_tables')
81 def test_get_pg_env_add_variable(monkeypatch):
82 monkeypatch.delenv('PGPASSWORD', raising=False)
83 env = get_pg_env('user=fooF')
85 assert env['PGUSER'] == 'fooF'
86 assert 'PGPASSWORD' not in env
89 def test_get_pg_env_overwrite_variable(monkeypatch):
90 monkeypatch.setenv('PGUSER', 'some default')
91 env = get_pg_env('user=overwriter')
93 assert env['PGUSER'] == 'overwriter'
96 def test_get_pg_env_ignore_unknown():
97 env = get_pg_env('tty=stuff', base_env={})