From: Daniele Varrazzo Date: Fri, 14 May 2021 17:26:37 +0000 (+0200) Subject: Add docs and tests for ConnectionInfo.timezone X-Git-Tag: 3.0.dev0~42^2~12 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b836900919f78d2fefe86cb003d3854e642b637f;p=thirdparty%2Fpsycopg.git Add docs and tests for ConnectionInfo.timezone --- diff --git a/docs/api/connections.rst b/docs/api/connections.rst index 813736391..d22fb5a9f 100644 --- a/docs/api/connections.rst +++ b/docs/api/connections.rst @@ -275,6 +275,7 @@ Connection support objects .. autoattribute:: error_message .. automethod:: get_parameters + .. autoattribute:: timezone .. autoattribute:: host This can be a host name, an IP address, or a directory path if the diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index be1e706ed..94f936a69 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -1,3 +1,5 @@ +import datetime as dt + import pytest import psycopg3 @@ -188,3 +190,27 @@ class TestConnectionInfo: conn.close() with pytest.raises(psycopg3.OperationalError): conn.info.backend_pid + + def test_timezone(self, conn): + conn.execute("set timezone to 'Europe/Rome'") + tz = conn.info.timezone + assert isinstance(tz, dt.tzinfo) + assert tz.utcoffset(dt.datetime(2000, 1, 1)).total_seconds() == 3600 + assert tz.utcoffset(dt.datetime(2000, 7, 1)).total_seconds() == 7200 + + def test_timezone_warn(self, conn, caplog): + conn.execute("set timezone to 'FOOBAR0'") + assert len(caplog.records) == 0 + tz = conn.info.timezone + assert tz == dt.timezone.utc + assert len(caplog.records) == 1 + assert "FOOBAR0" in caplog.records[0].message + + conn.info.timezone + assert len(caplog.records) == 1 + + conn.execute("set timezone to 'FOOBAAR0'") + assert len(caplog.records) == 1 + conn.info.timezone + assert len(caplog.records) == 2 + assert "FOOBAAR0" in caplog.records[1].message