test: add comprehensive test suite for schemas, tempo map, runner, and cli
This commit is contained in:
144
tests/test_runner.py
Normal file
144
tests/test_runner.py
Normal file
@ -0,0 +1,144 @@
|
||||
"""Unit tests for PipelineRunner, caching, and hash invalidation."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from songsplit.pipeline.runner import PipelineContext, PipelineRunner, Stage
|
||||
|
||||
|
||||
class MockStageA(Stage):
|
||||
name = "stage_a"
|
||||
version = "1.0.0"
|
||||
model_name = "mock_model_a"
|
||||
model_version = "1.0.0"
|
||||
dependencies: list[str] = []
|
||||
|
||||
def get_inputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.source_file]
|
||||
|
||||
def get_outputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_a.txt"]
|
||||
|
||||
def run(self, ctx: PipelineContext) -> None:
|
||||
(ctx.output_dir / "out_a.txt").write_text("output A")
|
||||
|
||||
|
||||
class MockStageB(Stage):
|
||||
name = "stage_b"
|
||||
version = "1.0.0"
|
||||
model_name = "mock_model_b"
|
||||
model_version = "1.0.0"
|
||||
dependencies = ["stage_a"]
|
||||
|
||||
def get_inputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_a.txt"]
|
||||
|
||||
def get_outputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_b.txt"]
|
||||
|
||||
def run(self, ctx: PipelineContext) -> None:
|
||||
in_text = (ctx.output_dir / "out_a.txt").read_text()
|
||||
(ctx.output_dir / "out_b.txt").write_text(f"{in_text} -> B")
|
||||
|
||||
|
||||
class MockStageFailing(Stage):
|
||||
name = "stage_fail"
|
||||
version = "1.0.0"
|
||||
model_name = "mock_fail"
|
||||
model_version = "1.0.0"
|
||||
dependencies = ["stage_a"]
|
||||
|
||||
def get_inputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_a.txt"]
|
||||
|
||||
def get_outputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_fail.txt"]
|
||||
|
||||
def run(self, ctx: PipelineContext) -> None:
|
||||
raise RuntimeError("Stage failed intentionally")
|
||||
|
||||
|
||||
class MockStageDepOnFail(Stage):
|
||||
name = "stage_dep_on_fail"
|
||||
version = "1.0.0"
|
||||
model_name = "mock_dep"
|
||||
model_version = "1.0.0"
|
||||
dependencies = ["stage_fail"]
|
||||
|
||||
def get_inputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_fail.txt"]
|
||||
|
||||
def get_outputs(self, ctx: PipelineContext) -> list[Path]:
|
||||
return [ctx.output_dir / "out_dep.txt"]
|
||||
|
||||
def run(self, ctx: PipelineContext) -> None:
|
||||
(ctx.output_dir / "out_dep.txt").write_text("should not run")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def run_env(tmp_path: Path):
|
||||
source = tmp_path / "song.mp3"
|
||||
source.write_text("fake audio bytes")
|
||||
out_dir = tmp_path / "song_pkg"
|
||||
return source, out_dir
|
||||
|
||||
|
||||
def test_runner_execution_and_caching(run_env):
|
||||
source, out_dir = run_env
|
||||
ctx = PipelineContext(source_file=source, output_dir=out_dir)
|
||||
runner = PipelineRunner([MockStageA(), MockStageB()])
|
||||
|
||||
# First run: should execute both stages
|
||||
meta = runner.run(ctx)
|
||||
assert "stage_a" in meta.stages_done
|
||||
assert "stage_b" in meta.stages_done
|
||||
assert (out_dir / "out_a.txt").exists()
|
||||
assert (out_dir / "out_b.txt").read_text() == "output A -> B"
|
||||
assert (out_dir / ".cache" / "stage_a.json").exists()
|
||||
assert (out_dir / ".cache" / "stage_b.json").exists()
|
||||
|
||||
# Second run without changes: both should be skipped from cache
|
||||
# Modify output file content to verify it is NOT overwritten
|
||||
(out_dir / "out_b.txt").write_text("modified manually")
|
||||
runner.run(ctx)
|
||||
assert (out_dir / "out_b.txt").read_text() == "modified manually"
|
||||
|
||||
# Run with force=True: should recalculate
|
||||
ctx_force = PipelineContext(source_file=source, output_dir=out_dir, force=True)
|
||||
runner.run(ctx_force)
|
||||
assert (out_dir / "out_b.txt").read_text() == "output A -> B"
|
||||
|
||||
|
||||
def test_target_stage_only_runs_prerequisites(run_env):
|
||||
source, out_dir = run_env
|
||||
ctx = PipelineContext(source_file=source, output_dir=out_dir, target_stage="stage_b")
|
||||
runner = PipelineRunner([MockStageA(), MockStageB()])
|
||||
|
||||
stages = runner.get_stages_to_run(ctx)
|
||||
stage_names = [s.name for s in stages]
|
||||
assert stage_names == ["stage_a", "stage_b"]
|
||||
|
||||
|
||||
def test_stage_failure_does_not_abort_independent_stages(run_env):
|
||||
source, out_dir = run_env
|
||||
ctx = PipelineContext(source_file=source, output_dir=out_dir)
|
||||
# stage_b depends on stage_a (independent of stage_fail)
|
||||
# stage_dep_on_fail depends on stage_fail
|
||||
runner = PipelineRunner([MockStageA(), MockStageFailing(), MockStageDepOnFail(), MockStageB()])
|
||||
|
||||
meta = runner.run(ctx)
|
||||
|
||||
# stage_a and stage_b succeeded
|
||||
assert "stage_a" in meta.stages_done
|
||||
assert "stage_b" in meta.stages_done
|
||||
assert (out_dir / "out_b.txt").exists()
|
||||
|
||||
# stage_fail recorded error
|
||||
assert "stage_fail" in meta.stages_failed
|
||||
assert "RuntimeError" in meta.stages_failed["stage_fail"]
|
||||
|
||||
# stage_dep_on_fail was skipped due to failed dependency
|
||||
assert "stage_dep_on_fail" in meta.stages_failed
|
||||
assert "dependencies failed" in meta.stages_failed["stage_dep_on_fail"]
|
||||
assert not (out_dir / "out_dep.txt").exists()
|
||||
Reference in New Issue
Block a user