forked from longtao.wu/droboticflow
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHumanPoolReadScopeMongoParity(t *testing.T) {
|
|
if os.Getenv("TEST_WORKFORCE_MONGO_URI") == "" {
|
|
t.Skip("local Mongo opt-in")
|
|
}
|
|
if os.Getenv("TEST_WORKFORCE_MONGO_URI") != "mongodb://127.0.0.1:27018" {
|
|
t.Fatal("loopback only")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cl, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cl.Disconnect(context.Background())
|
|
db := cl.Database(fmt.Sprintf("qc_scope_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
ms := NewMemoryStore()
|
|
ds := NewMongoStore(db)
|
|
for i, supplier := range []string{"vendor", "", "other"} {
|
|
for j, project := range []string{"p", "other", ""} {
|
|
it := domain.HumanItemRecord{ID: fmt.Sprintf("%d-%d", i, j), OrganizationID: "o", SupplierID: supplier, ProjectID: project, CreatedAt: "2026-10-01T00:00:00Z"}
|
|
if err := ms.UpsertHumanItem(ctx, it); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ds.UpsertHumanItem(ctx, it); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
// Missing legacy supplier fields must behave exactly like empty strings.
|
|
if _, err := db.Collection("humanItems").UpdateMany(ctx, bson.M{"supplierId": ""}, bson.M{"$unset": bson.M{"supplierId": ""}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
supplier := "vendor"
|
|
for _, q := range []domain.HumanItemQuery{{}, {AvailableProjectIDs: []string{}}, {AvailableProjectIDs: []string{"p"}, AvailableSupplierID: &supplier}, {AvailableProjectIDs: []string{"p"}, AvailableSupplierID: &supplier, ProjectID: "other"}, {AvailableProjectIDs: []string{""}, AvailableSupplierID: &supplier}} {
|
|
a, e := ms.ListHumanItems(ctx, "o", q)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
b, e := ds.ListHumanItems(ctx, "o", q)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
ids := func(items []domain.HumanItemRecord) []string {
|
|
out := []string{}
|
|
for _, it := range items {
|
|
out = append(out, it.ID)
|
|
}
|
|
return out
|
|
}
|
|
if !reflect.DeepEqual(ids(a), ids(b)) {
|
|
t.Fatalf("scope mismatch %v %v", ids(a), ids(b))
|
|
}
|
|
}
|
|
}
|