Skip to content

Commit

Permalink
install snapshottest
Browse files Browse the repository at this point in the history
  • Loading branch information
swuecho committed Jun 13, 2024
1 parent c5a2efe commit f22fc2d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pandas==1.5.1
pytest==6.0.2
pyarrow==14.0.1
pyarrow==11.0.0
19 changes: 11 additions & 8 deletions snapshottest_ext/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
from snapshottest.formatters import BaseFormatter


def pandas_to_bytes(df: pd.DataFrame) -> str:
df_bytestring = pa.serialize(df).to_buffer().to_pybytes()
return df_bytestring
class ArrowSerializer:

@staticmethod
def pandas_to_bytes(df: pd.DataFrame) -> str:
df_bytestring = pa.serialize(df).to_buffer().to_pybytes()
return df_bytestring

def bytes_to_pandas(raw_bytes) -> pd.DataFrame:
original_df = pa.deserialize(raw_bytes)
return original_df
@staticmethod
def bytes_to_pandas(raw_bytes) -> pd.DataFrame:
original_df = pa.deserialize(raw_bytes)
return original_df


class PandasSnapshot(object):
Expand All @@ -36,7 +39,7 @@ def get_formatter(value):

def store(self, formatter, pandas_snap: PandasSnapshot):
""" store pd.DataFrame as bytes in snapshot file"""
return pandas_to_bytes(pandas_snap.value)
return ArrowSerializer.pandas_to_bytes(pandas_snap.value)

#def get_imports(self):
# """not useful in this one, because we do not deserialize from the bytes directly"""
Expand All @@ -48,7 +51,7 @@ def assert_value_matches_snapshot(self, test, test_value: PandasSnapshot, snapsh
:param test_value: the value in snapshot.assert_mach(value)
:param snapshot_value: the value of format.store (after load)
"""
prev_df = bytes_to_pandas(snapshot_value) # deserialize from bytes to pd.DataFrame
prev_df = ArrowSerializer.bytes_to_pandas(snapshot_value) # deserialize from bytes to pd.DataFrame
pd.testing.assert_frame_equal(test_value.value, prev_df)


Expand Down

0 comments on commit f22fc2d

Please sign in to comment.