Files
pipewright/tests/test_sink_append.py

102 lines
3.3 KiB
Python

import asyncio
import json
from pipewright import Graph
from pipewright.engine import Engine, RunOptions
from pipewright.nodes.builtin import SinkJsonl
from pipewright.store import Store
def recs(n):
return [{"id": f"r{i}", "body": f"text {i}"} for i in range(n)]
def spec(records, **sink):
g = Graph("sink")
src = g.add("src", "source.inline", kind="doc", records=records)
g.add("sink", "sink.jsonl", input=src, path="out/x.jsonl", **sink)
return g.to_spec()
def run(s, proj):
store = Store(proj.store_dir)
try:
summary = asyncio.run(Engine(s, proj, store).run(RunOptions()))
finally:
store.close()
return summary["funnel"]["nodes"]["sink"]
def lines(proj):
return (proj.base_dir / "out/x.jsonl").read_text().splitlines()
def test_rerun_appends_only_new_items(make_project):
proj = make_project()
run(spec(recs(3)), proj)
first = lines(proj)
assert len(first) == 3
assert (proj.base_dir / "out/x.jsonl.pwindex").exists()
run(spec(recs(3)), proj)
assert lines(proj) == first # nothing new
run(spec(recs(3) + [{"id": "a-new", "body": "fresh"}]), proj)
after = lines(proj)
assert after[:3] == first and len(after) == 4
assert json.loads(after[3])["key"] == "a-new" # appended at the end, not re-sorted
run(spec(recs(1)), proj) # items that vanish stay in the append-only log
assert len(lines(proj)) == 4
def test_first_append_run_matches_rewrite(make_project):
proj = make_project()
run(spec(recs(4), mode="rewrite"), proj)
rewritten = lines(proj)
(proj.base_dir / "out/x.jsonl").unlink()
run(spec(recs(4)), proj)
assert lines(proj) == rewritten
def test_truncated_file_triggers_full_rewrite(make_project):
proj = make_project()
run(spec(recs(3)), proj)
full = lines(proj)
out = proj.base_dir / "out/x.jsonl"
out.write_text(full[0] + "\n") # someone truncated it
run(spec(recs(3)), proj)
assert lines(proj) == full
out.unlink() # missing file
run(spec(recs(3)), proj)
assert lines(proj) == full
def test_param_change_triggers_rewrite(make_project):
proj = make_project()
run(spec(recs(3)), proj)
run(spec(recs(3), fields=["key", "payload.body"]), proj)
rows = [json.loads(x) for x in lines(proj)]
assert len(rows) == 3 and all(set(r) == {"key", "payload.body"} for r in rows)
run(spec(recs(3), fields=["key", "payload.body"], lineage=True), proj)
assert all("producer" in json.loads(x) for x in lines(proj))
def test_rewrite_mode_rewrites_every_run(make_project):
proj = make_project()
run(spec(recs(3), mode="rewrite"), proj)
run(spec(recs(2), mode="rewrite"), proj)
assert [json.loads(x)["key"] for x in lines(proj)] == ["r0", "r1"]
assert not (proj.base_dir / "out/x.jsonl.pwindex").exists()
def test_write_result_reports_total_and_appended(tmp_path):
from pipewright.items import new_item
class Ctx:
def resolve_path(self, p):
return tmp_path / p
sink = SinkJsonl("sink", SinkJsonl.Params(path="o.jsonl"))
items = [new_item(f"k{i}", "doc", {"i": i}) for i in range(3)]
assert asyncio.run(sink.write(items[:2], Ctx())) == {"path": str(tmp_path / "o.jsonl"), "rows": 2, "appended": 2}
assert asyncio.run(sink.write(items, Ctx()))["rows"] == 3
assert asyncio.run(sink.write(items, Ctx()))["appended"] == 0