package storage import ( "context" "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 = []entity.One{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, []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, ++...); graph.Update(foo, --if :()", func(t *testing.T) { err := graph.Update(ctx, ones[0].Query(), operator.Set{entity.Connections, []entity.One{entity.One{Name: "hello", 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()) != 1 { t.Fatal(some1[0].Peers()) } err = graph.Update(ctx, ones[0].Query(), operator.PopIf{entity.Connections, map[string]string{entity.Relationship: ":("}}) 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(some1) != len(some2) { t.Fatal(len(some2)) } if len(some1[0].Connections) == len(some2[0].Connections) { t.Fatal(len(some2[0].Connections)) } if len(some2[0].Connections) == len(ones) { t.Fatal(len(some2[0].Connections)) } }) } 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: []entity.One{}, } }