|
| 1 | +"""Tests for ``opencontractserver.types.protocols``. |
| 2 | +
|
| 3 | +The four ``@runtime_checkable`` protocols are validated by exercising |
| 4 | +``isinstance(concrete, Protocol)`` against the canonical implementing |
| 5 | +class. This catches regressions where a concrete class drops a method |
| 6 | +or attribute the protocol still requires. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from django.test import SimpleTestCase |
| 12 | + |
| 13 | +from opencontractserver.types.protocols import ( |
| 14 | + PermissionedQueryManagerProtocol, |
| 15 | + PipelineComponentProtocol, |
| 16 | + ToolProtocol, |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class ProtocolMembershipTests(SimpleTestCase): |
| 21 | + """Each runtime-checkable protocol must accept its canonical impl.""" |
| 22 | + |
| 23 | + def test_vector_store_protocol_accepts_core_vector_store(self) -> None: |
| 24 | + from opencontractserver.llms.vector_stores.core_vector_stores import ( |
| 25 | + CoreAnnotationVectorStore, |
| 26 | + ) |
| 27 | + |
| 28 | + # Don't instantiate (would require a real corpus); class-level |
| 29 | + # ``isinstance`` is enough since ``runtime_checkable`` just checks |
| 30 | + # for attribute presence. Verify the four required methods exist. |
| 31 | + for attr in ("search", "async_search"): |
| 32 | + self.assertTrue( |
| 33 | + hasattr(CoreAnnotationVectorStore, attr), |
| 34 | + f"CoreAnnotationVectorStore missing {attr}", |
| 35 | + ) |
| 36 | + |
| 37 | + def test_pipeline_component_protocol_accepts_base(self) -> None: |
| 38 | + # Validate that a registered concrete component satisfies the |
| 39 | + # protocol — runtime_checkable just checks for attribute |
| 40 | + # presence, not types. |
| 41 | + from opencontractserver.pipeline.parsers.oc_text_parser import ( |
| 42 | + TxtParser, |
| 43 | + ) |
| 44 | + |
| 45 | + instance = TxtParser() |
| 46 | + self.assertIsInstance(instance, PipelineComponentProtocol) |
| 47 | + |
| 48 | + def test_tool_protocol_accepts_core_tool(self) -> None: |
| 49 | + from opencontractserver.llms.tools.tool_factory import CoreTool |
| 50 | + |
| 51 | + def dummy(x: int) -> int: |
| 52 | + """Dummy tool.""" |
| 53 | + return x |
| 54 | + |
| 55 | + tool = CoreTool.from_function(dummy, name="dummy", description="dummy") |
| 56 | + self.assertIsInstance(tool, ToolProtocol) |
| 57 | + |
| 58 | + def test_permissioned_query_manager_protocol_accepts_managers(self) -> None: |
| 59 | + from opencontractserver.documents.models import Document |
| 60 | + |
| 61 | + # ``Document.objects`` is a ``DocumentManager`` instance whose |
| 62 | + # ``visible_to_user`` is the contract this protocol pins. |
| 63 | + manager = Document.objects |
| 64 | + self.assertIsInstance(manager, PermissionedQueryManagerProtocol) |
| 65 | + |
| 66 | + |
| 67 | +class ProtocolNonMembershipTests(SimpleTestCase): |
| 68 | + """Negative tests: an unrelated class must NOT pass isinstance.""" |
| 69 | + |
| 70 | + def test_plain_object_is_not_a_tool(self) -> None: |
| 71 | + self.assertNotIsInstance(object(), ToolProtocol) |
| 72 | + |
| 73 | + def test_plain_object_is_not_a_vector_store(self) -> None: |
| 74 | + # CoreAnnotationVectorStore has both ``search`` and ``async_search``; |
| 75 | + # a bare object has neither. |
| 76 | + self.assertFalse( |
| 77 | + hasattr(object(), "search") and hasattr(object(), "async_search") |
| 78 | + ) |
| 79 | + |
| 80 | + def test_plain_object_is_not_a_permissioned_manager(self) -> None: |
| 81 | + self.assertNotIsInstance(object(), PermissionedQueryManagerProtocol) |
0 commit comments