2 Tests for specialised conenction and cursor classes.
7 from nominatim.db.connection import connect, get_pg_env
11 with connect(dsn) as conn:
15 def test_connection_table_exists(db, table_factory):
16 assert not db.table_exists('foobar')
18 table_factory('foobar')
20 assert db.table_exists('foobar')
23 def test_connection_index_exists(db, table_factory, temp_db_cursor):
24 assert not db.index_exists('some_index')
26 table_factory('foobar')
27 temp_db_cursor.execute('CREATE INDEX some_index ON foobar(id)')
29 assert db.index_exists('some_index')
30 assert db.index_exists('some_index', table='foobar')
31 assert not db.index_exists('some_index', table='bar')
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_with_extensions):
59 ver = db.postgis_version_tuple()
61 assert isinstance(ver, tuple)
66 def test_cursor_scalar(db, table_factory):
67 table_factory('dummy')
69 with db.cursor() as cur:
70 assert cur.scalar('SELECT count(*) FROM dummy') == 0
73 def test_cursor_scalar_many_rows(db):
74 with db.cursor() as cur:
75 with pytest.raises(RuntimeError):
76 cur.scalar('SELECT * FROM pg_tables')
79 def test_cursor_scalar_no_rows(db, table_factory):
80 table_factory('dummy')
82 with db.cursor() as cur:
83 with pytest.raises(RuntimeError):
84 cur.scalar('SELECT id FROM dummy')
87 def test_get_pg_env_add_variable(monkeypatch):
88 monkeypatch.delenv('PGPASSWORD', raising=False)
89 env = get_pg_env('user=fooF')
91 assert env['PGUSER'] == 'fooF'
92 assert 'PGPASSWORD' not in env
95 def test_get_pg_env_overwrite_variable(monkeypatch):
96 monkeypatch.setenv('PGUSER', 'some default')
97 env = get_pg_env('user=overwriter')
99 assert env['PGUSER'] == 'overwriter'
102 def test_get_pg_env_ignore_unknown():
103 env = get_pg_env('tty=stuff', base_env={})