Skip to content

Commit ecc4132

Browse files
justinwon777claude
andcommitted
test(internal/idmap): add direct coverage for ChunkMetadata
ChunkMetadata was previously only exercised indirectly through ResultToGetTargetGraphResponse. Add tests covering single-chunk output when maps are under the chunk size, splitting large maps across chunks (with small maps only present in the first chunk), empty inputs, and the non-positive chunkSize default. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent f2436a7 commit ecc4132

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

internal/idmap/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ go_test(
1212
name = "idmap_test",
1313
srcs = ["idmap_test.go"],
1414
embed = [":idmap"],
15-
deps = ["@com_github_stretchr_testify//assert"],
15+
deps = [
16+
"@com_github_stretchr_testify//assert",
17+
"@com_github_stretchr_testify//require",
18+
],
1619
)

internal/idmap/idmap_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"testing"
2020

2121
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
2223
)
2324

2425
func TestNameIDMapper_AssignsSequentialAndStableIDs(t *testing.T) {
@@ -65,3 +66,48 @@ func TestNameIDMapper_NeverAssignsZero(t *testing.T) {
6566
assert.NotEqual(t, int32(0), id)
6667
}
6768
}
69+
70+
func TestChunkMetadata_SplitsLargeMapsAcrossChunks(t *testing.T) {
71+
targetIDToName := make(map[int32]string, 250)
72+
for i := int32(1); i <= 250; i++ {
73+
targetIDToName[i] = fmt.Sprintf("//pkg:target%d", i)
74+
}
75+
ruleTypeIDToName := map[int32]string{1: "go_library"}
76+
tagIDToName := map[int32]string{1: "manual"}
77+
attrNameIDToName := map[int32]string{1: "srcs"}
78+
attrStrValIDToVal := make(map[int32]string, 150)
79+
for i := int32(1); i <= 150; i++ {
80+
attrStrValIDToVal[i] = fmt.Sprintf("value%d", i)
81+
}
82+
83+
chunks := ChunkMetadata(targetIDToName, ruleTypeIDToName, tagIDToName, attrNameIDToName, attrStrValIDToVal, 100)
84+
85+
require.Len(t, chunks, 3)
86+
87+
// The small maps only ever appear in the first chunk.
88+
assert.Equal(t, ruleTypeIDToName, chunks[0].RuleTypeMapping)
89+
assert.Equal(t, tagIDToName, chunks[0].TagMapping)
90+
assert.Equal(t, attrNameIDToName, chunks[0].AttributeNameMapping)
91+
for _, c := range chunks[1:] {
92+
assert.Nil(t, c.RuleTypeMapping)
93+
assert.Nil(t, c.TagMapping)
94+
assert.Nil(t, c.AttributeNameMapping)
95+
}
96+
97+
// The large maps are split into chunks of at most chunkSize entries each,
98+
// and the union of all chunks reconstructs the original maps.
99+
gotTargets := make(map[int32]string, len(targetIDToName))
100+
gotAttrVals := make(map[int32]string, len(attrStrValIDToVal))
101+
for _, c := range chunks {
102+
assert.LessOrEqual(t, len(c.TargetIdMapping), 100)
103+
assert.LessOrEqual(t, len(c.AttributeStringValueMapping), 100)
104+
for k, v := range c.TargetIdMapping {
105+
gotTargets[k] = v
106+
}
107+
for k, v := range c.AttributeStringValueMapping {
108+
gotAttrVals[k] = v
109+
}
110+
}
111+
assert.Equal(t, targetIDToName, gotTargets)
112+
assert.Equal(t, attrStrValIDToVal, gotAttrVals)
113+
}

0 commit comments

Comments
 (0)