|
| 1 | +"""Unit tests for yt_framework.yt.support._client_dev_runtime helpers.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from yt_framework.yt.support._client_dev_runtime import ( |
| 9 | + dev_copy_output_to_table, |
| 10 | + dev_resolve_sort_keys, |
| 11 | + dev_sort_jsonl_file, |
| 12 | +) |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +def test_dev_resolve_sort_keys_prefers_sort_by_when_set() -> None: |
| 19 | + keys = dev_resolve_sort_keys(reduce_by=["a"], sort_by=["b", "a"]) |
| 20 | + assert keys == ["b", "a"], "sort_by wins when non-empty" |
| 21 | + |
| 22 | + |
| 23 | +def test_dev_resolve_sort_keys_falls_back_to_reduce_by() -> None: |
| 24 | + keys = dev_resolve_sort_keys(reduce_by=["k"], sort_by=None) |
| 25 | + assert keys == ["k"], "reduce_by used when sort_by absent" |
| 26 | + |
| 27 | + |
| 28 | +def test_dev_sort_jsonl_file_missing_sort_key_sorts_before_present( |
| 29 | + tmp_path: Path, |
| 30 | +) -> None: |
| 31 | + path = tmp_path / "rows.jsonl" |
| 32 | + path.write_text( |
| 33 | + '{"k":"b"}\n{}\n{"k":"a"}\n', |
| 34 | + encoding="utf-8", |
| 35 | + ) |
| 36 | + dev_sort_jsonl_file(path, ["k"]) |
| 37 | + rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()] |
| 38 | + assert rows == [{}, {"k": "a"}, {"k": "b"}], ( |
| 39 | + "rows missing sort key sort before rows that define the key" |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_dev_sort_jsonl_file_mixed_types_do_not_raise(tmp_path: Path) -> None: |
| 44 | + path = tmp_path / "rows.jsonl" |
| 45 | + path.write_text( |
| 46 | + '{"k": 2}\n{"k": "1"}\n{"k": 10}\n', |
| 47 | + encoding="utf-8", |
| 48 | + ) |
| 49 | + dev_sort_jsonl_file(path, ["k"]) |
| 50 | + rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()] |
| 51 | + assert rows == [{"k": "1"}, {"k": 10}, {"k": 2}], ( |
| 52 | + "mixed types sort by JSON canonical string without TypeError" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def test_dev_sort_jsonl_file_multi_key_missing_secondary_key(tmp_path: Path) -> None: |
| 57 | + path = tmp_path / "rows.jsonl" |
| 58 | + path.write_text( |
| 59 | + '{"k":"a","v":2}\n{"k":"a"}\n{"k":"a","v":1}\n', |
| 60 | + encoding="utf-8", |
| 61 | + ) |
| 62 | + dev_sort_jsonl_file(path, ["k", "v"]) |
| 63 | + rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()] |
| 64 | + assert rows == [{"k": "a"}, {"k": "a", "v": 1}, {"k": "a", "v": 2}], ( |
| 65 | + "missing secondary sort key sorts before rows that define it" |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def test_dev_sort_jsonl_file_orders_rows_by_keys(tmp_path: Path) -> None: |
| 70 | + path = tmp_path / "rows.jsonl" |
| 71 | + path.write_text( |
| 72 | + '{"k":"b","v":1}\n{"k":"a","v":2}\n{"k":"a","v":1}\n', |
| 73 | + encoding="utf-8", |
| 74 | + ) |
| 75 | + dev_sort_jsonl_file(path, ["k"]) |
| 76 | + rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()] |
| 77 | + assert [r["k"] for r in rows] == ["a", "a", "b"], "stable sort by reduce key" |
| 78 | + |
| 79 | + |
| 80 | +def test_dev_sort_jsonl_file_noop_when_sort_keys_empty(tmp_path: Path) -> None: |
| 81 | + path = tmp_path / "rows.jsonl" |
| 82 | + original = '{"k":"b"}\n{"k":"a"}\n' |
| 83 | + path.write_text(original, encoding="utf-8") |
| 84 | + dev_sort_jsonl_file(path, []) |
| 85 | + assert path.read_text(encoding="utf-8") == original |
| 86 | + |
| 87 | + |
| 88 | +def test_dev_copy_output_to_table_skips_on_nonzero_returncode(tmp_path: Path) -> None: |
| 89 | + sandbox_output = tmp_path / "sand.jsonl" |
| 90 | + sandbox_output.write_text('{"x": 1}\n', encoding="utf-8") |
| 91 | + out_table = tmp_path / "out.jsonl" |
| 92 | + dev_copy_output_to_table( |
| 93 | + proc_returncode=1, |
| 94 | + sandbox_output=sandbox_output, |
| 95 | + output_table_local_path=out_table, |
| 96 | + ) |
| 97 | + assert not out_table.exists(), "failed leg must not publish output" |
| 98 | + |
| 99 | + |
| 100 | +def test_dev_sort_jsonl_file_uses_sort_by_columns(tmp_path: Path) -> None: |
| 101 | + path = tmp_path / "rows.jsonl" |
| 102 | + path.write_text( |
| 103 | + '{"k":"x","ord":2}\n{"k":"x","ord":1}\n', |
| 104 | + encoding="utf-8", |
| 105 | + ) |
| 106 | + dev_sort_jsonl_file(path, ["ord"]) |
| 107 | + rows = [json.loads(line) for line in path.read_text(encoding="utf-8").splitlines()] |
| 108 | + assert [r["ord"] for r in rows] == [1, 2] |
0 commit comments