dndex/storage/graph_test.go

144 lines
3.1 KiB
Go

package storage
import (
"context"
"fmt"
"local/dndex/storage/entity"
"local/dndex/storage/operator"
"os"
"testing"
"time"
"github.com/google/uuid"
)
func TestIntegration(t *testing.T) {
if len(os.Getenv("INTEGRATION")) == 0 {
t.Logf("skipping because $INTEGRATION unset")
return
}
os.Args = os.Args[:1]
graph := NewGraph()
graph.mongo.db = "test-db"
graph.mongo.col = "test-col"
ctx, can := context.WithCancel(context.TODO())
defer can()
clean := func() {
graph.mongo.client.Database(graph.mongo.db).Collection(graph.mongo.col).DeleteMany(ctx, map[string]interface{}{})
}
clean()
defer clean()
ones := []entity.One{
randomOne(),
randomOne(),
randomOne(),
}
ones[0].Connections = map[string]entity.One{ones[2].Name: entity.One{Name: ones[2].Name, Relationship: ":("}}
t.Run("graph.Insert(...)", func(t *testing.T) {
for _, one := range ones {
err := graph.Insert(ctx, one)
if err != nil {
t.Fatal(err)
}
}
})
t.Run("graph.List", func(t *testing.T) {
all, err := graph.List(ctx)
if err != nil {
t.Fatal(err)
}
t.Logf("\nall = %+v", all)
if len(all) != 3 {
t.Fatalf("%v: %+v", len(all), all)
}
})
t.Run("graph.List(foo => *)", func(t *testing.T) {
some, err := graph.List(ctx, ones[0].Peers()...)
if err != nil {
t.Fatal(err)
}
t.Logf("\nsom = %+v", some)
if len(some) != 1 {
t.Fatalf("%+v: %+v", len(some), some)
}
})
t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
if err != nil {
t.Fatal(err)
}
some, err := graph.List(ctx, ones[0].Name)
if err != nil {
t.Fatal(err)
}
t.Logf("\nsm' = %+v", some)
if len(some) != 1 {
t.Fatal(len(some))
}
if some[0].Name != ones[0].Name {
t.Fatal(some[0].Name)
}
if l := len(some[0].Peers()); l > 0 {
t.Fatalf("%d: %+v", l, some[0])
}
})
t.Run("graph.Update(foo, +=2); graph.Update(foo, -=1)", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, map[string]entity.One{
"hello": entity.One{Name: "hello", Relationship: ":("},
"world": entity.One{Name: "world", Relationship: ":("},
}})
if err != nil {
t.Fatal(err)
}
some1, err := graph.List(ctx, ones[0].Name)
if err != nil {
t.Fatal(err)
}
t.Logf("sm1 = %+v", some1[0])
if len(some1) != 1 {
t.Fatal(len(some1))
}
if len(some1[0].Peers()) != 2 {
t.Fatal(some1[0].Peers())
}
err = graph.Update(ctx, ones[0].Query(), operator.Unset(fmt.Sprintf("%s.world", entity.Connections)))
if err != nil {
t.Fatal(err)
}
some2, err := graph.List(ctx, ones[0].Name)
if err != nil {
t.Fatal(err)
}
t.Logf("sm2 = %+v", some2[0])
if len(some2) != 1 {
t.Fatal(len(some2))
}
if len(some2[0].Peers()) != 1 {
t.Fatal(some2[0].Peers())
}
})
}
func randomOne() entity.One {
return entity.One{
Name: uuid.New().String()[:5],
Type: "Humman",
Title: "Biggus",
Image: "/path/to.jpg",
Text: "tee hee xd",
Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{},
}
}