95 lines
3.0 KiB
Go
95 lines
3.0 KiB
Go
package tree
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestTypeAndTagLifecycleAndClassification(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := newTestStore(t)
|
|
node := createTestNode(t, store, UnsortedID, "Box")
|
|
typeValue, err := store.CreateNodeType(ctx, "Storage bin", nil, nil, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tag, err := store.CreateTag(ctx, "Fragile", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
classified, err := store.SetClassification(ctx, node.ID, &typeValue.ID, []string{tag.ID})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if classified.TypeID == nil || *classified.TypeID != typeValue.ID || len(classified.TagIDs) != 1 || classified.TagIDs[0] != tag.ID {
|
|
t.Fatalf("classification missing: %#v", classified)
|
|
}
|
|
treeValue, err := store.Tree(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fromTree := treeValue.findForTest(node.ID)
|
|
if fromTree == nil || fromTree.TypeID == nil || len(fromTree.TagIDs) != 1 {
|
|
t.Fatalf("tree classification missing: %#v", fromTree)
|
|
}
|
|
if err := store.DeleteNodeType(ctx, typeValue.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.DeleteTag(ctx, tag.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after := mustGet(t, store, node.ID)
|
|
if after.TypeID != nil || len(after.TagIDs) != 0 {
|
|
t.Fatalf("deleting metadata damaged assignments: %#v", after)
|
|
}
|
|
}
|
|
|
|
func TestBulkClassificationIsAtomic(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := newTestStore(t)
|
|
first := createTestNode(t, store, UnsortedID, "First")
|
|
second := createTestNode(t, store, UnsortedID, "Second")
|
|
typeValue, _ := store.CreateNodeType(ctx, "Box", nil, nil, nil)
|
|
tag, _ := store.CreateTag(ctx, "Blue", nil)
|
|
if err := store.ClassifyMany(ctx, []string{first.ID, second.ID}, true, &typeValue.ID, []string{tag.ID}, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{first.ID, second.ID} {
|
|
node := mustGet(t, store, id)
|
|
if node.TypeID == nil || len(node.TagIDs) != 1 {
|
|
t.Fatalf("bulk assignment missing: %#v", node)
|
|
}
|
|
}
|
|
if err := store.ClassifyMany(ctx, []string{first.ID, "missing"}, false, nil, nil, []string{tag.ID}); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("expected not found, got %v", err)
|
|
}
|
|
if len(mustGet(t, store, first.ID).TagIDs) != 1 {
|
|
t.Fatal("failed bulk request partially changed nodes")
|
|
}
|
|
}
|
|
|
|
func TestIndividualizeInheritsTypeAndTags(t *testing.T) {
|
|
ctx := context.Background()
|
|
store := newTestStore(t)
|
|
quantity := int64(2)
|
|
node, err := store.Create(ctx, nil, "Bin", nil, &quantity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
typeValue, _ := store.CreateNodeType(ctx, "Bin", nil, nil, nil)
|
|
tag, _ := store.CreateTag(ctx, "Blue", nil)
|
|
if _, err := store.SetClassification(ctx, node.ID, &typeValue.ID, []string{tag.ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
units, err := store.Individualize(ctx, node.ID, []string{"Bin 01", "Bin 02"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, unit := range units {
|
|
if unit.TypeID == nil || *unit.TypeID != typeValue.ID || len(unit.TagIDs) != 1 || unit.TagIDs[0] != tag.ID {
|
|
t.Fatalf("metadata not inherited: %#v", unit)
|
|
}
|
|
}
|
|
}
|