Datasets:
Download src/test_render_sample.py from SlayerLab/polish-dynaword: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/2ae87973217935bf33958d0e51cc72587ed08071/src/test_render_sample.py
- Command line
-
hf download hf://datasets/SlayerLab/polish-dynaword@2ae87973217935bf33958d0e51cc72587ed08071/src/test_render_sample.py
-
curl -L -o test_render_sample.py https://huggingface.co/datasets/SlayerLab/polish-dynaword/resolve/2ae87973217935bf33958d0e51cc72587ed08071/src/test_render_sample.py
2.98 kB
| """Contract for render_sample: readable, deterministic PR-description samples.""" | |
| import datetime | |
| import sys | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| sys.path.insert(0, str(Path(__file__).resolve().parent)) | |
| from render_sample import render, render_doc, sample_rows, truncate | |
| def write_parquet(path, n, row_group_size): | |
| table = pa.table({ | |
| "id": [f"src_{i}" for i in range(n)], | |
| "text": [f"Akapit pierwszy {i}.\n\nAkapit drugi." for i in range(n)], | |
| "source": ["src"] * n, | |
| "added": [datetime.date(2026, 7, 19)] * n, | |
| "created": [["2023-11-15", "2026-09-03"]] * n, | |
| "token_count": list(range(n)), | |
| }) | |
| pq.write_table(table, path, row_group_size=row_group_size) | |
| class RenderSampleTest(unittest.TestCase): | |
| def setUp(self): | |
| self.tmp = tempfile.TemporaryDirectory() | |
| self.path = Path(self.tmp.name) / "src.parquet" | |
| def tearDown(self): | |
| self.tmp.cleanup() | |
| def test_block_layout_matches_pr24_format(self): | |
| row = {"id": "x", "text": "Line one.\n\nLine two.", "added": datetime.date(2026, 7, 19), | |
| "created": ["2023-11-15", "2026-09-03"], "token_count": 5} | |
| self.assertEqual( | |
| render_doc(row, 800), | |
| "```\nid: x\nadded: 2026-07-19\ncreated: 2023-11-15, 2026-09-03\ntoken_count: 5\n\n" | |
| "Line one.\n\nLine two.\n```", | |
| ) | |
| def test_real_newlines_not_escaped(self): | |
| write_parquet(self.path, 5, 2) | |
| out = render(self.path, n=1) | |
| self.assertIn("\n\nAkapit drugi.", out) | |
| self.assertNotIn("\\n", out) | |
| def test_truncates_on_word_boundary(self): | |
| self.assertEqual(truncate("alpha beta gamma", 12), "alpha beta […]") | |
| self.assertEqual(truncate("short", 12), "short") | |
| self.assertEqual(truncate("x" * 20, 5), "xxxxx […]") | |
| def test_fence_outgrows_backticks_in_text(self): | |
| out = render_doc({"text": "code: ```py\nx```"}, 800) | |
| self.assertTrue(out.startswith("````\n") and out.endswith("\n````")) | |
| def test_deterministic_and_bounded(self): | |
| write_parquet(self.path, 50, 10) | |
| self.assertEqual(render(self.path, n=3, seed=1), render(self.path, n=3, seed=1)) | |
| self.assertEqual(len(sample_rows(self.path, 3)), 3) | |
| ids = [r["id"] for r in sample_rows(self.path, 3)] | |
| self.assertEqual(len(set(ids)), 3) | |
| def test_fewer_row_groups_than_n(self): | |
| write_parquet(self.path, 6, 6) # one row group | |
| self.assertEqual(len(sample_rows(self.path, 4)), 4) | |
| def test_n_larger_than_table(self): | |
| write_parquet(self.path, 2, 1) | |
| self.assertEqual(len(sample_rows(self.path, 10)), 2) | |
| def test_empty_parquet(self): | |
| pq.write_table(pa.table({"id": pa.array([], pa.string()), "text": pa.array([], pa.string())}), self.path) | |
| self.assertEqual(render(self.path), "") | |
| if __name__ == "__main__": | |
| unittest.main() | |