From f338381b731aeb44ea54f570f009fe557b264b0d Mon Sep 17 00:00:00 2001 From: Darren Burns Date: Mon, 23 Jan 2023 15:47:25 +0000 Subject: [PATCH] Testing around row key generation, add_rows etc --- tests/test_data_table.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/test_data_table.py b/tests/test_data_table.py index cf5a4e25d..16567785b 100644 --- a/tests/test_data_table.py +++ b/tests/test_data_table.py @@ -1,8 +1,12 @@ +import pytest + from textual.app import App from textual.coordinate import Coordinate from textual.message import Message from textual.widgets import DataTable -from textual.widgets._data_table import StringKey +from textual.widgets._data_table import StringKey, CellDoesNotExist + +ROWS = [["0/0", "0/1"], ["1/0", "1/1"], ["2/0", "2/1"]] class DataTableApp(App): @@ -41,7 +45,7 @@ async def test_datatable_message_emission(): assert messages == expected_messages table.add_columns("Column0", "Column1") - table.add_rows([["0/0", "0/1"], ["1/0", "1/1"], ["2/0", "2/1"]]) + table.add_rows(ROWS) # A CellHighlighted is emitted because there were no rows (and # therefore no highlighted cells), but then a row was added, and @@ -150,6 +154,28 @@ async def test_clear(): assert len(table.columns) == 0 +def test_add_rows_generates_keys(): + table = DataTable() + keys = table.add_rows(ROWS) + + # Ensure the keys are returned in order, and there's one for each row + for key, row in zip(keys, range(len(ROWS))): + assert table.rows[key].index == row + + +def test_get_cell_value_returns_value_at_cell(): + table = DataTable() + table.add_rows(ROWS) + assert table.get_cell_value(Coordinate(0, 0)) == "0/0" + + +def test_get_cell_value_exception(): + table = DataTable() + table.add_rows(ROWS) + with pytest.raises(CellDoesNotExist): + table.get_cell_value(Coordinate(9999, 0)) + + def test_key_equals_equivalent_string(): text = "Hello" key = StringKey(text)