forked from longtao.wu/droboticflow
81 lines
3.5 KiB
Go
81 lines
3.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func testLatestProgressReads(t *testing.T, s Store) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
org := "progress-org"
|
|
for _, task := range []string{"a", "b", "outside"} {
|
|
for rev := 0; rev < 20; rev++ {
|
|
slice := domain.SliceRevisionRecord{ID: fmt.Sprintf("slice-%s-%d", task, rev), OrganizationID: org, TaskID: task, Revision: rev, CreatedAt: fmt.Sprint(rev), ChangeReason: "private history", Segments: []domain.SliceSegment{{ID: "slice", Start: 0, End: 10, Name: "large name", AnnotationStatus: domain.SliceAnnotationLabeled, DeliveryDisposition: "discard"}}, Operations: []domain.SliceOperation{{}}}
|
|
if err := s.UpsertSliceRevision(ctx, slice); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
annotation := domain.AnnotationRevisionRecord{ID: fmt.Sprintf("annotation-%s-%d", task, rev), OrganizationID: org, TaskID: task, Revision: rev, AuthorUserID: "human", Segments: []domain.AnnotationSegment{{ID: "event", Start: 1, End: 5, Description: "large description"}}, ObjectHints: []string{"large hints"}}
|
|
if rev > 17 {
|
|
annotation.AuthorUserID = " system "
|
|
}
|
|
if err := s.UpsertAnnotationRevision(ctx, annotation); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
// Organization isolation and human-only selection happen before latest wins.
|
|
for _, r := range []domain.AnnotationRevisionRecord{
|
|
{ID: "other-org", OrganizationID: "other-org", TaskID: "a", Revision: 99, AuthorUserID: "human"},
|
|
{ID: "system-only", OrganizationID: org, TaskID: "system-only", Revision: 5, AuthorUserID: "\u0085system\u0085"},
|
|
{ID: "nul-author", OrganizationID: org, TaskID: "nul-author", Revision: 1, AuthorUserID: "\x00system"},
|
|
{ID: "suggestion-only", OrganizationID: org, TaskID: "suggestion-only", Revision: 0, AuthorUserID: "human"},
|
|
} {
|
|
if err := s.UpsertAnnotationRevision(ctx, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
ids := []string{"a", "b", "a", "system-only", "suggestion-only", "missing", "nul-author"}
|
|
slices, err := s.ListLatestSliceProgressByTaskIDs(ctx, org, ids)
|
|
if err != nil || len(slices) != 2 {
|
|
t.Fatalf("slices=%v err=%v", slices, err)
|
|
}
|
|
for _, r := range slices {
|
|
if r.Revision != 19 || len(r.Operations) != 0 || r.Segments[0].Name != "" || r.Segments[0].ID != "" || r.Segments[0].End != 10 || r.CreatedAt != "19" || r.Segments[0].DeliveryDisposition != "discard" {
|
|
t.Fatalf("invalid slice projection: %+v", r)
|
|
}
|
|
}
|
|
annotations, err := s.ListLatestAnnotationProgressByTaskIDs(ctx, org, ids)
|
|
if err != nil || len(annotations) != 3 {
|
|
t.Fatalf("annotations=%v err=%v", annotations, err)
|
|
}
|
|
for _, r := range annotations {
|
|
if r.TaskID == "nul-author" {
|
|
if r.AuthorUserID != "\x00system" {
|
|
t.Fatal("NUL author changed")
|
|
}
|
|
continue
|
|
}
|
|
if r.Revision != 17 || r.AuthorUserID != "human" || len(r.ObjectHints) != 0 || !reflect.DeepEqual(r.Segments, []domain.AnnotationSegment{{Start: 1, End: 5}}) {
|
|
t.Fatalf("invalid annotation projection: %+v", r)
|
|
}
|
|
}
|
|
empty, err := s.ListLatestSliceProgressByTaskIDs(ctx, org, nil)
|
|
if err != nil || len(empty) != 0 {
|
|
t.Fatalf("empty query expanded: %v %v", empty, err)
|
|
}
|
|
all, err := s.ListSliceRevisionsByTaskIDs(ctx, org, []string{"a"})
|
|
if err != nil || len(all) != 20 || all[0].Segments[0].Name != "large name" {
|
|
t.Fatalf("history changed: %v", err)
|
|
}
|
|
}
|
|
func TestLatestProgressReadsMemory(t *testing.T) { testLatestProgressReads(t, NewMemoryStore()) }
|
|
func TestLatestProgressReadsMongo(t *testing.T) {
|
|
_, s := poolLocalMongo(t)
|
|
testLatestProgressReads(t, s)
|
|
}
|