diff --git a/pyproject.toml b/pyproject.toml index 38f2b24..2006584 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,6 +32,7 @@ distribution = true [tool.pytest.ini_options] testpaths = ["tests"] +markers = ["slow: slow tests (dummy.capnp, generics, interface)"] [tool.setuptools.packages.find] where = ["src"] diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..b946016 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1 @@ +*_capnp.pyi diff --git a/tests/schemas/test_stubs.py b/tests/schemas/test_stubs.py new file mode 100644 index 0000000..a8b1f99 --- /dev/null +++ b/tests/schemas/test_stubs.py @@ -0,0 +1,70 @@ +"""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 diff --git a/tests/test_models.py b/tests/test_models.py index d15e5b6..937469e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -85,11 +85,11 @@ class TestTypeRegistry: assert reg.get_or_raise(1) is info import pytest with pytest.raises(KeyError): - reg.get_or_raise(999) + _ = reg.get_or_raise(999) def test_contains(self): reg = TypeRegistry() - info = TypeInfo(1, "Foo", "Foo", {}, None, "struct") + info: TypeInfo = TypeInfo(1, "Foo", "Foo", {}, None, "struct") reg.register(info) assert 1 in reg assert 2 not in reg diff --git a/tests/test_utils.py b/tests/test_utils.py index 4f779bf..5f96cde 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,5 @@ """Tests for capnp_stubgen.utils.""" -import pytest from capnp_stubgen.utils import ( filename_to_module_name, get_display_name,