init
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package galleries
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
appdb "github.com/example/sndit/backend/internal/db"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestRepositoryHandlesSQLiteGallerySchema(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
database, err := appdb.New(ctx, "sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite database: %v", err)
|
||||
}
|
||||
defer database.Close()
|
||||
|
||||
_, sourceFile, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("find test source file")
|
||||
}
|
||||
migrationPath := filepath.Join(filepath.Dir(sourceFile), "..", "..", "..", "migrations", "sqlite", "003_gallery_platform.sql")
|
||||
migration, err := os.ReadFile(migrationPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read gallery migration: %v", err)
|
||||
}
|
||||
if _, err := database.ExecContext(ctx, string(migration)); err != nil {
|
||||
t.Fatalf("apply gallery migration: %v", err)
|
||||
}
|
||||
|
||||
userID := uuid.MustParse("55555555-5555-4555-8555-555555555555")
|
||||
if _, err := database.ExecContext(ctx, `INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, $3, $4)`, userID, "demo@example.com", "hash", "Northline Studio"); err != nil {
|
||||
t.Fatalf("insert user: %v", err)
|
||||
}
|
||||
repository := NewRepository(database)
|
||||
record, err := repository.Create(ctx, userID, "emma-james-wedding-12345678", "Emma & James", "Emma & James", "A summer wedding.")
|
||||
if err != nil {
|
||||
t.Fatalf("create gallery: %v", err)
|
||||
}
|
||||
if record.Status != StatusDraft || record.Title != "Emma & James" {
|
||||
t.Fatalf("unexpected gallery: %+v", record)
|
||||
}
|
||||
|
||||
mediaID := uuid.MustParse("77777777-7777-4777-8777-777777777777")
|
||||
if _, err := database.ExecContext(ctx, `
|
||||
INSERT INTO media (id, gallery_id, original_filename, mime_type, file_size, storage_key, processing_status, sort_order)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, 'READY', 0)
|
||||
`, mediaID, record.ID, "one.jpg", "image/jpeg", 4096, "gallery/one.jpg"); err != nil {
|
||||
t.Fatalf("insert media: %v", err)
|
||||
}
|
||||
|
||||
updated, err := repository.Update(ctx, userID, record.ID, UpdateInput{
|
||||
Title: record.Title,
|
||||
ClientName: record.ClientName,
|
||||
Description: record.Description,
|
||||
DownloadsEnabled: true,
|
||||
FavoritesEnabled: true,
|
||||
DownloadAllEnabled: true,
|
||||
ThemeConfig: json.RawMessage(`{"mode":"dark"}`),
|
||||
BrandingConfig: json.RawMessage(`{"studioName":"Northline"}`),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("update gallery: %v", err)
|
||||
}
|
||||
if string(updated.ThemeConfig) != `{"mode":"dark"}` {
|
||||
t.Fatalf("theme config was not persisted: %s", updated.ThemeConfig)
|
||||
}
|
||||
|
||||
summaries, err := repository.ListForUser(ctx, userID)
|
||||
if err != nil {
|
||||
t.Fatalf("list galleries: %v", err)
|
||||
}
|
||||
if len(summaries) != 1 || summaries[0].PhotoCount != 1 || summaries[0].TotalBytes != 4096 {
|
||||
t.Fatalf("unexpected summary: %+v", summaries)
|
||||
}
|
||||
if _, err := repository.SetStatus(ctx, userID, record.ID, StatusPublished); err != nil {
|
||||
t.Fatalf("publish gallery: %v", err)
|
||||
}
|
||||
if _, err := repository.GetPublicBySlug(ctx, record.Slug); err != nil {
|
||||
t.Fatalf("public gallery lookup: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user