dndex/storage/graph_test.go

129 lines
2.6 KiB
Go

package storage
import (
"context"
"os"
"testing"
)
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 := []One{
One{ID: "A", Relation: ":)"},
One{ID: "B", Relation: ":("},
One{ID: "C", Relation: ":/"},
}
ones[0].Know = []One{ones[len(ones)-1]}
ones[0].Know[0].Relation = ":D"
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].Knows()...)
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].Min(), Set{"know", []interface{}{}})
if err != nil {
t.Fatal(err)
}
some, err := graph.List(ctx, ones[0].ID)
if err != nil {
t.Fatal(err)
}
t.Logf("\nsm' = %+v", some)
if len(some) != 1 {
t.Fatal(len(some))
}
if some[0].ID != ones[0].ID {
t.Fatal(some[0].ID)
}
if len(some[0].Knows()) > 0 {
t.Fatal(some[0].Knows())
}
})
t.Run("graph.Update(foo, ++...); graph.Update(foo, --if :()", func(t *testing.T) {
err := graph.Update(ctx, ones[0].Min(), Set{"know", ones})
if err != nil {
t.Fatal(err)
}
some1, err := graph.List(ctx, ones[0].ID)
if err != nil {
t.Fatal(err)
}
t.Logf("sm1 = %+v", some1[0])
if len(some1) != 1 {
t.Fatal(len(some1))
}
if len(some1[0].Knows()) != len(ones) {
t.Fatal(some1[0].Knows())
}
err = graph.Update(ctx, ones[0].Min(), PopIf{"know", map[string]string{"relation": ":("}})
if err != nil {
t.Fatal(err)
}
some2, err := graph.List(ctx, ones[0].ID)
if err != nil {
t.Fatal(err)
}
t.Logf("sm2 = %+v", some2[0])
if len(some1) != len(some2) {
t.Fatal(len(some2))
}
if len(some1[0].Knows()) == len(some2[0].Knows()) {
t.Fatal(len(some2[0].Knows()))
}
if len(some2[0].Knows()) == len(ones) {
t.Fatal(len(some2[0].Knows()))
}
})
}