Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: record_to_csv header option #75

Merged
merged 3 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions turu-snowflake/src/turu/snowflake/record/async_record_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ async def fetch_pandas_all(self, **kwargs) -> "PandasDataFrame":

if isinstance(self._recorder, turu.core.record.CsvRecorder):
if limit := self._recorder._options.get("limit"):
df.head(limit).to_csv(self._recorder.file, index=False)
record_df = df.head(limit)

else:
df.to_csv(self._recorder.file, index=False)
record_df = df

record_df.to_csv(
self._recorder.file,
index=False,
header=self._recorder._options.get("header", True),
)

return df
10 changes: 8 additions & 2 deletions turu-snowflake/src/turu/snowflake/record/record_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ def fetch_pandas_all(self, **kwargs) -> "PandasDataFrame":

if isinstance(self._recorder, turu.core.record.CsvRecorder):
if limit := self._recorder._options.get("limit"):
df.head(limit).to_csv(self._recorder.file, index=False)
record_df = df.head(limit)

else:
df.to_csv(self._recorder.file, index=False)
record_df = df

record_df.to_csv(
self._recorder.file,
index=False,
header=self._recorder._options.get("header", True),
)

return df
29 changes: 29 additions & 0 deletions turu-snowflake/tests/turu/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,35 @@ def test_record_pandas_dataframe(self, connection: turu.snowflake.Connection):
).lstrip()
)

@pytest.mark.skipif(not USE_PANDAS, reason="pandas is not installed")
def test_record_pandas_dataframe_without_header_option(
self, connection: turu.snowflake.Connection
):
import pandas as pd # type: ignore[import]
from pandas.testing import assert_frame_equal # type: ignore[import]

with tempfile.NamedTemporaryFile() as file:
with record_to_csv(
file.name,
connection.execute_map(
pd.DataFrame, "select 1 as ID union all select 2 AS ID"
),
header=False,
) as cursor:
expected = pd.DataFrame({"ID": [1, 2]}, dtype="int8")

assert_frame_equal(cursor.fetch_pandas_all(), expected)

assert (
Path(file.name).read_text()
== dedent(
"""
1
2
"""
).lstrip()
)

@pytest.mark.skipif(not USE_PANDAS, reason="pandas is not installed")
def test_record_pandas_dataframe_with_limit_option(
self, connection: turu.snowflake.Connection
Expand Down
70 changes: 64 additions & 6 deletions turu-snowflake/tests/turu/test_snowflake_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,72 @@ async def test_record_pandas_dataframe(
"select 1 as ID union all select 2 AS ID",
),
) as cursor:
expected = pd.DataFrame(
{"ID": [1, 2]},
dtype="int8",
)
expected = pd.DataFrame({"ID": [1, 2]}, dtype="int8")

assert_frame_equal(await cursor.fetch_pandas_all(), expected)

assert (
Path(file.name).read_text()
== dedent(
"""
ID
1
2
"""
).lstrip()
)

@pytest.mark.skipif(not USE_PANDAS, reason="pandas is not installed")
@pytest.mark.asyncio
async def test_record_pandas_dataframe_without_header_option(
self, async_connection: turu.snowflake.AsyncConnection
):
import pandas as pd # type: ignore[import]
from pandas.testing import assert_frame_equal # type: ignore[import]

with tempfile.NamedTemporaryFile() as file:
async with record_to_csv(
file.name,
await async_connection.execute_map(
pd.DataFrame,
"select 1 as ID union all select 2 AS ID",
),
header=False,
) as cursor:
expected = pd.DataFrame({"ID": [1, 2]}, dtype="int8")

assert_frame_equal(await cursor.fetch_pandas_all(), expected)

assert (
Path(file.name).read_text()
== dedent(
"""
1
2
"""
).lstrip()
)

@pytest.mark.skipif(not USE_PANDAS, reason="pandas is not installed")
@pytest.mark.asyncio
async def test_record_pandas_dataframe_with_limit_option(
self, async_connection: turu.snowflake.AsyncConnection
):
import pandas as pd # type: ignore[import]
from pandas.testing import assert_frame_equal # type: ignore[import]

with tempfile.NamedTemporaryFile() as file:
async with record_to_csv(
file.name,
await async_connection.execute_map(
pd.DataFrame,
"select value::integer as ID from table(flatten(ARRAY_GENERATE_RANGE(1, 10)))",
),
limit=2,
) as cursor:
expected = pd.DataFrame({"ID": list(range(1, 10))}, dtype="object")

assert_frame_equal(await cursor.fetch_pandas_all(), expected)
for row in expected.values:
print(row)

assert (
Path(file.name).read_text()
Expand Down
Loading