File size: 4,594 Bytes
f764cdf
 
 
917c16d
 
370fed4
917c16d
370fed4
f764cdf
 
 
 
 
 
917c16d
 
 
f764cdf
 
 
 
 
 
 
 
 
 
917c16d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370fed4
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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)