Fix order sensitive and nondeterministic unit tests

This commit is contained in:
breel
2020-07-25 13:26:29 -06:00
parent d3c1cb8b4f
commit 34075cf19a
5 changed files with 199 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"local/dndex/storage/entity"
"local/dndex/storage/operator"
"os"
"strings"
"testing"
"time"
@@ -37,8 +38,17 @@ func TestIntegration(t *testing.T) {
randomOne(),
}
ones[0].Connections = map[string]entity.One{ones[2].Name: entity.One{Name: ones[2].Name, Relationship: ":("}}
cleanFill := func() {
clean()
for i := range ones {
if err := graph.driver.Insert(context.TODO(), "col", ones[i]); err != nil {
t.Fatal(err)
}
}
}
t.Run("graph.Insert(...)", func(t *testing.T) {
clean()
for _, one := range ones {
err := graph.Insert(ctx, "col", one)
if err != nil {
@@ -48,6 +58,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.List", func(t *testing.T) {
cleanFill()
all, err := graph.List(ctx, "col")
if err != nil {
t.Fatal(err)
@@ -58,7 +69,20 @@ func TestIntegration(t *testing.T) {
}
})
t.Run("graph.ListCaseInsensitive", func(t *testing.T) {
cleanFill()
all, err := graph.ListCaseInsensitive(ctx, "col")
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) {
cleanFill()
some, err := graph.List(ctx, "col", ones[0].Peers()...)
if err != nil {
t.Fatal(err)
@@ -69,7 +93,40 @@ func TestIntegration(t *testing.T) {
}
})
t.Run("graph.List(FOO => *)", func(t *testing.T) {
cleanFill()
peers := ones[0].Peers()
for i := range peers {
peers[i] = strings.ToUpper(peers[i])
}
some, err := graph.List(ctx, "col", peers...)
if err != nil {
t.Fatal(err)
}
t.Logf("\nsom = %+v", some)
if len(some) != 0 {
t.Fatalf("%+v: %+v", len(some), some)
}
})
t.Run("graph.ListCaseInsensitive(FOO => *)", func(t *testing.T) {
cleanFill()
peers := ones[0].Peers()
for i := range peers {
peers[i] = strings.ToUpper(peers[i])
}
some, err := graph.ListCaseInsensitive(ctx, "col", 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.Search(foo => *)", func(t *testing.T) {
cleanFill()
some, err := graph.Search(ctx, "col", ones[0].Name[:3])
if err != nil {
t.Fatal(err)
@@ -81,6 +138,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.Update(foo, --bar)", func(t *testing.T) {
cleanFill()
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]interface{}{}})
if err != nil {
t.Fatal(err)
@@ -104,6 +162,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.Update(foo, +=2); graph.Update(foo, -=1)", func(t *testing.T) {
cleanFill()
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{entity.Connections, map[string]entity.One{
"hello": entity.One{Name: "hello", Relationship: ":("},
"world": entity.One{Name: "world", Relationship: ":("},
@@ -142,6 +201,7 @@ func TestIntegration(t *testing.T) {
})
t.Run("graph.Update(new attachment), Update(--new attachment)", func(t *testing.T) {
cleanFill()
err := graph.Update(ctx, "col", ones[0].Query(), operator.Set{Key: fmt.Sprintf("%s.new attachment", entity.Attachments), Value: "my new attachment"})
if err != nil {
t.Fatal(err)
@@ -180,11 +240,67 @@ func TestIntegration(t *testing.T) {
t.Fatal(len(some2[0].Attachments), some2[0].Attachments)
}
})
t.Run("graph.Insert Collision(...)", func(t *testing.T) {
cleanFill()
one := randomOne()
err := graph.Insert(ctx, "col", one)
if err != nil {
t.Fatal(err)
}
one.Name = strings.ToUpper(one.Name)
err = graph.Insert(ctx, "col", one)
if err == nil {
t.Fatal(err)
}
err = graph.Delete(ctx, "col", operator.Regex{Key: entity.Name, Value: "^(?i)" + one.Name})
if err != nil {
t.Fatal(err)
}
ones, err = graph.ListCaseInsensitive(ctx, "col", one.Name)
if err != nil {
t.Fatal(err)
}
if len(ones) > 0 {
t.Fatal(err)
}
})
t.Run("graph.Insert Collision(...)", func(t *testing.T) {
cleanFill()
one := randomOne()
err := graph.Insert(ctx, "col", one)
if err != nil {
t.Fatal(err)
}
err = graph.Insert(ctx, "col", one)
if err == nil {
t.Fatal(err)
}
err = graph.Delete(ctx, "col", operator.Regex{Key: entity.Name, Value: "^(?i)" + one.Name})
if err != nil {
t.Fatal(err)
}
ones, err = graph.ListCaseInsensitive(ctx, "col", one.Name)
if err != nil {
t.Fatal(err)
}
if len(ones) > 0 {
t.Fatal(err)
}
})
}
func randomOne() entity.One {
return entity.One{
Name: uuid.New().String()[:5],
Name: "name-" + uuid.New().String()[:5],
Type: "Humman",
Title: "Biggus",
Text: "tee hee xd",