Fix order sensitive and nondeterministic unit tests
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user