Skip to content
Merged
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
21 changes: 19 additions & 2 deletions src/hflow/catalog_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
import sys
import webbrowser
from dataclasses import dataclass
Expand Down Expand Up @@ -32,10 +33,26 @@ class CatalogUiSettings:
catalog_poll_interval_seconds: float = DEFAULT_CATALOG_POLL_INTERVAL_SECONDS

def __post_init__(self) -> None:
if isinstance(self.port, bool) or not isinstance(self.port, int):
raise ValueError(f"port must be an int, got {type(self.port).__name__}")
if not 1 <= self.port <= 65535:
raise ValueError("port must be between 1 and 65535")
if self.catalog_poll_interval_seconds <= 0:
raise ValueError("catalog poll interval must be greater than zero")

if isinstance(self.catalog_poll_interval_seconds, bool) or not isinstance(
self.catalog_poll_interval_seconds, int | float
):
raise ValueError(
"catalog_poll_interval_seconds must be an int or float, got "
f"{type(self.catalog_poll_interval_seconds).__name__}"
)
if (
not math.isfinite(self.catalog_poll_interval_seconds)
or self.catalog_poll_interval_seconds <= 0
):
raise ValueError(
"catalog_poll_interval_seconds must be positive and finite, got "
f"{self.catalog_poll_interval_seconds}"
)


def _raise_if_loopback_port_is_unavailable(port: int) -> None:
Expand Down
74 changes: 74 additions & 0 deletions tests/test_catalog_ui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""The local DuckDB catalog browser lifecycle."""

from dataclasses import replace
from enum import IntEnum
from pathlib import Path
from socket import AF_INET, SOCK_STREAM, socket
from threading import Event, Thread
Expand Down Expand Up @@ -109,6 +111,78 @@ def test_catalog_ui_refuses_an_invalid_port(tmp_path: Path, port: int) -> None:
catalog_ui.CatalogUiSettings(catalog_root=tmp_path / "catalog", port=port)


@pytest.mark.parametrize(
("port", "type_name"),
[(True, "bool"), (4213.0, "float"), ("4213", "str")],
)
def test_catalog_ui_refuses_a_port_of_the_wrong_type(
tmp_path: Path, port: object, type_name: str
) -> None:
settings = catalog_ui.CatalogUiSettings(catalog_root=tmp_path / "catalog")
with pytest.raises(ValueError, match=f"port must be an int, got {type_name}"):
replace(settings, port=port)


@pytest.mark.parametrize("port", [1, 65535])
def test_catalog_ui_accepts_port_boundaries(tmp_path: Path, port: int) -> None:
settings = catalog_ui.CatalogUiSettings(catalog_root=tmp_path / "catalog", port=port)

assert settings.port == port


def test_catalog_ui_accepts_an_int_enum_port(tmp_path: Path) -> None:
class Port(IntEnum):
CATALOG_UI = 4213

settings = catalog_ui.CatalogUiSettings(catalog_root=tmp_path / "catalog", port=Port.CATALOG_UI)

assert settings.port == Port.CATALOG_UI


@pytest.mark.parametrize(
("poll_interval", "type_name"),
[(True, "bool"), ("0.5", "str"), (None, "NoneType")],
)
def test_catalog_ui_refuses_a_poll_interval_of_the_wrong_type(
tmp_path: Path, poll_interval: object, type_name: str
) -> None:
settings = catalog_ui.CatalogUiSettings(catalog_root=tmp_path / "catalog")
with pytest.raises(
ValueError,
match=f"catalog_poll_interval_seconds must be an int or float, got {type_name}",
):
replace(settings, catalog_poll_interval_seconds=poll_interval)


@pytest.mark.parametrize(
"poll_interval",
[0, -0.5, float("nan"), float("inf"), float("-inf")],
)
def test_catalog_ui_refuses_a_nonpositive_or_nonfinite_poll_interval(
tmp_path: Path, poll_interval: float
) -> None:
with pytest.raises(
ValueError,
match="catalog_poll_interval_seconds must be positive and finite",
):
catalog_ui.CatalogUiSettings(
catalog_root=tmp_path / "catalog",
catalog_poll_interval_seconds=poll_interval,
)


@pytest.mark.parametrize("poll_interval", [1, 0.001])
def test_catalog_ui_accepts_a_positive_finite_poll_interval(
tmp_path: Path, poll_interval: float
) -> None:
settings = catalog_ui.CatalogUiSettings(
catalog_root=tmp_path / "catalog",
catalog_poll_interval_seconds=poll_interval,
)

assert settings.catalog_poll_interval_seconds == poll_interval


def test_catalog_ui_refuses_a_port_owned_by_another_process() -> None:
with socket(AF_INET, SOCK_STREAM) as occupied_port_socket:
occupied_port_socket.bind(("127.0.0.1", 0))
Expand Down
Loading