Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions packages/inputlayer-py/src/inputlayer/knowledge_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -963,11 +963,22 @@ async def status(self) -> ServerStatus:
)

async def load(self, path: str, *, mode: str | None = None) -> None:
"""Load data from a file."""
cmd = f".load {path}"
if mode:
cmd += f" {mode}"
await self._execute(cmd)
"""Load an IQL file into this knowledge graph.

The file is read locally and sent to the server as a single
multi-statement program, so the load is atomic: the server parses
every statement before executing any, and rejects the whole
program on the first error.

(Sending ``.load`` over the wire does not work: the server treats
it as a client-only REPL command and silently ignores it.)
"""
if mode is not None:
msg = "load(mode=...) is not supported; --replace/--merge are unimplemented server-side"
raise NotImplementedError(msg)
with open(path, encoding="utf-8") as f:
program = f.read()
await self._execute(program)

async def clear_prefix(self, prefix: str) -> ClearResult:
"""Clear all relations matching a prefix."""
Expand Down
51 changes: 51 additions & 0 deletions packages/inputlayer-py/tests/test_kg_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Tests for KnowledgeGraph.load: local file read, sent as one atomic program."""

from __future__ import annotations

from unittest.mock import AsyncMock

import pytest

from inputlayer.knowledge_graph import KnowledgeGraph


def _kg_with_mocked_execute() -> KnowledgeGraph:
kg = KnowledgeGraph.__new__(KnowledgeGraph)
kg._execute = AsyncMock() # type: ignore[method-assign]
return kg


class TestLoad:
async def test_sends_file_contents_as_one_program(self, tmp_path) -> None:
pack = tmp_path / "pack.iql"
pack.write_text(
"// a comment the server strips\n"
'+claim(id: string, val: string)\n'
'+claim[("c1", "v1")]\n'
)

kg = _kg_with_mocked_execute()
await kg.load(str(pack))

kg._execute.assert_awaited_once()
program = kg._execute.await_args.args[0]
# The whole file goes over in a single execute, comments included
# (the server strips them), never as a `.load` meta command.
assert '+claim[("c1", "v1")]' in program
assert "// a comment" in program
assert not program.lstrip().startswith(".load")

async def test_mode_is_rejected(self, tmp_path) -> None:
pack = tmp_path / "pack.iql"
pack.write_text("+r(x: string)\n")

kg = _kg_with_mocked_execute()
with pytest.raises(NotImplementedError):
await kg.load(str(pack), mode="--replace")
kg._execute.assert_not_awaited()

async def test_missing_file_raises(self) -> None:
kg = _kg_with_mocked_execute()
with pytest.raises(FileNotFoundError):
await kg.load("/nonexistent/pack.iql")
kg._execute.assert_not_awaited()
Loading