142 lines
3.8 KiB
Go
142 lines
3.8 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/syonad/clonepack/internal/store"
|
|
)
|
|
|
|
func newSnapshotStore(t *testing.T) (*store.SQLiteSnapshotStore, int64) {
|
|
t.Helper()
|
|
db := newTestDB(t)
|
|
repoID := insertTestRepo(t, db)
|
|
return store.NewSnapshotStore(db), repoID
|
|
}
|
|
|
|
func TestCreateAndGetSnapshot(t *testing.T) {
|
|
s, repoID := newSnapshotStore(t)
|
|
ctx := context.Background()
|
|
|
|
id, err := s.CreateSnapshot(ctx, repoID, "v1.0")
|
|
if err != nil {
|
|
t.Fatalf("CreateSnapshot: %v", err)
|
|
}
|
|
|
|
snap, err := s.GetSnapshot(ctx, id)
|
|
if err != nil {
|
|
t.Fatalf("GetSnapshot: %v", err)
|
|
}
|
|
if snap.RepoID != repoID {
|
|
t.Errorf("RepoID: got %d, want %d", snap.RepoID, repoID)
|
|
}
|
|
if snap.Label != "v1.0" {
|
|
t.Errorf("Label: got %q, want v1.0", snap.Label)
|
|
}
|
|
}
|
|
|
|
func TestGetSnapshot_notFound(t *testing.T) {
|
|
s, _ := newSnapshotStore(t)
|
|
_, err := s.GetSnapshot(context.Background(), 9999)
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestListSnapshots(t *testing.T) {
|
|
s, repoID := newSnapshotStore(t)
|
|
ctx := context.Background()
|
|
|
|
snaps, err := s.ListSnapshots(ctx, repoID)
|
|
if err != nil {
|
|
t.Fatalf("ListSnapshots (empty): %v", err)
|
|
}
|
|
if len(snaps) != 0 {
|
|
t.Errorf("expected 0 snapshots, got %d", len(snaps))
|
|
}
|
|
|
|
s.CreateSnapshot(ctx, repoID, "snap-1")
|
|
s.CreateSnapshot(ctx, repoID, "snap-2")
|
|
|
|
snaps, err = s.ListSnapshots(ctx, repoID)
|
|
if err != nil {
|
|
t.Fatalf("ListSnapshots: %v", err)
|
|
}
|
|
if len(snaps) != 2 {
|
|
t.Fatalf("expected 2 snapshots, got %d", len(snaps))
|
|
}
|
|
}
|
|
|
|
func TestAddAndGetSnapshotPackages(t *testing.T) {
|
|
s, repoID := newSnapshotStore(t)
|
|
ctx := context.Background()
|
|
|
|
snapID, _ := s.CreateSnapshot(ctx, repoID, "with-pkgs")
|
|
|
|
pkgs := []store.SnapshotPackage{
|
|
{Name: "curl", Version: "7.88", Arch: "amd64", Location: "Packages/curl.rpm", Checksum: "abc", ChecksumType: "sha256", Size: 1000},
|
|
{Name: "wget", Version: "1.21", Arch: "amd64", Location: "Packages/wget.rpm", Checksum: "def", ChecksumType: "sha256", Size: 2000},
|
|
}
|
|
if err := s.AddSnapshotPackages(ctx, snapID, pkgs); err != nil {
|
|
t.Fatalf("AddSnapshotPackages: %v", err)
|
|
}
|
|
|
|
got, err := s.GetSnapshotPackages(ctx, snapID)
|
|
if err != nil {
|
|
t.Fatalf("GetSnapshotPackages: %v", err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("expected 2 packages, got %d", len(got))
|
|
}
|
|
if got[0].Name != "curl" {
|
|
t.Errorf("pkg[0].Name: got %q, want curl", got[0].Name)
|
|
}
|
|
if got[1].Size != 2000 {
|
|
t.Errorf("pkg[1].Size: got %d, want 2000", got[1].Size)
|
|
}
|
|
}
|
|
|
|
func TestAddSnapshotPackages_empty(t *testing.T) {
|
|
s, repoID := newSnapshotStore(t)
|
|
ctx := context.Background()
|
|
|
|
snapID, _ := s.CreateSnapshot(ctx, repoID, "empty")
|
|
if err := s.AddSnapshotPackages(ctx, snapID, nil); err != nil {
|
|
t.Fatalf("AddSnapshotPackages with nil: %v", err)
|
|
}
|
|
pkgs, _ := s.GetSnapshotPackages(ctx, snapID)
|
|
if len(pkgs) != 0 {
|
|
t.Errorf("expected 0 packages, got %d", len(pkgs))
|
|
}
|
|
}
|
|
|
|
func TestDeleteSnapshot(t *testing.T) {
|
|
s, repoID := newSnapshotStore(t)
|
|
ctx := context.Background()
|
|
|
|
snapID, _ := s.CreateSnapshot(ctx, repoID, "to-delete")
|
|
pkgs := []store.SnapshotPackage{
|
|
{Name: "curl", Version: "7.88", Arch: "amd64", Location: "loc", Checksum: "abc", ChecksumType: "sha256", Size: 100},
|
|
}
|
|
s.AddSnapshotPackages(ctx, snapID, pkgs)
|
|
|
|
if err := s.DeleteSnapshot(ctx, snapID); err != nil {
|
|
t.Fatalf("DeleteSnapshot: %v", err)
|
|
}
|
|
if _, err := s.GetSnapshot(ctx, snapID); err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound after delete, got %v", err)
|
|
}
|
|
// Cascade: packages should also be gone.
|
|
remaining, _ := s.GetSnapshotPackages(ctx, snapID)
|
|
if len(remaining) != 0 {
|
|
t.Errorf("expected cascade delete of packages, got %d", len(remaining))
|
|
}
|
|
}
|
|
|
|
func TestDeleteSnapshot_notFound(t *testing.T) {
|
|
s, _ := newSnapshotStore(t)
|
|
err := s.DeleteSnapshot(context.Background(), 9999)
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|