|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | +# ruff: noqa: D103 |
| 3 | + |
| 4 | +"""Protocol 3.3 write-path wire changes. |
| 5 | +
|
| 6 | +1. ``catalog_table_scan_function_get`` is gone; ``catalog_table_scan_branches_get`` |
| 7 | + is the only scan-discovery method. |
| 8 | +2. ``catalog_table_create`` replies ``TableCreateResponse(created: bool not null)``. |
| 9 | +3. The write-options batch carries only ``result_mode``. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import inspect |
| 15 | +from types import SimpleNamespace |
| 16 | +from typing import Any |
| 17 | + |
| 18 | +import pyarrow as pa |
| 19 | +import pytest |
| 20 | +from vgi_rpc.utils import serialize_record_batch_bytes |
| 21 | + |
| 22 | +from vgi.catalog.catalog_interface import CatalogInterface, OnConflict, ReadOnlyCatalogInterface |
| 23 | +from vgi.client.catalog_mixin import CatalogClientMixin |
| 24 | +from vgi.meta_worker import MetaWorker |
| 25 | +from vgi.protocol import TableCreateRequest, TableCreateResponse, VgiProtocol |
| 26 | +from vgi.worker import Worker |
| 27 | +from vgi.write_results import WriteOptions, write_options |
| 28 | + |
| 29 | +SCHEMA = pa.schema([pa.field("id", pa.int64())]) |
| 30 | + |
| 31 | + |
| 32 | +def _create_request(on_conflict: OnConflict = OnConflict.ERROR) -> TableCreateRequest: |
| 33 | + return TableCreateRequest( |
| 34 | + attach_opaque_data=b"sealed-attach", |
| 35 | + schema_path=["main"], |
| 36 | + name="t", |
| 37 | + columns=SCHEMA.serialize().to_pybytes(), |
| 38 | + on_conflict=on_conflict, |
| 39 | + constraint_definitions=[], |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _worker(monkeypatch: pytest.MonkeyPatch, result: Any) -> tuple[Worker, list[dict[str, Any]]]: |
| 44 | + instance = object.__new__(Worker) |
| 45 | + calls: list[dict[str, Any]] = [] |
| 46 | + |
| 47 | + def table_create(**kwargs: Any) -> Any: |
| 48 | + calls.append(kwargs) |
| 49 | + return result |
| 50 | + |
| 51 | + provider = SimpleNamespace(table_create=table_create) |
| 52 | + monkeypatch.setattr(instance, "_get_catalog", lambda: provider) |
| 53 | + monkeypatch.setattr(instance, "_unwrap_attach", lambda envelope: b"provider-attach") |
| 54 | + monkeypatch.setattr(instance, "_enrich_catalog_span", lambda **kwargs: None) |
| 55 | + monkeypatch.setattr(instance, "_require_dependency_transaction", lambda *args: None) |
| 56 | + monkeypatch.setattr(instance, "_validate_column_definition_expressions", lambda *args: None) |
| 57 | + monkeypatch.setattr( |
| 58 | + instance, "_effective_catalog_capabilities", lambda envelope: SimpleNamespace(read=[], ddl=[], write=[]) |
| 59 | + ) |
| 60 | + return instance, calls |
| 61 | + |
| 62 | + |
| 63 | +# ---------------------------------------------------------------- change 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_scan_function_get_rpc_removed() -> None: |
| 67 | + """No vgi.v3 method, worker handler or meta-worker route for the legacy RPC remains.""" |
| 68 | + assert not hasattr(VgiProtocol, "catalog_table_scan_function_get") |
| 69 | + assert not hasattr(Worker, "catalog_table_scan_function_get") |
| 70 | + assert hasattr(VgiProtocol, "catalog_table_scan_branches_get") |
| 71 | + assert "catalog_table_scan_function_get" not in inspect.getsource(MetaWorker) |
| 72 | + |
| 73 | + |
| 74 | +def test_author_scan_function_api_still_backs_branches_get() -> None: |
| 75 | + """Authors keep ``table_scan_function_get``; the base wraps it as one branch.""" |
| 76 | + for cls in (CatalogInterface, ReadOnlyCatalogInterface): |
| 77 | + assert callable(getattr(cls, "table_scan_function_get", None)) |
| 78 | + assert callable(getattr(cls, "table_scan_branches_get", None)) |
| 79 | + |
| 80 | + |
| 81 | +def test_client_scan_function_get_reads_the_single_branch() -> None: |
| 82 | + """The client's ``table_scan_function_get`` asks branches_get, never the removed RPC.""" |
| 83 | + source = inspect.getsource(CatalogClientMixin) |
| 84 | + assert "proxy.catalog_table_scan_function_get" not in source |
| 85 | + |
| 86 | + |
| 87 | +# ---------------------------------------------------------------- change 2 |
| 88 | + |
| 89 | + |
| 90 | +def test_table_create_response_schema() -> None: |
| 91 | + field = TableCreateResponse.ARROW_SCHEMA.field("created") |
| 92 | + assert field.type == pa.bool_() |
| 93 | + assert not field.nullable |
| 94 | + assert TableCreateResponse.ARROW_SCHEMA.names == ["created"] |
| 95 | + |
| 96 | + |
| 97 | +def test_create_method_returns_table_create_response() -> None: |
| 98 | + hints = inspect.signature(VgiProtocol.catalog_table_create).return_annotation |
| 99 | + assert hints in (TableCreateResponse, "TableCreateResponse") |
| 100 | + |
| 101 | + |
| 102 | +@pytest.mark.parametrize( |
| 103 | + ("returned", "created"), |
| 104 | + [(None, True), (True, True), (False, False)], |
| 105 | +) |
| 106 | +def test_worker_reports_created(monkeypatch: pytest.MonkeyPatch, returned: Any, created: bool) -> None: |
| 107 | + instance, calls = _worker(monkeypatch, returned) |
| 108 | + response = instance.catalog_table_create(_create_request(OnConflict.IGNORE)) |
| 109 | + assert response == TableCreateResponse(created=created) |
| 110 | + assert calls[0]["on_conflict"] == OnConflict.IGNORE |
| 111 | + |
| 112 | + |
| 113 | +def test_worker_rejects_non_bool_create_result(monkeypatch: pytest.MonkeyPatch) -> None: |
| 114 | + instance, _ = _worker(monkeypatch, "yes") |
| 115 | + with pytest.raises(TypeError, match="table_create must return bool or None"): |
| 116 | + instance.catalog_table_create(_create_request()) |
| 117 | + |
| 118 | + |
| 119 | +def test_table_create_response_round_trips() -> None: |
| 120 | + for value in (True, False): |
| 121 | + response = TableCreateResponse(created=value) |
| 122 | + assert TableCreateResponse.deserialize_from_bytes(response.serialize_to_bytes()) == response |
| 123 | + |
| 124 | + |
| 125 | +# ---------------------------------------------------------------- change 3 |
| 126 | + |
| 127 | + |
| 128 | +def _call(raw: bytes) -> SimpleNamespace: |
| 129 | + from vgi.arguments import Arguments |
| 130 | + |
| 131 | + return SimpleNamespace(arguments=Arguments(named={"write_options": pa.scalar(raw)})) |
| 132 | + |
| 133 | + |
| 134 | +def test_write_options_has_only_result_mode() -> None: |
| 135 | + raw = serialize_record_batch_bytes(pa.RecordBatch.from_pylist([{"result_mode": "rows"}])) |
| 136 | + options = write_options(_call(raw)) |
| 137 | + assert options == WriteOptions(result_mode="rows") |
| 138 | + # Deprecated accessors keep the only values DuckDB ever sent. |
| 139 | + assert options.on_conflict == "throw" |
| 140 | + assert options.on_conflict_columns == [] |
| 141 | + with pytest.raises(TypeError): |
| 142 | + WriteOptions(result_mode="count", on_conflict="nothing") # type: ignore[call-arg] |
| 143 | + |
| 144 | + |
| 145 | +def test_write_options_ignores_removed_columns_from_an_old_peer() -> None: |
| 146 | + raw = serialize_record_batch_bytes( |
| 147 | + pa.RecordBatch.from_pylist([{"result_mode": "count", "on_conflict": "nothing", "on_conflict_columns": ["id"]}]) |
| 148 | + ) |
| 149 | + options = write_options(_call(raw)) |
| 150 | + assert options.result_mode == "count" |
| 151 | + assert options.on_conflict == "throw" |
0 commit comments