From 25f76287d78aec61b9378ad2dae325171e7018fb Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 22 May 2020 18:16:03 +1200 Subject: [PATCH] Added a few tests around connection encoding --- tests/test_async_connection.py | 39 ++++++++++++++++++++++++++++++++++ tests/test_connection.py | 33 ++++++++++++++++++++++++++++ tests/test_errors.py | 1 + 3 files changed, 73 insertions(+) diff --git a/tests/test_async_connection.py b/tests/test_async_connection.py index 272d6f079..75ec95e2b 100644 --- a/tests/test_async_connection.py +++ b/tests/test_async_connection.py @@ -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")) diff --git a/tests/test_connection.py b/tests/test_connection.py index 7edea64da..259212149 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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): diff --git a/tests/test_errors.py b/tests/test_errors.py index 52e2ba264..8a01da6e4 100644 --- a/tests/test_errors.py +++ b/tests/test_errors.py @@ -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() -- 2.47.2