import json from pathlib import Path import pyarrow as pa import pyarrow.parquet as pq import pytest from stackgo.scripts.export_stack_go import _validate_interval_coverage, export, export_merged def test_export_separates_metadata_and_contents(tmp_path): source = tmp_path / "source" shard = source / "shards" / "train-00000-of-00001" / "contents" shard.mkdir(parents=True) (source / "dataset_manifest.json").write_text(json.dumps({ "source": "test", "star_events_count_exclusive": 10, })) (source / "shards" / "train-00000-of-00001" / "repositories.parquet").write_bytes(b"metadata") (source / "shards" / "train-00000-of-00001" / "ready.json").write_text( json.dumps({"repositories": 2, "file_references": 3}) ) (shard / "body.parquet").write_bytes(b"contents") result = export(source, tmp_path / "the-stack-go", mode="copy", verify=True) assert result["totals"]["shards"] == 1 assert result["totals"]["repositories"] == 2 assert (tmp_path / "the-stack-go/repo-meta/train-00000-of-00001/repositories.parquet").read_bytes() == b"metadata" assert (tmp_path / "the-stack-go/repo-contents/train-00000-of-00001/body.parquet").read_bytes() == b"contents" def _write_merged_source(root: Path, name: str, manifest: dict, metadata: list[dict], contents: list[dict]) -> Path: source = root / name shard = source / "shards" / "train-00000-of-00001" (shard / "contents").mkdir(parents=True) (source / "dataset_manifest.json").write_text(json.dumps({ **manifest, "sources": [{"name": "train-00000-of-00001.parquet"}], })) pq.write_table(pa.Table.from_pylist(metadata), shard / "repositories.parquet") pq.write_table(pa.Table.from_pylist(contents), shard / "contents" / "body.parquet") (shard / "ready.json").write_text(json.dumps({ "repositories": len(metadata), "file_references": sum(row["num_files"] for row in metadata), })) return source def test_merged_export_combines_ranges_and_deduplicates_blobs(tmp_path): shared = "a" * 40 first = "b" * 40 second = "c" * 40 common_file = {"blob_id": shared, "path": "shared.py", "length_bytes": 6} source_one = _write_merged_source( tmp_path, "low", {"star_events_count_exclusive": 1, "star_events_count_max_exclusive": 6}, [{"repo_name": "low/repo", "revision_id": "low", "star_events_count": 2, "num_files": 2, "files": [common_file, {"blob_id": first, "path": "one.py", "length_bytes": 3}]}], [ {"blob_id": shared, "content": b"shared", "length_bytes": 6, "indexed_length_bytes": 6, "length_matches_index": True}, {"blob_id": first, "content": b"one", "length_bytes": 3, "indexed_length_bytes": 3, "length_matches_index": True}, ], ) source_two = _write_merged_source( tmp_path, "high", {"star_events_count_exclusive": 5}, [{"repo_name": "high/repo", "revision_id": "high", "star_events_count": 6, "num_files": 2, "files": [common_file, {"blob_id": second, "path": "two.py", "length_bytes": 3}]}], [ {"blob_id": shared, "content": b"shared", "length_bytes": 6, "indexed_length_bytes": 6, "length_matches_index": True}, {"blob_id": second, "content": b"two", "length_bytes": 3, "indexed_length_bytes": 3, "length_matches_index": True}, ], ) output = tmp_path / "merged" result = export_merged( [source_one, source_two], output, star_events_count_exclusive=1, expected_repositories=2, expected_files=4, ) metadata = pq.read_table( output / "repo-meta/train-00000-of-00001/repositories.parquet" ).to_pylist() contents = pq.read_table(output / "repo-contents/train-00000-of-00001/00000.parquet").to_pylist() assert [row["repo_name"] for row in metadata] == ["low/repo", "high/repo"] assert {row["blob_id"] for row in contents} == {shared, first, second} assert len(contents) == 3 assert result["totals"]["repositories"] == 2 assert result["totals"]["blobs"] == 3 assert result["star_events_count_exclusive"] == 1 def test_finite_star_interval_coverage(): _validate_interval_coverage([ {"star_events_count_exclusive": 9, "star_events_count_max_exclusive": 11}, ], 9, 11) with pytest.raises(ValueError, match="do not cover"): _validate_interval_coverage([ {"star_events_count_exclusive": 9, "star_events_count_max_exclusive": 11}, ], 9, 12)