fix: actor tests

This commit is contained in:
2026-04-01 20:37:38 -07:00
parent 789e32a57f
commit 0308c70061
3 changed files with 128 additions and 76 deletions
+5 -5
View File
@@ -13,7 +13,6 @@ import (
v1 "git.tipsy.codes/charles/webstory/pkg/api/webstory/v1" v1 "git.tipsy.codes/charles/webstory/pkg/api/webstory/v1"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/lib/pq"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
) )
@@ -180,8 +179,8 @@ func (a *DBActor) insert(db *sql.DB) error {
a.StoryID, a.StoryID,
a.ActorID, a.ActorID,
a.NameValue, a.NameValue,
pq.Array([]string{a.Role}), a.Role,
pq.Array([]string{a.Notes}), a.Notes,
a.CreatedAt, a.CreatedAt,
a.UpdatedAt, a.UpdatedAt,
etag, etag,
@@ -396,11 +395,12 @@ func GetActorIDForResourceName(resourceName string) (int64, string, error) {
remaining := strings.TrimPrefix(resourceName, prefix) remaining := strings.TrimPrefix(resourceName, prefix)
actorPrefix := "actors/" actorPrefix := "actors/"
if !strings.HasPrefix(remaining, actorPrefix) { idx := strings.Index(remaining, actorPrefix)
if idx == -1 {
return 0, "", fmt.Errorf("invalid actor resource name: must contain '%s', got '%s'", actorPrefix, remaining) return 0, "", fmt.Errorf("invalid actor resource name: must contain '%s', got '%s'", actorPrefix, remaining)
} }
actorID := strings.TrimPrefix(remaining, actorPrefix) actorID := remaining[idx+len(actorPrefix):]
if actorID == "" { if actorID == "" {
return 0, "", errors.New("actor_id is empty in resource name") return 0, "", errors.New("actor_id is empty in resource name")
} }
+62 -10
View File
@@ -11,6 +11,8 @@ import (
v1 "git.tipsy.codes/charles/webstory/pkg/api/webstory/v1" v1 "git.tipsy.codes/charles/webstory/pkg/api/webstory/v1"
"git.tipsy.codes/charles/webstory/pkg/database/schema" "git.tipsy.codes/charles/webstory/pkg/database/schema"
story "git.tipsy.codes/charles/webstory/pkg/database/story"
embeddedpostgres "github.com/fergusstrange/embedded-postgres" embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
) )
@@ -222,9 +224,19 @@ func TestDBActor_Save(t *testing.T) {
t.Fatalf("failed to run schema: %v", err) t.Fatalf("failed to run schema: %v", err)
} }
// Create a story first
story := &story.DBStory{
StoryID: "save-test-story-1",
Title: "Save Test Story",
}
err = story.Save(db)
if err != nil {
t.Fatalf("failed to save story: %v", err)
}
actor := &DBActor{ actor := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "save-test1", ActorID: "save-test-1",
NameValue: "Save Test", NameValue: "Save Test",
Role: "Test Role", Role: "Test Role",
Notes: "Test notes", Notes: "Test notes",
@@ -291,9 +303,19 @@ func TestDBActor_GetByID(t *testing.T) {
t.Fatalf("failed to run schema: %v", err) t.Fatalf("failed to run schema: %v", err)
} }
// Create a story first
story := &story.DBStory{
StoryID: "get-by-id-story-1",
Title: "Get By ID Story",
}
err = story.Save(db)
if err != nil {
t.Fatalf("failed to save story: %v", err)
}
actor := &DBActor{ actor := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "get-by-id-test", ActorID: "get-by-id-test-1",
NameValue: "Get Test", NameValue: "Get Test",
} }
err = actor.Save(db) err = actor.Save(db)
@@ -340,9 +362,19 @@ func TestDBActor_GetByActorID(t *testing.T) {
t.Fatalf("failed to run schema: %v", err) t.Fatalf("failed to run schema: %v", err)
} }
// Create a story first
story := &story.DBStory{
StoryID: "get-by-actor-id-story-1",
Title: "Get By Actor ID Story",
}
err = story.Save(db)
if err != nil {
t.Fatalf("failed to save story: %v", err)
}
actor := &DBActor{ actor := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "get-by-actor-id-test", ActorID: "get-by-actor-id-test-1",
NameValue: "Get By Actor ID Test", NameValue: "Get By Actor ID Test",
} }
err = actor.Save(db) err = actor.Save(db)
@@ -386,14 +418,24 @@ func TestDBActor_ListByStoryID(t *testing.T) {
t.Fatalf("failed to run schema: %v", err) t.Fatalf("failed to run schema: %v", err)
} }
// Create a story first
story := &story.DBStory{
StoryID: "list-test-story-1",
Title: "List Test Story",
}
err = story.Save(db)
if err != nil {
t.Fatalf("failed to save story: %v", err)
}
actor1 := &DBActor{ actor1 := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "list-test-1", ActorID: "list-test-1",
NameValue: "Actor One", NameValue: "Actor One",
Role: "Role One", Role: "Role One",
} }
actor2 := &DBActor{ actor2 := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "list-test-2", ActorID: "list-test-2",
NameValue: "Actor Two", NameValue: "Actor Two",
Role: "Role Two", Role: "Role Two",
@@ -448,9 +490,19 @@ func TestDBActor_Delete(t *testing.T) {
t.Fatalf("failed to run schema: %v", err) t.Fatalf("failed to run schema: %v", err)
} }
// Create a story first
story := &story.DBStory{
StoryID: "delete-test-story-1",
Title: "Delete Test Story",
}
err = story.Save(db)
if err != nil {
t.Fatalf("failed to save story: %v", err)
}
actor := &DBActor{ actor := &DBActor{
StoryID: 1, StoryID: story.ID,
ActorID: "delete-test-1", ActorID: "delete-test-actor-1",
NameValue: "Delete Test", NameValue: "Delete Test",
} }
err = actor.Save(db) err = actor.Save(db)
+1 -1
View File
@@ -115,7 +115,7 @@ CREATE TABLE IF NOT EXISTS actors (
-- Constraints -- Constraints
CONSTRAINT actors_story_actor_id_unique UNIQUE (story_id, actor_id), CONSTRAINT actors_story_actor_id_unique UNIQUE (story_id, actor_id),
CONSTRAINT actors_story_id_exists CHECK (story_id > 0), CONSTRAINT actors_story_id_exists CHECK (story_id > 0),
CONSTRAINT actors_actor_id_pattern CHECK (actor_id ~ '^[a-z][0-9-]{2,61}[0-9]$'), CONSTRAINT actors_actor_id_pattern CHECK (actor_id ~ '^[a-z][0-9a-z-]{2,61}[0-9a-z]$'),
CONSTRAINT actors_etag_pattern CHECK (char_length(etag) > 0) CONSTRAINT actors_etag_pattern CHECK (char_length(etag) > 0)
); );