1414import time
1515import uuid
1616from typing import Dict , Optional
17+ from tzlocal import get_localzone_name
1718from unittest import mock
1819from urllib .parse import urlparse
1920
4748 _RetryWithExponentialBackoff ,
4849)
4950
51+ try :
52+ from zoneinfo ._common import ZoneInfoNotFoundError # type: ignore
53+
54+ except ModuleNotFoundError :
55+ from backports .zoneinfo ._common import ZoneInfoNotFoundError # type: ignore
56+
5057
5158@mock .patch ("trino.client.TrinoRequest.http" )
5259def test_trino_initial_request (mock_requests , sample_post_response_data ):
@@ -80,6 +87,7 @@ def test_request_headers(mock_get_and_post):
8087 schema = "test_schema"
8188 user = "test_user"
8289 source = "test_source"
90+ timezone = "Europe/Brussels"
8391 accept_encoding_header = "accept-encoding"
8492 accept_encoding_value = "identity,deflate,gzip"
8593 client_info_header = constants .HEADER_CLIENT_INFO
@@ -93,6 +101,7 @@ def test_request_headers(mock_get_and_post):
93101 source = source ,
94102 catalog = catalog ,
95103 schema = schema ,
104+ timezone = timezone ,
96105 headers = {
97106 accept_encoding_header : accept_encoding_value ,
98107 client_info_header : client_info_value ,
@@ -108,9 +117,10 @@ def assert_headers(headers):
108117 assert headers [constants .HEADER_SOURCE ] == source
109118 assert headers [constants .HEADER_USER ] == user
110119 assert headers [constants .HEADER_SESSION ] == ""
120+ assert headers [constants .HEADER_TIMEZONE ] == timezone
111121 assert headers [accept_encoding_header ] == accept_encoding_value
112122 assert headers [client_info_header ] == client_info_value
113- assert len (headers .keys ()) == 8
123+ assert len (headers .keys ()) == 9
114124
115125 req .post ("URL" )
116126 _ , post_kwargs = post .call_args
@@ -1071,3 +1081,62 @@ def test_request_headers_role_empty(mock_get_and_post):
10711081 req .get ("URL" )
10721082 _ , get_kwargs = get .call_args
10731083 assert_headers_with_roles (post_kwargs ["headers" ], None )
1084+
1085+
1086+ def assert_headers_timezone (headers : Dict [str , str ], timezone : str ):
1087+ assert headers [constants .HEADER_TIMEZONE ] == timezone
1088+
1089+
1090+ def test_request_headers_with_timezone (mock_get_and_post ):
1091+ get , post = mock_get_and_post
1092+
1093+ req = TrinoRequest (
1094+ host = "coordinator" ,
1095+ port = 8080 ,
1096+ client_session = ClientSession (
1097+ user = "test_user" ,
1098+ timezone = "Europe/Brussels"
1099+ ),
1100+ )
1101+
1102+ req .post ("URL" )
1103+ _ , post_kwargs = post .call_args
1104+ assert_headers_timezone (post_kwargs ["headers" ], "Europe/Brussels" )
1105+
1106+ req .get ("URL" )
1107+ _ , get_kwargs = get .call_args
1108+ assert_headers_timezone (post_kwargs ["headers" ], "Europe/Brussels" )
1109+
1110+
1111+ def test_request_headers_without_timezone (mock_get_and_post ):
1112+ get , post = mock_get_and_post
1113+
1114+ req = TrinoRequest (
1115+ host = "coordinator" ,
1116+ port = 8080 ,
1117+ client_session = ClientSession (
1118+ user = "test_user" ,
1119+ ),
1120+ )
1121+ localzone = get_localzone_name ()
1122+
1123+ req .post ("URL" )
1124+ _ , post_kwargs = post .call_args
1125+ assert_headers_timezone (post_kwargs ["headers" ], localzone )
1126+
1127+ req .get ("URL" )
1128+ _ , get_kwargs = get .call_args
1129+ assert_headers_timezone (post_kwargs ["headers" ], localzone )
1130+
1131+
1132+ def test_request_with_invalid_timezone (mock_get_and_post ):
1133+ with pytest .raises (ZoneInfoNotFoundError ) as zinfo_error :
1134+ TrinoRequest (
1135+ host = "coordinator" ,
1136+ port = 8080 ,
1137+ client_session = ClientSession (
1138+ user = "test_user" ,
1139+ timezone = "INVALID_TIMEZONE"
1140+ ),
1141+ )
1142+ assert str (zinfo_error .value ).startswith ("'No time zone found with key" )
0 commit comments