Add Actor model with unit tests and integration tests
Add Actor model implementation in pkg/database/actor/actor.go with associated unit tests in actor_test.go. Implement tests following the pattern used for Story model, including Postgres integration tests.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
// Package actor provides database access for the Actor resource.
|
||||
package actor
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
v1 "git.tipsy.codes/charles/webstory/pkg/api/webstory/v1"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
func TestActorTranslator_FromAPI(t *testing.T) {
|
||||
now := time.Now()
|
||||
tests := []struct {
|
||||
name string
|
||||
apiActor *v1.Actor
|
||||
expectErr bool
|
||||
errMsg string
|
||||
expectID int64
|
||||
checkFunc func(*DBActor) error
|
||||
}{
|
||||
{
|
||||
name: "nil actor returns error",
|
||||
apiActor: nil,
|
||||
expectErr: true,
|
||||
errMsg: "api actor cannot be nil",
|
||||
},
|
||||
{
|
||||
name: "minimal valid actor creates translator correctly",
|
||||
apiActor: &v1.Actor{
|
||||
ActorId: "actor-1",
|
||||
NameValue: "John Doe",
|
||||
Role: "Hero",
|
||||
Notes: "Main character",
|
||||
CreateTime: timestamppb.New(now),
|
||||
UpdateTime: timestamppb.New(now),
|
||||
},
|
||||
expectErr: false,
|
||||
expectID: 0,
|
||||
checkFunc: func(s *DBActor) error {
|
||||
if s.ActorID != "actor-1" {
|
||||
return fmt.Errorf("expected ActorID actor-1, got %s", s.ActorID)
|
||||
}
|
||||
if s.NameValue != "John Doe" {
|
||||
return fmt.Errorf("expected NameValue John Doe, got %s", s.NameValue)
|
||||
}
|
||||
if s.Role != "Hero" {
|
||||
return fmt.Errorf("expected Role Hero, got %s", s.Role)
|
||||
}
|
||||
if s.Notes != "Main character" {
|
||||
return fmt.Errorf("expected Notes Main character, got %s", s.Notes)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
trans := NewActorTranslator()
|
||||
result, err := trans.FromAPI(tt.apiActor)
|
||||
|
||||
if tt.expectErr {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error but got nil")
|
||||
}
|
||||
if tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
|
||||
t.Errorf("expected error containing '%s' but got '%s'", tt.errMsg, err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
t.Fatalf("expected non-nil result")
|
||||
}
|
||||
|
||||
if result.ID != tt.expectID {
|
||||
t.Errorf("expected ID %d but got %d", tt.expectID, result.ID)
|
||||
}
|
||||
|
||||
if tt.checkFunc != nil {
|
||||
if err := tt.checkFunc(result); err != nil {
|
||||
t.Errorf("check failed: %v", err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user