forked from longtao.wu/droboticflow
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/store"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/event"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAcceptancePriorityConfigMongo(t *testing.T) {
|
|
if os.Getenv("ACCEPTANCE_PRIORITY_LOCAL_MONGO") != "1" {
|
|
t.Skip("isolated local Mongo opt-in")
|
|
}
|
|
f := priorityFixture(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
|
defer cancel()
|
|
var scans atomic.Int32
|
|
monitor := &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
|
if e.CommandName == "find" || e.CommandName == "aggregate" {
|
|
col, _ := e.Command.Lookup(e.CommandName).StringValueOK()
|
|
if col == "humanItems" || col == "humanBatches" || col == "annotationTasks" {
|
|
scans.Add(1)
|
|
}
|
|
}
|
|
}}
|
|
client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018").SetMonitor(monitor))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("acceptance_priority_config_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
s := store.NewMongoStore(db)
|
|
supplier, _ := f.store.GetSupplier(ctx, "priority-production")
|
|
if e = s.UpsertSupplier(ctx, *supplier); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.UpsertUser(ctx, f.admin); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
docs := make([]any, 5600)
|
|
for i := range docs {
|
|
docs[i] = bson.M{"id": fmt.Sprintf("candidate-%06d", i), "organizationId": f.item.OrganizationID, "currentStageId": "prepackage_acceptance", "status": "ready", "pluginId": "annotation-qa", "subject": bson.M{"payload": bson.M{"synthetic": "fixture"}}}
|
|
}
|
|
if _, e = db.Collection("humanItems").InsertMany(ctx, docs); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f.handler = NewHandler(s, nil)
|
|
for i, op := range []struct {
|
|
method string
|
|
body any
|
|
}{{"GET", nil}, {"PUT", map[string]any{"enabled": true, "version": 0, "expiresAt": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}}, {"PUT", map[string]any{"enabled": false, "version": 1}}} {
|
|
at := time.Now()
|
|
r := f.request(op.method, "/api/suppliers/priority-production/acceptance-priority", f.admin, op.body)
|
|
elapsed := time.Since(at)
|
|
t.Logf("operation%d %s: %s", i, op.method, elapsed)
|
|
if r.Code != 200 || elapsed >= time.Second {
|
|
t.Fatalf("config %d %s", r.Code, r.Body)
|
|
}
|
|
}
|
|
if scans.Load() != 0 {
|
|
t.Fatal("metadata used candidate/source queries")
|
|
}
|
|
rows, e := store.NewMongoStore(db).ListAcceptancePriorities(ctx, f.item.OrganizationID)
|
|
if e != nil || len(rows) != 1 || rows[0].Version != 2 || rows[0].Enabled || len(rows[0].History) != 2 {
|
|
t.Fatal("confirmed writes did not survive new store")
|
|
}
|
|
}
|