Repository navigation
Expand file tree
/
Copy pathtest_ingestion.py
More file actions
134 lines (97 loc) · 4 KB
/
Copy pathtest_ingestion.py
File metadata and controls
134 lines (97 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Ingestion V1 — LLM extraction and pipeline wiring (★③)."""
from __future__ import annotations
import asyncio
import os
import tempfile
from typing import Sequence
from lang2sql.core.ports.ingestion import CandidateKind, Document
from lang2sql.core.types import Completion, Message, ToolSpec
from lang2sql.ingestion import FileSource, IngestionPipeline, LLMExtractor
class _ScriptedLLM:
"""Inline fake LLMPort returning a fixed completion content."""
def __init__(self, content: str) -> None:
self._content = content
async def complete(
self,
messages: Sequence[Message],
tools: Sequence[ToolSpec] = (),
) -> Completion:
return Completion(content=self._content, finish_reason="stop")
_JSON_ARRAY = """
[
{"kind": "metric", "name": "total_revenue",
"definition": "SUM(orders.amount)", "applies_to": "orders"},
{"kind": "rule", "name": "exclude_cancelled",
"definition": "status != 'cancelled'", "applies_to": "total_revenue"}
]
"""
_FENCED = "```json\n" + _JSON_ARRAY.strip() + "\n```"
def test_llm_extractor_maps_json_array_to_candidates() -> None:
extractor = LLMExtractor(_ScriptedLLM(_JSON_ARRAY))
async def run() -> None:
doc = Document(name="defs.md", text="...", source_id="doc-1")
cands = await extractor.extract(doc)
assert [c.name for c in cands] == ["total_revenue", "exclude_cancelled"]
assert cands[0].kind == CandidateKind.METRIC
assert cands[1].kind == CandidateKind.RULE
assert all(c.source_id == "doc-1" for c in cands)
asyncio.run(run())
def test_llm_extractor_strips_json_fences() -> None:
extractor = LLMExtractor(_ScriptedLLM(_FENCED))
async def run() -> None:
cands = await extractor.extract(Document(name="d", text="t", source_id="s"))
assert len(cands) == 2
asyncio.run(run())
def test_llm_extractor_malformed_content_returns_empty() -> None:
async def run() -> None:
for content in ["not json at all", "", "{not: valid}", "42", "{}"]:
extractor = LLMExtractor(_ScriptedLLM(content))
assert await extractor.extract(Document(name="d", text="t")) == []
asyncio.run(run())
def test_llm_extractor_skips_rows_with_bad_kind_or_missing_fields() -> None:
content = """
[
{"kind": "metric", "name": "ok", "definition": "x"},
{"kind": "bogus", "name": "bad_kind", "definition": "y"},
{"kind": "metric", "name": "", "definition": "z"},
{"kind": "metric", "name": "no_def"},
"not a dict"
]
"""
extractor = LLMExtractor(_ScriptedLLM(content))
async def run() -> None:
cands = await extractor.extract(Document(name="d", text="t"))
assert [c.name for c in cands] == ["ok"]
asyncio.run(run())
def test_file_source_decodes_blob() -> None:
source = FileSource()
async def run() -> None:
doc = await source.fetch("path/to/defs.md", blob=b"hello blob")
assert doc.name == "defs.md"
assert doc.text == "hello blob"
assert doc.source_id == "path/to/defs.md"
asyncio.run(run())
def test_file_source_reads_from_disk() -> None:
source = FileSource()
async def run() -> None:
fd, path = tempfile.mkstemp(suffix=".txt")
try:
with os.fdopen(fd, "w", encoding="utf-8") as handle:
handle.write("on disk")
doc = await source.fetch(path)
assert doc.text == "on disk"
assert doc.name == os.path.basename(path)
finally:
os.remove(path)
asyncio.run(run())
def test_pipeline_fetches_then_extracts() -> None:
pipeline = IngestionPipeline()
source = FileSource()
extractor = LLMExtractor(_ScriptedLLM(_JSON_ARRAY))
async def run() -> None:
cands = await pipeline.ingest(
source, extractor, ref="defs.md", blob=b"document body"
)
assert [c.name for c in cands] == ["total_revenue", "exclude_cancelled"]
assert all(c.source_id == "defs.md" for c in cands)
asyncio.run(run())