- Added the `merge_missing_catalog_widgets` function to append missing widget IDs from the catalog to the dashboard layout while preserving the existing order. - Updated the admin and app dashboard routes to utilize the new function, ensuring that new catalog entries are visible without requiring users to reset their layouts. - Enhanced tests to validate the functionality of the new merging logic, ensuring proper integration with existing layouts. - Bumped application version to reflect these changes.
76 lines
2.2 KiB
Python
76 lines
2.2 KiB
Python
import pytest
|
|
|
|
from dashboard_layout_schema import (
|
|
ALLOWED_WIDGET_IDS,
|
|
DashboardLayoutPayload,
|
|
coalesce_effective_layout,
|
|
default_layout_dict,
|
|
merge_missing_catalog_widgets,
|
|
)
|
|
from widget_catalog import DEFAULT_PRODUCT_DASHBOARD_WIDGET_IDS
|
|
|
|
|
|
def test_default_has_all_allowed_ids():
|
|
d = default_layout_dict()
|
|
got = {w["id"] for w in d["widgets"]}
|
|
assert got == ALLOWED_WIDGET_IDS
|
|
assert {w["id"] for w in d["widgets"] if w["enabled"]} == DEFAULT_PRODUCT_DASHBOARD_WIDGET_IDS
|
|
|
|
|
|
def test_payload_rejects_duplicate_ids():
|
|
with pytest.raises(Exception):
|
|
DashboardLayoutPayload.model_validate(
|
|
{
|
|
"version": 1,
|
|
"widgets": [
|
|
{"id": "welcome", "enabled": True},
|
|
{"id": "welcome", "enabled": False},
|
|
],
|
|
}
|
|
)
|
|
|
|
|
|
def test_payload_requires_one_enabled():
|
|
with pytest.raises(Exception):
|
|
DashboardLayoutPayload.model_validate(
|
|
{
|
|
"version": 1,
|
|
"widgets": [{"id": "dashboard_greeting", "enabled": False}],
|
|
}
|
|
)
|
|
|
|
|
|
def test_coalesce_none():
|
|
custom, eff = coalesce_effective_layout(None)
|
|
assert custom is False
|
|
assert eff == default_layout_dict()
|
|
|
|
|
|
def test_coalesce_valid_raw():
|
|
raw = {
|
|
"version": 1,
|
|
"widgets": [
|
|
{"id": "welcome", "enabled": True},
|
|
{"id": "kpi_board", "enabled": True},
|
|
],
|
|
}
|
|
custom, eff = coalesce_effective_layout(raw)
|
|
assert custom is True
|
|
assert eff == raw
|
|
|
|
|
|
def test_merge_missing_catalog_widgets_keeps_order_and_fills_ids():
|
|
raw = {
|
|
"version": 1,
|
|
"widgets": [
|
|
{"id": "kpi_board", "enabled": True, "config": {}},
|
|
{"id": "welcome", "enabled": False, "config": {}},
|
|
],
|
|
}
|
|
merged = merge_missing_catalog_widgets(raw)
|
|
assert [w["id"] for w in merged["widgets"][:2]] == ["kpi_board", "welcome"]
|
|
assert {w["id"] for w in merged["widgets"]} == ALLOWED_WIDGET_IDS
|
|
extra = [w for w in merged["widgets"] if w["id"] not in ("kpi_board", "welcome")]
|
|
assert all(w["enabled"] is False for w in extra)
|
|
DashboardLayoutPayload.model_validate(merged)
|