From 133da2d442619c1d80190e431049e378a64de430 Mon Sep 17 00:00:00 2001 From: phamnazage-jpg Date: Tue, 2 Jun 2026 06:58:45 +0800 Subject: [PATCH] =?UTF-8?q?test:=20M-03=20=E6=B7=BB=E5=8A=A0=E8=BE=B9?= =?UTF-8?q?=E7=95=8C=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加大量数据边界测试(100条记录) - 添加特殊字符 PackID 边界测试 - 添加空字段验证边界测试 --- internal/store/sqlite/packs_repo_test.go | 108 +++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/internal/store/sqlite/packs_repo_test.go b/internal/store/sqlite/packs_repo_test.go index 6aa48264..f6e4d7fa 100644 --- a/internal/store/sqlite/packs_repo_test.go +++ b/internal/store/sqlite/packs_repo_test.go @@ -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) + } + }) + } +}