188 lines
4.7 KiB
Go
188 lines
4.7 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/syonad/clonepack/internal/store"
|
|
)
|
|
|
|
func newTestStore(t *testing.T) *store.SQLiteRepoStore {
|
|
t.Helper()
|
|
return store.NewRepoStore(newTestDB(t))
|
|
}
|
|
|
|
func TestCreateAndGetRepo(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
r := &store.Repo{Name: "test-repo", Type: "rpm", SourceURL: "https://example.com", SyncMode: "auto"}
|
|
id, err := s.CreateRepo(ctx, r)
|
|
if err != nil {
|
|
t.Fatalf("CreateRepo: %v", err)
|
|
}
|
|
if id <= 0 {
|
|
t.Errorf("expected positive id, got %d", id)
|
|
}
|
|
|
|
got, err := s.GetRepo(ctx, id)
|
|
if err != nil {
|
|
t.Fatalf("GetRepo: %v", err)
|
|
}
|
|
if got.Name != "test-repo" {
|
|
t.Errorf("Name: got %q, want %q", got.Name, "test-repo")
|
|
}
|
|
if got.Type != "rpm" {
|
|
t.Errorf("Type: got %q, want %q", got.Type, "rpm")
|
|
}
|
|
if got.SourceURL != "https://example.com" {
|
|
t.Errorf("SourceURL: got %q", got.SourceURL)
|
|
}
|
|
if got.SyncMode != "auto" {
|
|
t.Errorf("SyncMode: got %q, want auto", got.SyncMode)
|
|
}
|
|
}
|
|
|
|
func TestGetRepo_notFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := s.GetRepo(ctx, 9999)
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestGetRepoByName(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
r := &store.Repo{Name: "debian-bookworm", Type: "apt", SourceURL: "https://deb.debian.org/debian", SyncMode: "manual", Config: `{"suite":"bookworm"}`}
|
|
id, err := s.CreateRepo(ctx, r)
|
|
if err != nil {
|
|
t.Fatalf("CreateRepo: %v", err)
|
|
}
|
|
|
|
got, err := s.GetRepoByName(ctx, "debian-bookworm")
|
|
if err != nil {
|
|
t.Fatalf("GetRepoByName: %v", err)
|
|
}
|
|
if got.ID != id {
|
|
t.Errorf("ID: got %d, want %d", got.ID, id)
|
|
}
|
|
if got.Config != `{"suite":"bookworm"}` {
|
|
t.Errorf("Config: got %q", got.Config)
|
|
}
|
|
}
|
|
|
|
func TestGetRepoByName_notFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
_, err := s.GetRepoByName(ctx, "does-not-exist")
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestListRepos(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
repos, err := s.ListRepos(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListRepos (empty): %v", err)
|
|
}
|
|
if len(repos) != 0 {
|
|
t.Errorf("expected empty list, got %d repos", len(repos))
|
|
}
|
|
|
|
for _, name := range []string{"repo-a", "repo-b", "repo-c"} {
|
|
_, err := s.CreateRepo(ctx, &store.Repo{Name: name, Type: "rpm", SourceURL: "https://x.com", SyncMode: "auto"})
|
|
if err != nil {
|
|
t.Fatalf("CreateRepo %q: %v", name, err)
|
|
}
|
|
}
|
|
|
|
repos, err = s.ListRepos(ctx)
|
|
if err != nil {
|
|
t.Fatalf("ListRepos: %v", err)
|
|
}
|
|
if len(repos) != 3 {
|
|
t.Errorf("expected 3 repos, got %d", len(repos))
|
|
}
|
|
}
|
|
|
|
func TestDeleteRepo(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
id, _ := s.CreateRepo(ctx, &store.Repo{Name: "to-delete", Type: "rpm", SourceURL: "https://x.com", SyncMode: "auto"})
|
|
|
|
if err := s.DeleteRepo(ctx, id); err != nil {
|
|
t.Fatalf("DeleteRepo: %v", err)
|
|
}
|
|
_, err := s.GetRepo(ctx, id)
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound after delete, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteRepo_notFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
err := s.DeleteRepo(ctx, 9999)
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRepo(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
id, _ := s.CreateRepo(ctx, &store.Repo{Name: "updatable", Type: "apt", SourceURL: "https://old.example.com", SyncMode: "auto", Config: ""})
|
|
|
|
newConfig := `{"suite":"bookworm","components":["main"],"architectures":["amd64"]}`
|
|
if err := s.UpdateRepo(ctx, id, "https://new.example.com", "manual", newConfig); err != nil {
|
|
t.Fatalf("UpdateRepo: %v", err)
|
|
}
|
|
|
|
got, err := s.GetRepo(ctx, id)
|
|
if err != nil {
|
|
t.Fatalf("GetRepo after update: %v", err)
|
|
}
|
|
if got.SourceURL != "https://new.example.com" {
|
|
t.Errorf("SourceURL: got %q, want https://new.example.com", got.SourceURL)
|
|
}
|
|
if got.SyncMode != "manual" {
|
|
t.Errorf("SyncMode: got %q, want manual", got.SyncMode)
|
|
}
|
|
if got.Config != newConfig {
|
|
t.Errorf("Config: got %q", got.Config)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRepo_notFound(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
err := s.UpdateRepo(ctx, 9999, "https://x.com", "auto", "")
|
|
if err != store.ErrNotFound {
|
|
t.Errorf("expected ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCreateRepo_uniqueName(t *testing.T) {
|
|
s := newTestStore(t)
|
|
ctx := context.Background()
|
|
|
|
r := &store.Repo{Name: "unique", Type: "rpm", SourceURL: "https://x.com", SyncMode: "auto"}
|
|
if _, err := s.CreateRepo(ctx, r); err != nil {
|
|
t.Fatalf("first create: %v", err)
|
|
}
|
|
if _, err := s.CreateRepo(ctx, r); err == nil {
|
|
t.Error("expected error for duplicate name, got nil")
|
|
}
|
|
}
|