Migration 0002 replaces finding.message with title/fix/sources (a Postgres text[]). A finding needs to render into the dossier the way it looked at the moment it was raised — referencing the current rules/*.yaml by rule_id+version isn't safe once that file is edited for a later version, since old wording isn't kept around as a separate live file. Added as a new migration rather than editing 0001, since that's already applied on the test server. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
package store_test
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/netcell-it/deklarix/internal/store"
|
|
)
|
|
|
|
// Diese Tests brauchen eine laufende Postgres-Instanz und werden ohne
|
|
// DATABASE_URL übersprungen, statt eine Verbindung vorzutäuschen.
|
|
func testDatabaseURL(t *testing.T) string {
|
|
t.Helper()
|
|
url := os.Getenv("DATABASE_URL")
|
|
if url == "" {
|
|
t.Skip("DATABASE_URL nicht gesetzt, überspringe Store-Integrationstest")
|
|
}
|
|
return url
|
|
}
|
|
|
|
func TestMigrateAndOpen(t *testing.T) {
|
|
url := testDatabaseURL(t)
|
|
|
|
if err := store.Migrate(url); err != nil {
|
|
t.Fatalf("Migrate: %v", err)
|
|
}
|
|
|
|
s, err := store.Open(context.Background(), url)
|
|
if err != nil {
|
|
t.Fatalf("Open: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
var tableCount int
|
|
err = s.Pool.QueryRow(context.Background(), `
|
|
SELECT count(*) FROM information_schema.tables
|
|
WHERE table_schema = 'public' AND table_name = ANY($1)
|
|
`, []string{"submission", "asset", "extraction", "finding", "evidence_package", "participant"}).Scan(&tableCount)
|
|
if err != nil {
|
|
t.Fatalf("query tables: %v", err)
|
|
}
|
|
if tableCount != 6 {
|
|
t.Fatalf("expected 6 tables, got %d", tableCount)
|
|
}
|
|
}
|
|
|
|
func TestFindingIsAppendOnly(t *testing.T) {
|
|
url := testDatabaseURL(t)
|
|
|
|
if err := store.Migrate(url); err != nil {
|
|
t.Fatalf("Migrate: %v", err)
|
|
}
|
|
|
|
s, err := store.Open(context.Background(), url)
|
|
if err != nil {
|
|
t.Fatalf("Open: %v", err)
|
|
}
|
|
defer s.Close()
|
|
|
|
ctx := context.Background()
|
|
|
|
var submissionID string
|
|
err = s.Pool.QueryRow(ctx, `
|
|
INSERT INTO submission (platform, post_type) VALUES ('instagram', 'reel')
|
|
RETURNING id
|
|
`).Scan(&submissionID)
|
|
if err != nil {
|
|
t.Fatalf("insert submission: %v", err)
|
|
}
|
|
|
|
var findingID string
|
|
err = s.Pool.QueryRow(ctx, `
|
|
INSERT INTO finding (submission_id, rule_id, rule_version, severity, title, fix, sources)
|
|
VALUES ($1, 'WK-004', 3, 'hoch', 'Testfeststellung', 'Testkorrektur', '{}')
|
|
RETURNING id
|
|
`, submissionID).Scan(&findingID)
|
|
if err != nil {
|
|
t.Fatalf("insert finding: %v", err)
|
|
}
|
|
|
|
_, err = s.Pool.Exec(ctx, `UPDATE finding SET title = 'geändert' WHERE id = $1`, findingID)
|
|
if err == nil {
|
|
t.Fatal("expected UPDATE on finding to be rejected, but it succeeded")
|
|
}
|
|
}
|