|
| 1 | +# Copyright 2025, 2026 Query Farm LLC - https://query.farm |
| 2 | + |
| 3 | +"""Drift + behavior tests for `vgi.codegen.go_types`. |
| 4 | +
|
| 5 | +vgi-go checks in the protocol records it serves as Go structs (its wire schemas |
| 6 | +are derived from their ``vgirpc`` struct tags). The checked-in file must match |
| 7 | +what the generator emits right now. vgi-go's own |
| 8 | +``vgi/generated/protocol_types_test.go`` checks each struct's derived schema |
| 9 | +against the generated schema factories. |
| 10 | +
|
| 11 | +Skipped when the `vgi-go` checkout is not next to vgi-python; |
| 12 | +``VGI_GO_GENERATED_TYPES_GO`` overrides the file's location. |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import io |
| 18 | +import os |
| 19 | +import re |
| 20 | +from pathlib import Path |
| 21 | + |
| 22 | +import pyarrow as pa |
| 23 | +import pytest |
| 24 | + |
| 25 | +from vgi.codegen import go_types |
| 26 | +from vgi.codegen._common import GeneratorError |
| 27 | + |
| 28 | +_REGEN_HINT = ( |
| 29 | + "To regenerate, run:\n" |
| 30 | + " uv run --project ~/Development/vgi-python python scripts/regen_generated.py\n" |
| 31 | + "\n" |
| 32 | + "Do not redirect a generator into its destination with `>`: the shell\n" |
| 33 | + "truncates the file before the generator runs, so any failure destroys\n" |
| 34 | + "the checked-in artifact. The script renders to memory first." |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +def _vgi_go_types_path() -> Path: |
| 39 | + override = os.environ.get("VGI_GO_GENERATED_TYPES_GO") |
| 40 | + if override: |
| 41 | + return Path(override) |
| 42 | + return Path(__file__).resolve().parents[2] / "vgi-go" / "vgi" / "generated" / "protocol_types.go" |
| 43 | + |
| 44 | + |
| 45 | +def _render() -> str: |
| 46 | + buf = io.StringIO() |
| 47 | + go_types.emit(buf) |
| 48 | + return buf.getvalue() |
| 49 | + |
| 50 | + |
| 51 | +def test_generator_is_deterministic() -> None: |
| 52 | + """Calling emit() twice produces byte-identical output.""" |
| 53 | + assert _render() == _render() |
| 54 | + |
| 55 | + |
| 56 | +def test_checked_in_file_matches_generator() -> None: |
| 57 | + """Drift check: the checked-in Go matches what the generator produces.""" |
| 58 | + path = _vgi_go_types_path() |
| 59 | + if not path.exists(): |
| 60 | + pytest.skip(f"{path} not found; set VGI_GO_GENERATED_TYPES_GO or check out vgi-go next to vgi-python") |
| 61 | + assert path.read_text() == _render(), f"{path} is stale.\n{_REGEN_HINT}" |
| 62 | + |
| 63 | + |
| 64 | +def test_catalog_contents_records_are_emitted() -> None: |
| 65 | + """The records vgi-go serves for catalog_attach / catalog_contents are generated.""" |
| 66 | + names = {r.name for r in go_types.build_records()} |
| 67 | + assert {"CatalogAttachResult", "SchemaContents", "CatalogContentsResponse"} <= names |
| 68 | + |
| 69 | + |
| 70 | +def test_fields_follow_arrow_schema() -> None: |
| 71 | + """Field order, names and nullability come from ARROW_SCHEMA.""" |
| 72 | + for record in go_types.build_records(): |
| 73 | + assert [f.wire_name for f in record.fields] == record.schema.names, record.name |
| 74 | + for f, arrow_field in zip(record.fields, record.schema, strict=True): |
| 75 | + assert f.go_type.startswith("*") == arrow_field.nullable, (record.name, f.wire_name) |
| 76 | + |
| 77 | + |
| 78 | +def test_attach_result_shape() -> None: |
| 79 | + """Spot-check the mappings: binary, list<binary>, map, nullable string, trailing flag.""" |
| 80 | + record = next(r for r in go_types.build_records() if r.name == "CatalogAttachResult") |
| 81 | + fields = {f.wire_name: f for f in record.fields} |
| 82 | + assert fields["attach_opaque_data"].go_type == "[]byte" |
| 83 | + assert fields["settings"].go_type == "[][]byte" |
| 84 | + assert fields["tags"].go_type == "map[string]string" |
| 85 | + assert fields["comment"].go_type == "*string" |
| 86 | + assert fields["catalog_version"].go_type == "int64" |
| 87 | + last = record.fields[-1] |
| 88 | + assert (last.wire_name, last.name, last.go_type) == ("supports_catalog_contents", "SupportsCatalogContents", "bool") |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize( |
| 92 | + ("dtype", "expected"), |
| 93 | + [ |
| 94 | + (pa.large_binary(), ("[]byte", ["large_binary"])), |
| 95 | + (pa.list_(pa.large_binary()), ("[][]byte", ["elem=large_binary"])), |
| 96 | + (pa.dictionary(pa.int16(), pa.string()), ("string", ["enum"])), |
| 97 | + (pa.list_(pa.string()), ("[]string", [])), |
| 98 | + (pa.map_(pa.string(), pa.int64()), ("map[string]int64", [])), |
| 99 | + ], |
| 100 | +) |
| 101 | +def test_type_mapping(dtype: pa.DataType, expected: tuple[str, list[str]]) -> None: |
| 102 | + """Arrow types map to the Go type + tag option vgi-rpc-go derives them from.""" |
| 103 | + assert go_types._map(dtype, None, origin="t") == expected |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "dtype", |
| 108 | + [ |
| 109 | + pa.list_(pa.field("item", pa.binary(), nullable=False)), |
| 110 | + pa.list_(pa.list_(pa.large_binary())), |
| 111 | + pa.struct([pa.field("a", pa.int64())]), |
| 112 | + pa.dictionary(pa.int32(), pa.string()), |
| 113 | + ], |
| 114 | +) |
| 115 | +def test_inexpressible_types_raise(dtype: pa.DataType) -> None: |
| 116 | + """A type vgi-rpc-go cannot derive is an error, never a silently different struct.""" |
| 117 | + with pytest.raises(GeneratorError): |
| 118 | + go_types._map(dtype, None, origin="t") |
| 119 | + |
| 120 | + |
| 121 | +def test_initialisms() -> None: |
| 122 | + """Go field names spell initialisms the Go way.""" |
| 123 | + assert go_types.go_field_name("attach_opaque_data") == "AttachOpaqueData" |
| 124 | + assert go_types.go_field_name("notes_url") == "NotesURL" |
| 125 | + assert go_types.go_field_name("split_token_ttl_seconds") == "SplitTokenTTLSeconds" |
| 126 | + |
| 127 | + |
| 128 | +def test_output_is_gofmt_shaped() -> None: |
| 129 | + """No tabs-vs-space alignment for gofmt to redo, and no trailing blank line.""" |
| 130 | + text = _render() |
| 131 | + assert not text.endswith("\n\n") |
| 132 | + for line in text.splitlines(): |
| 133 | + if line.startswith("\t") and "`vgirpc:" in line: |
| 134 | + assert re.fullmatch(r"\t\w+ \S+ `vgirpc:\"[^\"]+\"`", line), line |
0 commit comments