@@ -98,38 +98,68 @@ def test_timezone_specified_in_schema(self):
98
98
self .assertEqual (table , expected )
99
99
100
100
def test_timezone_specified_in_codec_options (self ):
101
- # 1. When specified, CodecOptions.tzinfo will modify timestamp
102
- # type specifiers in the schema to inherit the specified timezone
103
- tz = pytz .timezone ("US/Pacific" )
104
- codec_options = CodecOptions (tz_aware = True , tzinfo = tz )
105
- expected = Table .from_pydict (
106
- {"_id" : [1 , 2 ], "data" : self .expected_times },
107
- ArrowSchema ([("_id" , int32 ()), ("data" , timestamp ("ms" , tz = tz ))]),
101
+ """Test behavior of setting tzinfo CodecOptions in Collection.with_options.
102
+
103
+ When provided, timestamp type specifiers in the schema to inherit the specified timezone.
104
+ Read values will maintain this information for timestamps whether schema is passed or not.
105
+
106
+ Note, this does not apply to datetimes.
107
+ We also test here that if one asks for a different timezone upon reading,
108
+ on returns the requested timezone.
109
+ """
110
+
111
+ # 1. We pass tzinfo to Collection.with_options, and same tzinfo in schema of find_arrow_all
112
+ tz_west = pytz .timezone ("US/Pacific" )
113
+ codec_options = CodecOptions (tz_aware = True , tzinfo = tz_west )
114
+ coll_west = self .coll .with_options (codec_options = codec_options )
115
+
116
+ schema_west = ArrowSchema ([("_id" , int32 ()), ("data" , timestamp ("ms" , tz = tz_west ))])
117
+ table_west = find_arrow_all (
118
+ collection = coll_west ,
119
+ query = {},
120
+ schema = Schema .from_arrow (schema_west ),
121
+ sort = [("_id" , ASCENDING )],
108
122
)
109
123
110
- schemas = [
111
- Schema ({"_id" : int32 (), "data" : timestamp ("ms" )}),
112
- Schema ({"_id" : int32 (), "data" : datetime }),
113
- ]
114
- for schema in schemas :
115
- table = find_arrow_all (
116
- self .coll .with_options (codec_options = codec_options ),
117
- {},
118
- schema = schema ,
119
- sort = [("_id" , ASCENDING )],
120
- )
124
+ expected_west = Table .from_pydict (
125
+ {"_id" : [1 , 2 ], "data" : self .expected_times }, schema = schema_west
126
+ )
127
+ self .assertTrue (table_west .equals (expected_west ))
121
128
122
- self .assertEqual (table , expected )
129
+ # 2. We pass tzinfo to Collection.with_options, but do NOT include a schema in find_arrow_all
130
+ table_none = find_arrow_all (
131
+ collection = coll_west ,
132
+ query = {},
133
+ schema = None ,
134
+ sort = [("_id" , ASCENDING )],
135
+ )
136
+ self .assertTrue (table_none .equals (expected_west ))
123
137
124
- # 2. CodecOptions.tzinfo will be ignored when tzinfo is specified
125
- # in the original schema type specifier.
126
- tz_east = pytz .timezone ("US/Eastern" )
127
- codec_options = CodecOptions (tz_aware = True , tzinfo = tz_east )
128
- schema = Schema ({"_id" : int32 (), "data" : timestamp ("ms" , tz = tz )})
129
- table = find_arrow_all (
130
- self .coll .with_options (codec_options = codec_options ),
131
- {},
132
- schema = schema ,
138
+ # 3. Now we pass a DIFFERENT timezone to the schema in find_arrow_all than we did to the Collection
139
+ schema_east = Schema (
140
+ {"_id" : int32 (), "data" : timestamp ("ms" , tz = pytz .timezone ("US/Eastern" ))}
141
+ )
142
+ table_east = find_arrow_all (
143
+ collection = coll_west ,
144
+ query = {},
145
+ schema = schema_east ,
133
146
sort = [("_id" , ASCENDING )],
134
147
)
135
- self .assertEqual (table , expected )
148
+ # Confirm that we get the timezone we requested
149
+ self .assertTrue (table_east .schema .types == [int32 (), timestamp (unit = "ms" , tz = "US/Eastern" )])
150
+ # Confirm that the times have been adjusted
151
+ times_west = table_west ["data" ].to_pylist ()
152
+ times_east = table_east ["data" ].to_pylist ()
153
+ self .assertTrue (all ([times_west [i ] == times_east [i ] for i in range (len (table_east ))]))
154
+
155
+ # 4. Test behavior of datetime. Output will be pyarrow.timestamp("ms") without timezone
156
+ schema_dt = Schema ({"_id" : int32 (), "data" : datetime })
157
+ table_dt = find_arrow_all (
158
+ collection = coll_west ,
159
+ query = {},
160
+ schema = schema_dt ,
161
+ sort = [("_id" , ASCENDING )],
162
+ )
163
+ self .assertTrue (table_dt .schema .types == [int32 (), timestamp (unit = "ms" )])
164
+ times = table_dt ["data" ].to_pylist ()
165
+ self .assertTrue (times == self .expected_times )
0 commit comments