test: M-03 添加边界测试

- 添加大量数据边界测试(100条记录)
- 添加特殊字符 PackID 边界测试
- 添加空字段验证边界测试
This commit is contained in:
phamnazage-jpg
2026-06-02 06:58:45 +08:00
parent b69cb9166e
commit 133da2d442

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"testing"
)
@@ -337,3 +338,110 @@ func TestPacksRepoListAll(t *testing.T) {
t.Fatalf("ListAll() ids = [%q %q], want [pack-a pack-b]", packs[0].PackID, packs[1].PackID)
}
}
// 边界测试:大量数据
func TestPacksRepoListAll_Boundary_LargeDataset(t *testing.T) {
store := openTestDB(t)
ctx := context.Background()
const count = 100
for i := 0; i < count; i++ {
pack := Pack{
PackID: fmt.Sprintf("pack-%d", i),
Version: "1.0.0",
Checksum: fmt.Sprintf("checksum-%d", i),
}
if _, err := store.Packs().Create(ctx, pack); err != nil {
t.Fatalf("Create(pack-%d) error = %v", i, err)
}
}
packs, err := store.Packs().ListAll(ctx)
if err != nil {
t.Fatalf("ListAll() error = %v", err)
}
if len(packs) != count {
t.Fatalf("ListAll() len = %d, want %d", len(packs), count)
}
}
// 边界测试:特殊字符 PackID
func TestPacksRepoUpsert_Boundary_SpecialChars(t *testing.T) {
store := openTestDB(t)
ctx := context.Background()
specialIDs := []string{
"pack-with-dash",
"pack_with_underscore",
"pack.with.dot",
"pack:with:colon",
"pack123numeric",
"UPPERCASE",
"mixedCase123",
}
for _, id := range specialIDs {
pack := Pack{
PackID: id,
Version: "1.0.0",
Checksum: "checksum",
}
_, err := store.Packs().Upsert(ctx, pack)
if err != nil {
t.Errorf("Upsert(%q) error = %v", id, err)
continue
}
// Verify by reading back
got, err := store.Packs().GetByPackID(ctx, id)
if err != nil {
t.Errorf("GetByPackID(%q) after Upsert error = %v", id, err)
continue
}
if got.PackID != id {
t.Errorf("Upsert(%q) stored PackID = %q", id, got.PackID)
}
}
}
// 边界测试:空字符串字段
func TestPacksRepoCreate_Boundary_EmptyFields(t *testing.T) {
store := openTestDB(t)
ctx := context.Background()
testCases := []struct {
name string
pack Pack
wantErr bool
}{
{
name: "empty version",
pack: Pack{
PackID: "pack-empty-version",
Version: "",
Checksum: "checksum",
},
wantErr: true, // 空版本不被允许
},
{
name: "empty checksum",
pack: Pack{
PackID: "pack-empty-checksum",
Version: "1.0.0",
Checksum: "",
},
wantErr: true, // 空校验和不被允许
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := store.Packs().Create(ctx, tc.pack)
if tc.wantErr && err == nil {
t.Errorf("Create() expected error, got nil")
}
if !tc.wantErr && err != nil {
t.Errorf("Create() unexpected error = %v", err)
}
})
}
}