Files
Mediarr/database/database_test.go
T

199 lines
4.9 KiB
Go

package database
import (
"context"
"encoding/hex"
"testing"
"time"
"mediarr/ident"
"github.com/pressly/goose/v3"
)
func TestNewDatabase(t *testing.T) {
t.Chdir(t.TempDir())
err, client := NewDatabase(NewDatabaseParams{})
if err != nil {
t.Fatalf("NewDatabase : %v", err)
}
defer client.Close()
ctx := context.Background()
var foreignKeys int
if err := client.DB.QueryRowContext(ctx, "PRAGMA foreign_keys").Scan(&foreignKeys); err != nil {
t.Fatalf("PRAGMA foreign_keys : %v", err)
}
if foreignKeys != 1 {
t.Fatalf("foreign_keys = %d, attendu 1", foreignKeys)
}
var journalMode string
if err := client.DB.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&journalMode); err != nil {
t.Fatalf("PRAGMA journal_mode : %v", err)
}
if journalMode != "wal" {
t.Fatalf("journal_mode = %q, attendu wal", journalMode)
}
var busyTimeout int
if err := client.DB.QueryRowContext(ctx, "PRAGMA busy_timeout").Scan(&busyTimeout); err != nil {
t.Fatalf("PRAGMA busy_timeout : %v", err)
}
if busyTimeout != 5000 {
t.Fatalf("busy_timeout = %d, attendu 5000", busyTimeout)
}
var synchronous int
if err := client.DB.QueryRowContext(ctx, "PRAGMA synchronous").Scan(&synchronous); err != nil {
t.Fatalf("PRAGMA synchronous : %v", err)
}
if synchronous != 1 {
t.Fatalf("synchronous = %d, attendu 1 (NORMAL)", synchronous)
}
systemID := ident.MustParse("00000000-0000-7000-8000-000000000001")
system, err := client.Queries.GetPrincipal(ctx, systemID)
if err != nil {
t.Fatalf("GetPrincipal(system) : %v", err)
}
if system.Kind != "system" {
t.Fatalf("kind = %q, attendu system", system.Kind)
}
adminsID := ident.MustParse("00000000-0000-7000-8000-000000000002")
_, err = client.WriteQueries.CreateMembership(ctx, CreateMembershipParams{
GroupID: adminsID,
MemberID: adminsID,
Role: "member",
CreatedAt: time.Now().UnixMilli(),
})
if err == nil {
t.Fatal("membership invalide (groupe dans groupe) acceptée")
}
policyID := ident.MustParse("00000000-0000-7000-8000-000000000101")
rootID, err := ident.NewV7()
if err != nil {
t.Fatal(err)
}
rootPath := hex.EncodeToString(rootID.Bytes()) + "/"
insertDocument(ctx, t, client, rootID, "library", nil, "Films", rootPath, 0, &policyID, "CLUSTER")
childID, err := ident.NewV7()
if err != nil {
t.Fatal(err)
}
childPath := rootPath + hex.EncodeToString(childID.Bytes()) + "/"
insertDocument(ctx, t, client, childID, "folder", &rootID, "Action", childPath, 1, nil, "")
badPathID, err := ident.NewV7()
if err != nil {
t.Fatal(err)
}
badPath := "/deadbeef/" + hex.EncodeToString(badPathID.Bytes()) + "/"
if insertDocumentErr(ctx, client, badPathID, "folder", &rootID, "Bad", badPath, 1, nil, "") == nil {
t.Fatal("document avec path incohérent accepté")
}
badDepthID, err := ident.NewV7()
if err != nil {
t.Fatal(err)
}
badDepthPath := rootPath + hex.EncodeToString(badDepthID.Bytes()) + "/"
if insertDocumentErr(ctx, client, badDepthID, "folder", &rootID, "Bad", badDepthPath, 5, nil, "") == nil {
t.Fatal("document avec profondeur incohérente accepté")
}
}
func insertDocument(
ctx context.Context,
t *testing.T,
client *DatabaseClient,
id ident.UUID,
kind string,
parentID *ident.UUID,
name string,
path string,
depth int,
policyID *ident.UUID,
visibility string,
) {
t.Helper()
if err := insertDocumentErr(ctx, client, id, kind, parentID, name, path, depth, policyID, visibility); err != nil {
t.Fatalf("insertion document %q : %v", name, err)
}
}
func insertDocumentErr(
ctx context.Context,
client *DatabaseClient,
id ident.UUID,
kind string,
parentID *ident.UUID,
name string,
path string,
depth int,
policyID *ident.UUID,
visibility string,
) error {
var visibilityValue any
if visibility != "" {
visibilityValue = visibility
}
_, err := client.WriteDB.ExecContext(ctx, `
INSERT INTO document (
id, kind, owner_id, created_by, parent_id, name, name_key, path, depth,
policy_id, visibility, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
id,
kind,
ident.MustParse("00000000-0000-7000-8000-000000000001"),
ident.MustParse("00000000-0000-7000-8000-000000000001"),
parentID,
name,
name,
path,
depth,
policyID,
visibilityValue,
time.Now().UnixMilli(),
time.Now().UnixMilli(),
)
return err
}
func TestMigrationsDown(t *testing.T) {
t.Chdir(t.TempDir())
err, client := NewDatabase(NewDatabaseParams{})
if err != nil {
t.Fatalf("NewDatabase : %v", err)
}
defer client.Close()
if err := goose.DownTo(client.WriteDB, "migrations", 0); err != nil {
t.Fatalf("goose DownTo : %v", err)
}
var count int
err = client.DB.QueryRowContext(
context.Background(),
"SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name = 'document'",
).Scan(&count)
if err != nil {
t.Fatalf("inspection sqlite_master : %v", err)
}
if count != 0 {
t.Fatal("la table document existe encore après le rollback")
}
}