rich/tests/test_table.py

130 lines
19 KiB
Python
Raw Normal View History

2020-06-06 14:34:59 +00:00
# encoding=utf-8
import io
2020-06-28 10:10:59 +00:00
import pytest
2020-06-06 14:34:59 +00:00
2020-06-28 10:10:59 +00:00
from rich import errors
2020-06-06 14:34:59 +00:00
from rich.console import Console
2020-06-28 10:10:59 +00:00
from rich.measure import Measurement
2020-10-08 07:33:32 +00:00
from rich.table import Table, Column
2020-06-23 10:40:31 +00:00
from rich.text import Text
2020-06-06 14:34:59 +00:00
def render_tables():
2020-06-23 10:40:31 +00:00
console = Console(
width=60,
force_terminal=True,
file=io.StringIO(),
legacy_windows=False,
2020-06-28 10:10:59 +00:00
color_system=None,
2020-06-23 10:40:31 +00:00
)
2020-06-06 14:34:59 +00:00
2020-06-23 10:40:31 +00:00
table = Table(title="test table", caption="table caption", expand=True)
table.add_column("foo", footer=Text("total"), no_wrap=True, overflow="ellipsis")
2020-06-06 14:34:59 +00:00
table.add_column("bar", justify="center")
table.add_column("baz", justify="right")
table.add_row("Averlongwordgoeshere", "banana pancakes", None)
2020-06-28 10:10:59 +00:00
assert Measurement.get(console, table, 80) == Measurement(41, 48)
2020-06-06 14:34:59 +00:00
for width in range(10, 60, 5):
console.print(table, width=width)
2020-06-23 10:40:31 +00:00
table.expand = False
console.print(table, justify="left")
console.print(table, justify="center")
console.print(table, justify="right")
assert table.row_count == 1
table.row_styles = ["red", "yellow"]
2020-06-28 10:10:59 +00:00
table.add_row("Coffee")
table.add_row("Coffee", "Chocolate", None, "cinnamon")
2020-06-23 10:40:31 +00:00
2020-06-28 10:10:59 +00:00
assert table.row_count == 3
2020-06-23 10:40:31 +00:00
console.print(table)
table.show_lines = True
console.print(table)
table.show_footer = True
console.print(table)
table.show_edge = False
console.print(table)
table.padding = 1
console.print(table)
table.width = 20
2020-06-30 20:40:11 +00:00
assert Measurement.get(console, table, 80) == Measurement(20, 20)
2020-06-23 10:40:31 +00:00
console.print(table)
2020-07-12 15:17:36 +00:00
table.columns[0].no_wrap = True
table.columns[1].no_wrap = True
table.columns[2].no_wrap = True
console.print(table)
table.padding = 0
table.width = 60
table.leading = 1
console.print(table)
2020-06-06 14:34:59 +00:00
return console.file.getvalue()
def test_render_table():
2020-09-07 15:44:31 +00:00
expected = " test table \n┏━━━━━━┳━┳━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━╇━╇━┩\n│ Ave… │ │ │\n└──────┴─┴─┘\n table \n caption \n test table \n┏━━━━━━━━━━━┳━┳━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━━━━━━╇━╇━┩\n│ Averlong… │ │ │\n└───────────┴─┴─┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━┳━┳━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━━━━━━━━━━━╇━╇━┩\n│ Averlongwordg… │ │ │\n└────────────────┴─┴─┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━┳━┳━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━╇━╇━┩\n│ Averlongwordgoeshe… │ │ │\n└─────────────────────┴─┴─┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━┳━━┓\n┃ foo ┃ ┃ ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━╇━━┩\n│ Averlongwordgoeshere │ │ │\n└──────────────────────┴──┴──┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━┓\n┃ foo ┃ bar ┃ b… ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━┩\n│ Averlongwordgoeshere │ ba… │ │\n│ │ pa… │ │\n└──────────────────────┴─────┴────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana │ │\n│ │ pancak… │ │\n└──────────────────────┴─────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana │ │\n│ │ pancakes │ │\n└──────────────────────┴──────────────┴─────┘\n table caption \n test table \n┏━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━┓\n┃ foo ┃ bar ┃ baz ┃\n┡━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━┩\n│ Averlongwordgoeshere │ banana pancakes │ │\n└───────────────────────┴──────────────────┴─────┘\n table caption \n
2020-06-06 14:34:59 +00:00
assert render_tables() == expected
2020-06-28 10:10:59 +00:00
def test_not_renderable():
class Foo:
pass
table = Table()
with pytest.raises(errors.NotRenderableError):
table.add_row(Foo())
def test_init_append_column():
2020-10-08 07:33:32 +00:00
header_names = ["header1", "header2", "header3"]
test_columns = [
Column(_index=index, header=header) for index, header in enumerate(header_names)
]
# Test appending of strings for header names
2020-10-08 07:44:16 +00:00
assert Table(*header_names).columns == test_columns
2020-10-08 07:33:32 +00:00
# Test directly passing a Table Column objects
assert Table(*test_columns).columns == test_columns
2020-10-08 07:44:16 +00:00
def test_rich_measure():
# Check __rich_measure__() for a negative width passed as an argument
assert Table("test_header", width=None).__rich_measure__(
Console(), -1
) == Measurement(0, 0)
2020-10-08 07:44:16 +00:00
# Check __rich_measure__() for a negative Table.width attribute
2020-10-08 11:25:28 +00:00
assert Table("test_header", width=-1).__rich_measure__(Console(), 1) == Measurement(
0, 0
)
# Check __rich_measure__() for a positive width passed as an argument
assert Table("test_header", width=None).__rich_measure__(
Console(), 10
) == Measurement(10, 10)
# Check __rich_measure__() for a positive Table.width attribute
assert Table("test_header", width=10).__rich_measure__(
Console(), -1
) == Measurement(10, 10)
2020-10-08 07:44:16 +00:00
2020-06-06 14:34:59 +00:00
if __name__ == "__main__":
render = render_tables()
print(render)
print(repr(render))