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

@@ -2,6 +2,7 @@ package storage
import (
"context"
"errors"
"fmt"
"local/dndex/config"
"local/dndex/storage/driver"
@@ -34,6 +35,16 @@ func (g Graph) Search(ctx context.Context, namespace string, nameContains string
return g.find(ctx, namespace, filter)
}
func (g Graph) ListCaseInsensitive(ctx context.Context, namespace string, from ...string) ([]entity.One, error) {
if len(from) == 0 {
return g.List(ctx, namespace)
}
pattern := strings.Join(from, "|")
pattern = "^(?i)(" + pattern + ")"
filter := operator.Regex{Key: entity.Name, Value: pattern}
return g.find(ctx, namespace, filter)
}
func (g Graph) List(ctx context.Context, namespace string, from ...string) ([]entity.One, error) {
filter := operator.NewFilterIn(entity.Name, from)
return g.find(ctx, namespace, filter)
@@ -60,6 +71,11 @@ func (g Graph) gatherOnes(ctx context.Context, ch <-chan bson.Raw) ([]entity.One
}
func (g Graph) Insert(ctx context.Context, namespace string, one entity.One) error {
if ones, err := g.ListCaseInsensitive(ctx, namespace, one.Name); err != nil {
return err
} else if len(ones) > 0 {
return errors.New("collision on primary key when case insensitive")
}
return g.driver.Insert(ctx, namespace, one)
}