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, table_factory):
16 assert db.table_exists('foobar') == False
18 table_factory('foobar')
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, table_factory):
35 table_factory('dummy')
36 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, table_factory):
69 table_factory('dummy')
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_cursor_scalar_no_rows(db, table_factory):
82 table_factory('dummy')
84 with db.cursor() as cur:
85 with pytest.raises(RuntimeError):
86 cur.scalar('SELECT id FROM dummy')
89 def test_get_pg_env_add_variable(monkeypatch):
90 monkeypatch.delenv('PGPASSWORD', raising=False)
91 env = get_pg_env('user=fooF')
93 assert env['PGUSER'] == 'fooF'
94 assert 'PGPASSWORD' not in env
97 def test_get_pg_env_overwrite_variable(monkeypatch):
98 monkeypatch.setenv('PGUSER', 'some default')
99 env = get_pg_env('user=overwriter')
101 assert env['PGUSER'] == 'overwriter'
104 def test_get_pg_env_ignore_unknown():
105 env = get_pg_env('tty=stuff', base_env={})