131 lines
3.7 KiB
Go
131 lines
3.7 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/syonad/clonepack/internal/store"
|
|
)
|
|
|
|
func newBlockedStore(t *testing.T) (*store.SQLiteBlockedPackageStore, int64) {
|
|
t.Helper()
|
|
db := newTestDB(t)
|
|
repoID := insertTestRepo(t, db)
|
|
return store.NewBlockedPackageStore(db), repoID
|
|
}
|
|
|
|
func TestBlockAndListPackages(t *testing.T) {
|
|
s, repoID := newBlockedStore(t)
|
|
ctx := context.Background()
|
|
|
|
pkgs := []store.BlockedPackage{
|
|
{Name: "curl", Location: "Packages/curl-7.88.rpm"},
|
|
{Name: "wget", Location: "Packages/wget-1.21.rpm"},
|
|
}
|
|
if err := s.BlockPackages(ctx, repoID, pkgs); err != nil {
|
|
t.Fatalf("BlockPackages: %v", err)
|
|
}
|
|
|
|
listed, err := s.ListBlocked(ctx, repoID)
|
|
if err != nil {
|
|
t.Fatalf("ListBlocked: %v", err)
|
|
}
|
|
if len(listed) != 2 {
|
|
t.Fatalf("expected 2 blocked, got %d", len(listed))
|
|
}
|
|
if listed[0].Name != "curl" {
|
|
t.Errorf("expected curl first, got %q", listed[0].Name)
|
|
}
|
|
if listed[0].RepoID != repoID {
|
|
t.Errorf("RepoID: got %d, want %d", listed[0].RepoID, repoID)
|
|
}
|
|
}
|
|
|
|
func TestBlockPackages_idempotent(t *testing.T) {
|
|
s, repoID := newBlockedStore(t)
|
|
ctx := context.Background()
|
|
|
|
pkg := []store.BlockedPackage{{Name: "curl", Location: "Packages/curl.rpm"}}
|
|
s.BlockPackages(ctx, repoID, pkg)
|
|
// INSERT OR IGNORE — should not duplicate.
|
|
s.BlockPackages(ctx, repoID, pkg)
|
|
|
|
listed, _ := s.ListBlocked(ctx, repoID)
|
|
if len(listed) != 1 {
|
|
t.Errorf("expected 1 after double block, got %d", len(listed))
|
|
}
|
|
}
|
|
|
|
func TestUnblockPackages(t *testing.T) {
|
|
s, repoID := newBlockedStore(t)
|
|
ctx := context.Background()
|
|
|
|
s.BlockPackages(ctx, repoID, []store.BlockedPackage{
|
|
{Name: "curl", Location: "Packages/curl.rpm"},
|
|
{Name: "wget", Location: "Packages/wget.rpm"},
|
|
})
|
|
|
|
listed, _ := s.ListBlocked(ctx, repoID)
|
|
if len(listed) != 2 {
|
|
t.Fatalf("setup: expected 2, got %d", len(listed))
|
|
}
|
|
|
|
// Unblock only the first.
|
|
if err := s.UnblockPackages(ctx, repoID, []int64{listed[0].ID}); err != nil {
|
|
t.Fatalf("UnblockPackages: %v", err)
|
|
}
|
|
|
|
remaining, _ := s.ListBlocked(ctx, repoID)
|
|
if len(remaining) != 1 {
|
|
t.Fatalf("expected 1 remaining, got %d", len(remaining))
|
|
}
|
|
if remaining[0].Name != "wget" {
|
|
t.Errorf("expected wget remaining, got %q", remaining[0].Name)
|
|
}
|
|
}
|
|
|
|
func TestUnblockPackages_empty(t *testing.T) {
|
|
s, repoID := newBlockedStore(t)
|
|
ctx := context.Background()
|
|
|
|
if err := s.UnblockPackages(ctx, repoID, nil); err != nil {
|
|
t.Errorf("UnblockPackages(nil): %v", err)
|
|
}
|
|
if err := s.UnblockPackages(ctx, repoID, []int64{}); err != nil {
|
|
t.Errorf("UnblockPackages([]): %v", err)
|
|
}
|
|
}
|
|
|
|
func TestListBlocked_empty(t *testing.T) {
|
|
s, repoID := newBlockedStore(t)
|
|
listed, err := s.ListBlocked(context.Background(), repoID)
|
|
if err != nil {
|
|
t.Fatalf("ListBlocked: %v", err)
|
|
}
|
|
if len(listed) != 0 {
|
|
t.Errorf("expected 0, got %d", len(listed))
|
|
}
|
|
}
|
|
|
|
func TestBlockPackages_isolation(t *testing.T) {
|
|
db := newTestDB(t)
|
|
repoStore := store.NewRepoStore(db)
|
|
ctx := context.Background()
|
|
|
|
id1, _ := repoStore.CreateRepo(ctx, &store.Repo{Name: "repo-1", Type: "rpm", SourceURL: "https://a.com", SyncMode: "auto"})
|
|
id2, _ := repoStore.CreateRepo(ctx, &store.Repo{Name: "repo-2", Type: "rpm", SourceURL: "https://b.com", SyncMode: "auto"})
|
|
|
|
bs := store.NewBlockedPackageStore(db)
|
|
bs.BlockPackages(ctx, id1, []store.BlockedPackage{{Name: "curl", Location: "pkg/curl.rpm"}})
|
|
bs.BlockPackages(ctx, id2, []store.BlockedPackage{{Name: "vim", Location: "pkg/vim.rpm"}})
|
|
|
|
list1, _ := bs.ListBlocked(ctx, id1)
|
|
list2, _ := bs.ListBlocked(ctx, id2)
|
|
|
|
if len(list1) != 1 || list1[0].Name != "curl" {
|
|
t.Errorf("repo-1: expected [curl], got %v", list1)
|
|
}
|
|
if len(list2) != 1 || list2[0].Name != "vim" {
|
|
t.Errorf("repo-2: expected [vim], got %v", list2)
|
|
}
|
|
}
|