|
| 1 | +import argparse |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from hyperbase.builders import hedge |
| 6 | +from hyperbase.cli.shuffle import run_shuffle |
| 7 | +from hyperbase.parsers.result import ParseResult |
| 8 | + |
| 9 | + |
| 10 | +def _line(text: str) -> str: |
| 11 | + tokens = text.split() |
| 12 | + pr = ParseResult( |
| 13 | + edge=hedge("(is/P a/C b/C)"), |
| 14 | + text=text, |
| 15 | + tokens=tokens, |
| 16 | + tok_pos=hedge("(is/P a/C b/C)"), |
| 17 | + ) |
| 18 | + return pr.to_json() |
| 19 | + |
| 20 | + |
| 21 | +def _write(path, lines: list[str]) -> str: |
| 22 | + path.write_text("\n".join(lines) + "\n", encoding="utf-8") |
| 23 | + return str(path) |
| 24 | + |
| 25 | + |
| 26 | +def _ns(file, output=None, in_place=False, seed=None) -> argparse.Namespace: |
| 27 | + return argparse.Namespace(file=file, output=output, in_place=in_place, seed=seed) |
| 28 | + |
| 29 | + |
| 30 | +def _read(path) -> list[str]: |
| 31 | + return path.read_text(encoding="utf-8").splitlines() |
| 32 | + |
| 33 | + |
| 34 | +def test_shuffle_is_a_permutation(tmp_path, capsys): |
| 35 | + lines = [_line(f"sentence number {i}") for i in range(20)] |
| 36 | + src = _write(tmp_path / "in.jsonl", lines) |
| 37 | + out = tmp_path / "out.jsonl" |
| 38 | + run_shuffle(_ns(src, output=str(out), seed=42)) |
| 39 | + |
| 40 | + result = _read(out) |
| 41 | + # Same lines, same count — only the order changed. |
| 42 | + assert sorted(result) == sorted(lines) |
| 43 | + assert len(result) == len(lines) |
| 44 | + # A fixed seed over 20 distinct lines reorders them. |
| 45 | + assert result != lines |
| 46 | + assert "Lines shuffled: 20" in capsys.readouterr().err |
| 47 | + |
| 48 | + |
| 49 | +def test_seed_is_reproducible(tmp_path): |
| 50 | + lines = [_line(f"s {i}") for i in range(15)] |
| 51 | + src = _write(tmp_path / "in.jsonl", lines) |
| 52 | + out_a = tmp_path / "a.jsonl" |
| 53 | + out_b = tmp_path / "b.jsonl" |
| 54 | + run_shuffle(_ns(src, output=str(out_a), seed=7)) |
| 55 | + run_shuffle(_ns(src, output=str(out_b), seed=7)) |
| 56 | + assert _read(out_a) == _read(out_b) |
| 57 | + |
| 58 | + |
| 59 | +def test_in_place_shuffles_input(tmp_path, capsys): |
| 60 | + lines = [_line(f"x {i}") for i in range(12)] |
| 61 | + src_path = tmp_path / "data.jsonl" |
| 62 | + _write(src_path, lines) |
| 63 | + run_shuffle(_ns(str(src_path), in_place=True, seed=3)) |
| 64 | + |
| 65 | + result = _read(src_path) |
| 66 | + assert sorted(result) == sorted(lines) |
| 67 | + err = capsys.readouterr().err |
| 68 | + assert "Lines shuffled: 12" in err |
| 69 | + assert "Seed: 3" in err |
| 70 | + |
| 71 | + |
| 72 | +def test_blank_lines_dropped(tmp_path, capsys): |
| 73 | + src_path = tmp_path / "in.jsonl" |
| 74 | + src_path.write_text( |
| 75 | + _line("one") + "\n\n" + _line("two") + "\n \n" + _line("three") + "\n", |
| 76 | + encoding="utf-8", |
| 77 | + ) |
| 78 | + out = tmp_path / "out.jsonl" |
| 79 | + run_shuffle(_ns(str(src_path), output=str(out), seed=1)) |
| 80 | + |
| 81 | + result = _read(out) |
| 82 | + assert len(result) == 3 |
| 83 | + assert "Lines shuffled: 3" in capsys.readouterr().err |
| 84 | + |
| 85 | + |
| 86 | +def test_no_seed_omits_seed_line(tmp_path, capsys): |
| 87 | + src = _write(tmp_path / "in.jsonl", [_line("a"), _line("b")]) |
| 88 | + run_shuffle(_ns(src, output=str(tmp_path / "out.jsonl"))) |
| 89 | + assert "Seed:" not in capsys.readouterr().err |
| 90 | + |
| 91 | + |
| 92 | +def test_neither_output_nor_in_place_exits(tmp_path, capsys): |
| 93 | + src = _write(tmp_path / "in.jsonl", [_line("x")]) |
| 94 | + with pytest.raises(SystemExit) as exc: |
| 95 | + run_shuffle(_ns(src)) |
| 96 | + assert exc.value.code == 1 |
| 97 | + assert "specify -o/--output" in capsys.readouterr().err |
| 98 | + |
| 99 | + |
| 100 | +def test_missing_file_exits(tmp_path, capsys): |
| 101 | + missing = tmp_path / "nope.jsonl" |
| 102 | + with pytest.raises(SystemExit) as exc: |
| 103 | + run_shuffle(_ns(str(missing), output=str(tmp_path / "out.jsonl"))) |
| 104 | + assert exc.value.code == 1 |
| 105 | + assert "file not found" in capsys.readouterr().err |
0 commit comments