summaryrefslogtreecommitdiffstats
path: root/tests/pq/test_conninfo.py
blob: 64d8b8fd688767a74f6121745000fe1645cdef44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pytest

import psycopg
from psycopg import pq


def test_defaults(monkeypatch):
    monkeypatch.setenv("PGPORT", "15432")
    defs = pq.Conninfo.get_defaults()
    assert len(defs) > 20
    port = [d for d in defs if d.keyword == b"port"][0]
    assert port.envvar == b"PGPORT"
    assert port.compiled == b"5432"
    assert port.val == b"15432"
    assert port.label == b"Database-Port"
    assert port.dispchar == b""
    assert port.dispsize == 6


@pytest.mark.libpq(">= 10")
def test_conninfo_parse():
    infos = pq.Conninfo.parse(
        b"postgresql://host1:123,host2:456/somedb"
        b"?target_session_attrs=any&application_name=myapp"
    )
    info = {i.keyword: i.val for i in infos if i.val is not None}
    assert info[b"host"] == b"host1,host2"
    assert info[b"port"] == b"123,456"
    assert info[b"dbname"] == b"somedb"
    assert info[b"application_name"] == b"myapp"


@pytest.mark.libpq("< 10")
def test_conninfo_parse_96():
    conninfo = pq.Conninfo.parse(
        b"postgresql://other@localhost/otherdb"
        b"?connect_timeout=10&application_name=myapp"
    )
    info = {i.keyword: i.val for i in conninfo if i.val is not None}
    assert info[b"host"] == b"localhost"
    assert info[b"dbname"] == b"otherdb"
    assert info[b"application_name"] == b"myapp"


def test_conninfo_parse_bad():
    with pytest.raises(psycopg.OperationalError) as e:
        pq.Conninfo.parse(b"bad_conninfo=")
        assert "bad_conninfo" in str(e.value)