71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
|
|
"""Verify generated .pyi stubs are consistent with pycapnp runtime.
|
||
|
|
|
||
|
|
Test file lives alongside the ``.capnp`` schemas + generated ``.pyi``
|
||
|
|
stubs. pycapnp's import hook handles runtime loading.
|
||
|
|
basedpyright type-checks this file together with the project.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import sys
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# Ensure this directory is importable
|
||
|
|
_HERE = Path(__file__).parent
|
||
|
|
if str(_HERE) not in sys.path:
|
||
|
|
sys.path.insert(0, str(_HERE))
|
||
|
|
|
||
|
|
import capnp # pyright: ignore[reportMissingTypeStubs, reportUnusedImport] # activates import hook
|
||
|
|
import test_simple_capnp
|
||
|
|
|
||
|
|
|
||
|
|
class TestSimpleStub:
|
||
|
|
"""test_simple.capnp — basic struct."""
|
||
|
|
|
||
|
|
def test_new_message(self) -> None:
|
||
|
|
p = test_simple_capnp.Person.new_message(
|
||
|
|
name="Alice", age=30, email="alice@example.com"
|
||
|
|
)
|
||
|
|
assert p.name == "Alice"
|
||
|
|
assert p.age == 30
|
||
|
|
|
||
|
|
def test_field_assignment(self) -> None:
|
||
|
|
p = test_simple_capnp.Person.new_message()
|
||
|
|
p.name = "Bob"
|
||
|
|
p.age = 25
|
||
|
|
assert p.name == "Bob"
|
||
|
|
|
||
|
|
def test_to_dict(self) -> None:
|
||
|
|
p = test_simple_capnp.Person.new_message(name="Carol", age=28)
|
||
|
|
d = p.to_dict()
|
||
|
|
assert d["name"] == "Carol"
|
||
|
|
|
||
|
|
def test_from_dict(self) -> None:
|
||
|
|
p = test_simple_capnp.Person.new_message()
|
||
|
|
_ = p.from_dict({"name": "Dave", "age": 35})
|
||
|
|
assert p.name == "Dave"
|
||
|
|
|
||
|
|
def test_serialize_roundtrip(self) -> None:
|
||
|
|
p = test_simple_capnp.Person.new_message(name="Eve", age=22)
|
||
|
|
data = p.to_bytes()
|
||
|
|
p2 = test_simple_capnp.Person.from_bytes(data)
|
||
|
|
assert p2 is not None
|
||
|
|
|
||
|
|
|
||
|
|
class TestNestedStub:
|
||
|
|
"""test_nested.capnp — nested types + lists."""
|
||
|
|
|
||
|
|
def test_nested_message(self) -> None:
|
||
|
|
import test_nested_capnp
|
||
|
|
ab = test_nested_capnp.AddressBook.new_message()
|
||
|
|
assert ab.people is not None
|
||
|
|
|
||
|
|
|
||
|
|
class TestGenericsStub:
|
||
|
|
"""test_generics.capnp — generic structs."""
|
||
|
|
|
||
|
|
def test_holder_loads(self) -> None:
|
||
|
|
import test_generics_capnp
|
||
|
|
c = test_generics_capnp.Container.new_message()
|
||
|
|
assert c is not None
|