Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 1, 2025
1 parent ddefccc commit 90d6c2f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
12 changes: 8 additions & 4 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,10 @@ def _validate_rolling_arguments(


def generate_repr(header: str, native_repr: str) -> str:
terminal_width = os.get_terminal_size().columns
try:
terminal_width = os.get_terminal_size().columns
except OSError:
terminal_width = 80
native_lines = native_repr.splitlines()
max_native_width = max(len(line) for line in native_lines)

Expand All @@ -976,8 +979,8 @@ def generate_repr(header: str, native_repr: str) -> str:
f"|{' '*(header_extra//2)}{header}{' '*(header_extra//2 + header_extra%2)}|\n"
)
output += f"|{'-'*(length)}|\n"
start_extra = (max_native_width - length) // 2
end_extra = (max_native_width - length) // 2 + (max_native_width - length) % 2
start_extra = (length - max_native_width) // 2
end_extra = (length - max_native_width) // 2 + (length - max_native_width) % 2
for line in native_lines:
output += f"|{' '*(start_extra)}{line}{' '*(end_extra + max_native_width - len(line))}|\n"
output += f"└{'─' * length}┘"
Expand All @@ -987,5 +990,6 @@ def generate_repr(header: str, native_repr: str) -> str:
return (
f"┌{'─' * (39)}\n"
f"|{' '*(diff//2)}{header}{' '*(diff//2+diff%2)}|\n"
"| Use `.to_native` to see native output |\n" + "└" + "─" * 39 + "┘"
"| Use `.to_native` to see native output |\n└"
f"{'─' * 39}┘"
)
73 changes: 73 additions & 0 deletions tests/repr_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import annotations

import pandas as pd
import pytest

import narwhals.stable.v1 as nw


def test_repr() -> None:
duckdb = pytest.importorskip("duckdb")
df = pd.DataFrame({"a": [1, 2, 3], "b": ["fdaf", "fda", "cf"]})
result = nw.from_native(df).__repr__()
expected = (
"┌──────────────────┐\n"
"|Narwhals DataFrame|\n"
"|------------------|\n"
"| a b |\n"
"| 0 1 fdaf |\n"
"| 1 2 fda |\n"
"| 2 3 cf |\n"
"└──────────────────┘"
)
assert result == expected
result = nw.from_native(df).lazy().__repr__()
expected = (
"┌──────────────────┐\n"
"|Narwhals LazyFrame|\n"
"|------------------|\n"
"| a b |\n"
"| 0 1 fdaf |\n"
"| 1 2 fda |\n"
"| 2 3 cf |\n"
"└──────────────────┘"
)
assert result == expected
result = nw.from_native(df)["a"].__repr__()
expected = (
"┌─────────────────────┐\n"
"| Narwhals Series |\n"
"|---------------------|\n"
"|0 1 |\n"
"|1 2 |\n"
"|2 3 |\n"
"|Name: a, dtype: int64|\n"
"└─────────────────────┘"
)
assert result == expected
result = nw.from_native(duckdb.table("df")).__repr__()
expected = (
"┌───────────────────┐\n"
"|Narwhals DataFrame |\n"
"|-------------------|\n"
"|┌───────┬─────────┐|\n"
"|│ a │ b │|\n"
"|│ int64 │ varchar │|\n"
"|├───────┼─────────┤|\n"
"|│ 1 │ fdaf │|\n"
"|│ 2 │ fda │|\n"
"|│ 3 │ cf │|\n"
"|└───────┴─────────┘|\n"
"└───────────────────┘"
)
assert result == expected
# Make something wider than the terminal size
df = pd.DataFrame({"a": [1, 2, 3], "b": ["fdaf" * 100, "fda", "cf"]})
result = nw.from_native(duckdb.table("df")).__repr__()
expected = (
"┌───────────────────────────────────────┐\n"
"| Narwhals DataFrame |\n"
"| Use `.to_native` to see native output |\n"
"└───────────────────────────────────────┘"
)
assert result == expected

0 comments on commit 90d6c2f

Please sign in to comment.