58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package gifts
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
appdb "github.com/example/sndit/backend/internal/db"
|
|
)
|
|
|
|
func TestRepositoryReadsSQLiteMigrations(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")
|
|
}
|
|
migrationDirectory := filepath.Join(filepath.Dir(sourceFile), "..", "..", "..", "migrations", "sqlite")
|
|
entries, err := os.ReadDir(migrationDirectory)
|
|
if err != nil {
|
|
t.Fatalf("read sqlite migrations: %v", err)
|
|
}
|
|
sort.Slice(entries, func(i, j int) bool { return entries[i].Name() < entries[j].Name() })
|
|
|
|
for _, entry := range entries {
|
|
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".sql") {
|
|
continue
|
|
}
|
|
migration, err := os.ReadFile(filepath.Join(migrationDirectory, entry.Name()))
|
|
if err != nil {
|
|
t.Fatalf("read migration %s: %v", entry.Name(), err)
|
|
}
|
|
if _, err := database.ExecContext(ctx, string(migration)); err != nil {
|
|
t.Fatalf("apply migration %s: %v", entry.Name(), err)
|
|
}
|
|
}
|
|
|
|
gift, err := NewRepository(database).GetPublicBySlug(ctx, "demo")
|
|
if err != nil {
|
|
t.Fatalf("load demo gift: %v", err)
|
|
}
|
|
if gift.RecipientName != "Anna" || gift.SenderName != "Alex" {
|
|
t.Fatalf("unexpected gift: %+v", gift)
|
|
}
|
|
if len(gift.Items) != 3 || gift.Items[0].Type != "image" || gift.Items[1].SortOrder != 2 {
|
|
t.Fatalf("unexpected gift items: %+v", gift.Items)
|
|
}
|
|
}
|