Skip to content
Closed
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
7 changes: 4 additions & 3 deletions raydar/dashboard/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 45 additions & 0 deletions raydar/tests/test_table_creation.py
Original file line number Diff line number Diff line change
@@ -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"