Skip to content

Commit 23176c0

Browse files
authored
TST: Avoid time dependency in GCS zip test (#44552)
* TST: Avoid time dependency in GCS zip test * Use context manager
1 parent 3a25cb9 commit 23176c0

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

pandas/tests/io/test_gcs.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from io import BytesIO
22
import os
3+
import zipfile
34

45
import numpy as np
56
import pytest
@@ -88,16 +89,23 @@ def test_to_read_gcs(gcs_buffer, format):
8889
tm.assert_frame_equal(df1, df2)
8990

9091

91-
def assert_equal_zip_safe(result: bytes, expected: bytes):
92+
def assert_equal_zip_safe(result: bytes, expected: bytes, compression: str):
9293
"""
93-
We would like to assert these are equal, but the 10th and 11th bytes are a
94-
last-modified timestamp, which in some builds is off-by-one, so we check around
95-
that.
94+
For zip compression, only compare the CRC-32 checksum of the file contents
95+
to avoid checking the time-dependent last-modified timestamp which
96+
in some CI builds is off-by-one
9697
9798
See https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
9899
"""
99-
assert result[:9] == expected[:9]
100-
assert result[11:] == expected[11:]
100+
if compression == "zip":
101+
# Only compare the CRC checksum of the file contents
102+
with zipfile.ZipFile(BytesIO(result)) as exp, zipfile.ZipFile(
103+
BytesIO(expected)
104+
) as res:
105+
for res_info, exp_info in zip(res.infolist(), exp.infolist()):
106+
assert res_info.CRC == exp_info.CRC
107+
else:
108+
assert result == expected
101109

102110

103111
@td.skip_if_no("gcsfs")
@@ -126,7 +134,7 @@ def test_to_csv_compression_encoding_gcs(gcs_buffer, compression_only, encoding)
126134
df.to_csv(path_gcs, compression=compression, encoding=encoding)
127135
res = gcs_buffer.getvalue()
128136
expected = buffer.getvalue()
129-
assert_equal_zip_safe(res, expected)
137+
assert_equal_zip_safe(res, expected, compression_only)
130138

131139
read_df = read_csv(
132140
path_gcs, index_col=0, compression=compression_only, encoding=encoding
@@ -142,7 +150,7 @@ def test_to_csv_compression_encoding_gcs(gcs_buffer, compression_only, encoding)
142150

143151
res = gcs_buffer.getvalue()
144152
expected = buffer.getvalue()
145-
assert_equal_zip_safe(res, expected)
153+
assert_equal_zip_safe(res, expected, compression_only)
146154

147155
read_df = read_csv(path_gcs, index_col=0, compression="infer", encoding=encoding)
148156
tm.assert_frame_equal(df, read_df)

0 commit comments

Comments
 (0)