|
| 1 | +"""Full SDK integration tests — exercises every public client method. |
| 2 | +
|
| 3 | +Requires a running CoordiNode instance: |
| 4 | + docker run -p 7080:7080 -p 7084:7084 ghcr.io/structured-world/coordinode:latest |
| 5 | + COORDINODE_ADDR=localhost:7080 pytest tests/integration/test_sdk.py -v |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import asyncio |
| 11 | +import os |
| 12 | +import uuid |
| 13 | + |
| 14 | +import pytest |
| 15 | + |
| 16 | +from coordinode import AsyncCoordinodeClient, CoordinodeClient |
| 17 | + |
| 18 | +ADDR = os.environ.get("COORDINODE_ADDR", "localhost:7080") |
| 19 | + |
| 20 | + |
| 21 | +# ── Fixtures ────────────────────────────────────────────────────────────────── |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="module") |
| 25 | +def client(): |
| 26 | + with CoordinodeClient(ADDR) as c: |
| 27 | + yield c |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture(scope="module") |
| 31 | +def run(): |
| 32 | + """Run a coroutine synchronously (re-used event loop for the module).""" |
| 33 | + loop = asyncio.new_event_loop() |
| 34 | + yield loop.run_until_complete |
| 35 | + loop.close() |
| 36 | + |
| 37 | + |
| 38 | +def uid() -> str: |
| 39 | + return uuid.uuid4().hex[:8] |
| 40 | + |
| 41 | + |
| 42 | +# ── Health ──────────────────────────────────────────────────────────────────── |
| 43 | + |
| 44 | + |
| 45 | +def test_health(client): |
| 46 | + assert client.health() is True |
| 47 | + |
| 48 | + |
| 49 | +# ── Cypher basics ───────────────────────────────────────────────────────────── |
| 50 | + |
| 51 | + |
| 52 | +def test_cypher_literal(client): |
| 53 | + rows = client.cypher("RETURN 42 AS n") |
| 54 | + assert rows == [{"n": 42}] |
| 55 | + |
| 56 | + |
| 57 | +def test_cypher_string_param(client): |
| 58 | + rows = client.cypher("RETURN $s AS s", params={"s": "hello"}) |
| 59 | + assert rows == [{"s": "hello"}] |
| 60 | + |
| 61 | + |
| 62 | +def test_cypher_float_param(client): |
| 63 | + rows = client.cypher("RETURN $f AS f", params={"f": 3.14}) |
| 64 | + assert len(rows) == 1 |
| 65 | + assert abs(rows[0]["f"] - 3.14) < 1e-6 |
| 66 | + |
| 67 | + |
| 68 | +def test_cypher_bool_param(client): |
| 69 | + rows = client.cypher("RETURN $b AS b", params={"b": True}) |
| 70 | + assert rows == [{"b": True}] |
| 71 | + |
| 72 | + |
| 73 | +def test_cypher_int_param(client): |
| 74 | + rows = client.cypher("RETURN $i AS i", params={"i": -7}) |
| 75 | + assert rows == [{"i": -7}] |
| 76 | + |
| 77 | + |
| 78 | +def test_cypher_null_param(client): |
| 79 | + rows = client.cypher("RETURN $n AS n", params={"n": None}) |
| 80 | + assert rows == [{"n": None}] |
| 81 | + |
| 82 | + |
| 83 | +def test_cypher_no_rows(client): |
| 84 | + rows = client.cypher("MATCH (n:NonExistent_ZZZ) RETURN n") |
| 85 | + assert rows == [] |
| 86 | + |
| 87 | + |
| 88 | +# ── Graph RPC API (stubs — data not yet persisted, node_id always 0) ────────── |
| 89 | +# GraphService (CreateNode / GetNode / CreateEdge) returns the request echoed |
| 90 | +# back with node_id=0. The nodes are NOT stored; MATCH via Cypher returns empty. |
| 91 | +# These tests document the current alpha behaviour so regressions are visible. |
| 92 | + |
| 93 | +_GRAPH_RPC_REASON = ( |
| 94 | + "GraphService stubs echo request data but do not persist nodes (id always 0). " |
| 95 | + "CypherService is the production path for node/edge creation in alpha." |
| 96 | +) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.xfail(reason=_GRAPH_RPC_REASON, strict=True) |
| 100 | +def test_create_node_rpc_returns_id(client): |
| 101 | + """CreateNode RPC must return a non-zero node_id once implemented.""" |
| 102 | + node = client.create_node(labels=["Person"], properties={"name": f"rpc-{uid()}"}) |
| 103 | + assert node.id > 0 |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.xfail(reason=_GRAPH_RPC_REASON, strict=True) |
| 107 | +def test_create_node_rpc_persists(client): |
| 108 | + """Node created via RPC must be retrievable via Cypher.""" |
| 109 | + name = f"persist-{uid()}" |
| 110 | + client.create_node(labels=["Person"], properties={"name": name}) |
| 111 | + rows = client.cypher("MATCH (n:Person {name: $name}) RETURN n.name AS name", params={"name": name}) |
| 112 | + assert len(rows) == 1, f"node not found via Cypher: {rows}" |
| 113 | + client.cypher("MATCH (n:Person {name: $name}) DELETE n", params={"name": name}) |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.xfail(reason=_GRAPH_RPC_REASON, strict=True) |
| 117 | +def test_get_node_rpc(client): |
| 118 | + """GetNode must return the stored node once CreateNode persists.""" |
| 119 | + node = client.create_node(labels=["Person"], properties={"name": f"get-{uid()}"}) |
| 120 | + fetched = client.get_node(node.id) |
| 121 | + assert fetched.id == node.id |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.xfail(reason=_GRAPH_RPC_REASON, strict=True) |
| 125 | +def test_create_edge_rpc(client): |
| 126 | + """CreateEdge must create a traversable relationship once node IDs are valid.""" |
| 127 | + a = client.create_node(labels=["EdgeTest"], properties={"name": f"a-{uid()}"}) |
| 128 | + b = client.create_node(labels=["EdgeTest"], properties={"name": f"b-{uid()}"}) |
| 129 | + edge = client.create_edge("KNOWS", a.id, b.id, properties={"since": 2020}) |
| 130 | + assert edge.id > 0 |
| 131 | + assert edge.source_id == a.id |
| 132 | + assert edge.target_id == b.id |
| 133 | + |
| 134 | + |
| 135 | +# ── Cypher node/edge create + retrieve ─────────────────────────────────────── |
| 136 | + |
| 137 | + |
| 138 | +def test_cypher_create_and_match(client): |
| 139 | + tag = uid() |
| 140 | + client.cypher( |
| 141 | + "CREATE (n:CypherTest {tag: $tag, score: $score})", |
| 142 | + params={"tag": tag, "score": 99}, |
| 143 | + ) |
| 144 | + rows = client.cypher( |
| 145 | + "MATCH (n:CypherTest {tag: $tag}) RETURN n.score AS score", |
| 146 | + params={"tag": tag}, |
| 147 | + ) |
| 148 | + assert rows == [{"score": 99}] |
| 149 | + client.cypher("MATCH (n:CypherTest {tag: $tag}) DELETE n", params={"tag": tag}) |
| 150 | + |
| 151 | + |
| 152 | +def test_cypher_traverse_edge(client): |
| 153 | + # Use Cypher CREATE for nodes+edge — graph RPC stubs are not yet wired. |
| 154 | + tag = uid() |
| 155 | + client.cypher( |
| 156 | + "CREATE (a:TraverseTest {role: 'source', tag: $tag})" |
| 157 | + "-[:POINTS_TO]->" |
| 158 | + "(b:TraverseTest {role: 'target', tag: $tag})", |
| 159 | + params={"tag": tag}, |
| 160 | + ) |
| 161 | + rows = client.cypher( |
| 162 | + "MATCH (a:TraverseTest {tag: $tag})-[:POINTS_TO]->(b:TraverseTest {tag: $tag}) " |
| 163 | + "RETURN a.role AS src, b.role AS dst", |
| 164 | + params={"tag": tag}, |
| 165 | + ) |
| 166 | + assert len(rows) == 1 |
| 167 | + assert rows[0]["src"] == "source" |
| 168 | + assert rows[0]["dst"] == "target" |
| 169 | + client.cypher("MATCH (n:TraverseTest {tag: $tag}) DETACH DELETE n", params={"tag": tag}) |
| 170 | + |
| 171 | + |
| 172 | +# ── Property types round-trip ───────────────────────────────────────────────── |
| 173 | + |
| 174 | + |
| 175 | +def test_property_types_roundtrip(client): |
| 176 | + """All scalar types must survive a write→read round-trip.""" |
| 177 | + tag = uid() |
| 178 | + client.cypher( |
| 179 | + "CREATE (n:TypeTest { tag: $tag, i: $i, f: $f, s: $s, b: $b})", |
| 180 | + params={"tag": tag, "i": 42, "f": 1.5, "s": "hello", "b": True}, |
| 181 | + ) |
| 182 | + rows = client.cypher( |
| 183 | + "MATCH (n:TypeTest {tag: $tag}) RETURN n.i AS i, n.f AS f, n.s AS s, n.b AS b", |
| 184 | + params={"tag": tag}, |
| 185 | + ) |
| 186 | + assert len(rows) == 1 |
| 187 | + r = rows[0] |
| 188 | + assert r["i"] == 42 |
| 189 | + assert abs(r["f"] - 1.5) < 1e-6 |
| 190 | + assert r["s"] == "hello" |
| 191 | + assert r["b"] is True |
| 192 | + client.cypher("MATCH (n:TypeTest {tag: $tag}) DELETE n", params={"tag": tag}) |
| 193 | + |
| 194 | + |
| 195 | +def test_bool_not_serialised_as_vector(client): |
| 196 | + """[True, False] must round-trip as a list of bools, NOT a vector of floats.""" |
| 197 | + tag = uid() |
| 198 | + client.cypher( |
| 199 | + "CREATE (n:BoolListTest {tag: $tag, flags: $flags})", |
| 200 | + params={"tag": tag, "flags": [True, False, True]}, |
| 201 | + ) |
| 202 | + rows = client.cypher( |
| 203 | + "MATCH (n:BoolListTest {tag: $tag}) RETURN n.flags AS flags", |
| 204 | + params={"tag": tag}, |
| 205 | + ) |
| 206 | + assert len(rows) == 1 |
| 207 | + flags = rows[0]["flags"] |
| 208 | + assert flags == [True, False, True], f"expected [True, False, True], got {flags!r}" |
| 209 | + client.cypher("MATCH (n:BoolListTest {tag: $tag}) DELETE n", params={"tag": tag}) |
| 210 | + |
| 211 | + |
| 212 | +# ── Schema ──────────────────────────────────────────────────────────────────── |
| 213 | + |
| 214 | + |
| 215 | +def test_get_schema_text(client): |
| 216 | + schema = client.get_schema_text() |
| 217 | + assert isinstance(schema, str) |
| 218 | + |
| 219 | + |
| 220 | +# ── Host:port string parsing ────────────────────────────────────────────────── |
| 221 | + |
| 222 | + |
| 223 | +def test_hostport_string_parsing(): |
| 224 | + """CoordinodeClient("host:port") must parse correctly.""" |
| 225 | + c = CoordinodeClient("localhost:7080") |
| 226 | + assert c._async._host == "localhost" |
| 227 | + assert c._async._port == 7080 |
| 228 | + |
| 229 | + |
| 230 | +def test_ipv6_bracket_parsing(): |
| 231 | + """Bracketed IPv6 [::1]:7080 must parse correctly.""" |
| 232 | + c = CoordinodeClient("[::1]:7080") |
| 233 | + assert c._async._host == "[::1]" |
| 234 | + assert c._async._port == 7080 |
| 235 | + |
| 236 | + |
| 237 | +def test_bare_ipv6_not_parsed(): |
| 238 | + """Unbracketed IPv6 must NOT be misinterpreted as host:port.""" |
| 239 | + c = CoordinodeClient("::1") |
| 240 | + assert c._async._host == "::1" |
| 241 | + assert c._async._port == 7080 # default unchanged |
| 242 | + |
| 243 | + |
| 244 | +# ── Async client ────────────────────────────────────────────────────────────── |
| 245 | + |
| 246 | + |
| 247 | +@pytest.mark.asyncio |
| 248 | +async def test_async_client_health(): |
| 249 | + async with AsyncCoordinodeClient(ADDR) as c: |
| 250 | + assert await c.health() is True |
| 251 | + |
| 252 | + |
| 253 | +@pytest.mark.asyncio |
| 254 | +async def test_async_client_cypher(): |
| 255 | + async with AsyncCoordinodeClient(ADDR) as c: |
| 256 | + rows = await c.cypher("RETURN 7 AS n") |
| 257 | + assert rows == [{"n": 7}] |
| 258 | + |
| 259 | + |
| 260 | +@pytest.mark.asyncio |
| 261 | +async def test_async_create_node(): |
| 262 | + # Use Cypher; graph RPC stubs not yet wired (see test_create_node_rpc_*). |
| 263 | + tag = uid() |
| 264 | + async with AsyncCoordinodeClient(ADDR) as c: |
| 265 | + await c.cypher("CREATE (n:AsyncTest {tag: $tag})", params={"tag": tag}) |
| 266 | + rows = await c.cypher("MATCH (n:AsyncTest {tag: $tag}) RETURN n.tag AS t", params={"tag": tag}) |
| 267 | + assert rows == [{"t": tag}] |
| 268 | + await c.cypher("MATCH (n:AsyncTest {tag: $tag}) DELETE n", params={"tag": tag}) |
| 269 | + |
| 270 | + |
| 271 | +# ── Vector search (xfail until wired) ───────────────────────────────────────── |
| 272 | + |
| 273 | + |
| 274 | +@pytest.mark.xfail( |
| 275 | + reason=( |
| 276 | + "VectorServiceImpl is a stub — always returns []." |
| 277 | + " HNSW is implemented in coordinode-vector but not wired to the RPC handler." |
| 278 | + ), |
| 279 | + strict=True, |
| 280 | +) |
| 281 | +def test_vector_search_returns_results(client): |
| 282 | + tag = uid() |
| 283 | + vec = [float(i) / 16 for i in range(16)] |
| 284 | + client.cypher( |
| 285 | + "CREATE (n:VecSDKTest {tag: $tag, embedding: $vec})", |
| 286 | + params={"tag": tag, "vec": vec}, |
| 287 | + ) |
| 288 | + try: |
| 289 | + results = client.vector_search(label="VecSDKTest", property="embedding", vector=vec, top_k=1) |
| 290 | + assert len(results) >= 1 |
| 291 | + assert hasattr(results[0], "distance") |
| 292 | + assert hasattr(results[0], "node") |
| 293 | + finally: |
| 294 | + client.cypher("MATCH (n:VecSDKTest {tag: $tag}) DELETE n", params={"tag": tag}) |
0 commit comments