]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added a few tests around connection encoding
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 22 May 2020 06:16:03 +0000 (18:16 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 22 May 2020 17:06:33 +0000 (05:06 +1200)
tests/test_async_connection.py
tests/test_connection.py
tests/test_errors.py

index 272d6f0795476f772ef255a942ffc35c18088e88..75ec95e2bcf9b9fe3b3ac8c36220c408bcba6347 100644 (file)
@@ -159,6 +159,45 @@ def test_set_encoding(aconn, loop):
     assert enc == newenc
 
 
+@pytest.mark.parametrize(
+    "enc, out, codec",
+    [
+        ("utf8", "UTF8", "utf-8"),
+        ("utf-8", "UTF8", "utf-8"),
+        ("utf_8", "UTF8", "utf-8"),
+        ("eucjp", "EUC_JP", "euc_jp"),
+        ("euc-jp", "EUC_JP", "euc_jp"),
+    ],
+)
+def test_normalize_encoding(aconn, loop, enc, out, codec):
+    loop.run_until_complete(aconn.set_client_encoding(enc))
+    assert aconn.encoding == out
+    assert aconn.codec.name == codec
+
+
+@pytest.mark.parametrize(
+    "enc, out, codec",
+    [
+        ("utf8", "UTF8", "utf-8"),
+        ("utf-8", "UTF8", "utf-8"),
+        ("utf_8", "UTF8", "utf-8"),
+        ("eucjp", "EUC_JP", "euc_jp"),
+        ("euc-jp", "EUC_JP", "euc_jp"),
+    ],
+)
+def test_encoding_env_var(dsn, loop, monkeypatch, enc, out, codec):
+    monkeypatch.setenv("PGCLIENTENCODING", enc)
+    aconn = loop.run_until_complete(psycopg3.AsyncConnection.connect(dsn))
+    assert aconn.encoding == out
+    assert aconn.codec.name == codec
+
+
+def test_set_encoding_unsupported(aconn, loop):
+    loop.run_until_complete(aconn.set_client_encoding("EUC_TW"))
+    with pytest.raises(psycopg3.NotSupportedError):
+        loop.run_until_complete(aconn.cursor().execute("select 1"))
+
+
 def test_set_encoding_bad(aconn, loop):
     with pytest.raises(psycopg3.DatabaseError):
         loop.run_until_complete(aconn.set_client_encoding("WAT"))
index 7edea64dab734a6755936f4e6978755989c4627b..25921214925d366e81a991591df28972a7080147 100644 (file)
@@ -149,6 +149,39 @@ def test_set_encoding(conn):
     assert enc == newenc
 
 
+@pytest.mark.parametrize(
+    "enc, out, codec",
+    [
+        ("utf8", "UTF8", "utf-8"),
+        ("utf-8", "UTF8", "utf-8"),
+        ("utf_8", "UTF8", "utf-8"),
+        ("eucjp", "EUC_JP", "euc_jp"),
+        ("euc-jp", "EUC_JP", "euc_jp"),
+    ],
+)
+def test_normalize_encoding(conn, enc, out, codec):
+    conn.set_client_encoding(enc)
+    assert conn.encoding == out
+    assert conn.codec.name == codec
+
+
+@pytest.mark.parametrize(
+    "enc, out, codec",
+    [
+        ("utf8", "UTF8", "utf-8"),
+        ("utf-8", "UTF8", "utf-8"),
+        ("utf_8", "UTF8", "utf-8"),
+        ("eucjp", "EUC_JP", "euc_jp"),
+        ("euc-jp", "EUC_JP", "euc_jp"),
+    ],
+)
+def test_encoding_env_var(dsn, monkeypatch, enc, out, codec):
+    monkeypatch.setenv("PGCLIENTENCODING", enc)
+    conn = psycopg3.connect(dsn)
+    assert conn.encoding == out
+    assert conn.codec.name == codec
+
+
 def test_set_encoding_unsupported(conn):
     conn.set_client_encoding("EUC_TW")
     with pytest.raises(psycopg3.NotSupportedError):
index 52e2ba26433e38b7921437a78c9ede20b91fbc5c..8a01da6e4b38cc6969d58a2c2210ac5bbde977d9 100644 (file)
@@ -46,6 +46,7 @@ def test_diag_right_attr(pgconn, pq, monkeypatch):
 @pytest.mark.parametrize("enc", ["utf8", "latin9"])
 def test_diag_encoding(conn, enc):
     msgs = []
+    conn.pgconn.exec_(b"set client_min_messages to notice")
     conn.add_notice_handler(lambda diag: msgs.append(diag.message_primary))
     conn.set_client_encoding(enc)
     cur = conn.cursor()