summaryrefslogtreecommitdiffstats
path: root/tests/test_encodings.py
blob: 113f0e38bdbf00986338b8d28221ce16b1e1ef0f (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
49
50
51
52
53
54
55
56
57
import codecs
import pytest

import psycopg
from psycopg import _encodings as encodings


def test_names_normalised():
    for name in encodings._py_codecs.values():
        assert codecs.lookup(name).name == name


@pytest.mark.parametrize(
    "pyenc, pgenc",
    [
        ("ascii", "SQL_ASCII"),
        ("utf8", "UTF8"),
        ("utf-8", "UTF8"),
        ("uTf-8", "UTF8"),
        ("latin9", "LATIN9"),
        ("iso8859-15", "LATIN9"),
    ],
)
def test_py2pg(pyenc, pgenc):
    assert encodings.py2pgenc(pyenc) == pgenc.encode()


@pytest.mark.parametrize(
    "pyenc, pgenc",
    [
        ("ascii", "SQL_ASCII"),
        ("utf-8", "UTF8"),
        ("iso8859-15", "LATIN9"),
    ],
)
def test_pg2py(pyenc, pgenc):
    assert encodings.pg2pyenc(pgenc.encode()) == pyenc


@pytest.mark.parametrize("pgenc", ["MULE_INTERNAL", "EUC_TW"])
def test_pg2py_missing(pgenc):
    with pytest.raises(psycopg.NotSupportedError):
        encodings.pg2pyenc(pgenc.encode())


@pytest.mark.parametrize(
    "conninfo, pyenc",
    [
        ("", "utf-8"),
        ("user=foo, dbname=bar", "utf-8"),
        ("user=foo, dbname=bar, client_encoding=EUC_JP", "euc_jp"),
        ("user=foo, dbname=bar, client_encoding=euc-jp", "euc_jp"),
        ("user=foo, dbname=bar, client_encoding=WAT", "utf-8"),
    ],
)
def test_conninfo_encoding(conninfo, pyenc):
    assert encodings.conninfo_encoding(conninfo) == pyenc