From 746615c757b124effed69314812a8c5834cbff1b Mon Sep 17 00:00:00 2001 From: Pengyi Peng Date: Sat, 14 Nov 2026 03:17:11 +0800 Subject: [PATCH] fix: allow retry after Perspective table creation fails Signed-off-by: Pengyi Peng --- raydar/dashboard/dashboard.py | 7 +++-- raydar/tests/test_table_creation.py | 45 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 raydar/tests/test_table_creation.py diff --git a/raydar/dashboard/dashboard.py b/raydar/dashboard/dashboard.py index 97c3cb2..46d499a 100644 --- a/raydar/dashboard/dashboard.py +++ b/raydar/dashboard/dashboard.py @@ -63,12 +63,13 @@ def new_table(self, tablename: str, schema: dict) -> bool: """Create a table, returning whether it did not already exist.""" if tablename in self._schemas: return False - self._schemas[tablename] = schema - self._temporal_columns[tablename] = {column for column, kind in schema.items() if kind in _TEMPORAL_TYPES} kwargs = {"name": tablename} if self._limit is not None: kwargs["limit"] = self._limit - self._tables[tablename] = self._client.table(schema, **kwargs) + table = self._client.table(schema, **kwargs) + self._schemas[tablename] = schema + self._temporal_columns[tablename] = {column for column, kind in schema.items() if kind in _TEMPORAL_TYPES} + self._tables[tablename] = table return True def update(self, tablename: str, data) -> None: diff --git a/raydar/tests/test_table_creation.py b/raydar/tests/test_table_creation.py new file mode 100644 index 0000000..ead3d04 --- /dev/null +++ b/raydar/tests/test_table_creation.py @@ -0,0 +1,45 @@ +import pytest +from perspective import PerspectiveError + +from raydar.dashboard import Dashboard, TableHost + +INVALID_SCHEMA = {"price": "not-a-perspective-type"} +VALID_SCHEMA = {"price": "float"} + + +def test_failed_table_creation_does_not_register_metadata(): + host = TableHost() + + with pytest.raises(PerspectiveError, match="Unknown type"): + host.new_table("quotes", INVALID_SCHEMA) + + assert host.names() == [] + assert "quotes" not in host._tables + assert "quotes" not in host._temporal_columns + + +def test_corrected_schema_can_retry_after_failed_table_creation(): + host = TableHost() + + with pytest.raises(PerspectiveError, match="Unknown type"): + host.new_table("quotes", INVALID_SCHEMA) + + assert host.new_table("quotes", VALID_SCHEMA) + host.update("quotes", [{"price": 101.25}]) + assert host.names() == ["quotes"] + assert host.total_rows() == 1 + assert host._tables["quotes"].view().to_columns() == {"price": [101.25]} + + +def test_failed_table_creation_does_not_publish_a_phantom_tab(): + dashboard = Dashboard() + + with pytest.raises(PerspectiveError, match="Unknown type"): + dashboard.apply({"schemas": {"existing": VALID_SCHEMA, "quotes": INVALID_SCHEMA}}) + + assert dashboard.state.tables == ["existing"] + assert dashboard.tables.names() == ["existing"] + + dashboard.apply({"schemas": {"quotes": VALID_SCHEMA}, "updates": {"existing": [{"price": 99.0}], "quotes": [{"price": 101.25}]}}) + assert dashboard.state.tables == ["existing", "quotes"] + assert dashboard.state.rows == "2"