129 lines
2.9 KiB
Go
129 lines
2.9 KiB
Go
package testsuite
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/filter"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type documentStoreQueryTestCase struct {
|
|
Name string
|
|
Before func(ctx context.Context, store storage.DocumentStore) error
|
|
Collection string
|
|
Filter *filter.Filter
|
|
QueryOptionsFuncs []storage.QueryOptionFunc
|
|
After func(t *testing.T, results []storage.Document, err error)
|
|
}
|
|
|
|
var documentStoreQueryTestCases = []documentStoreQueryTestCase{
|
|
{
|
|
Name: "Simple select",
|
|
Before: func(ctx context.Context, store storage.DocumentStore) error {
|
|
doc1 := storage.Document{
|
|
"attr1": "Foo",
|
|
}
|
|
|
|
if _, err := store.Upsert(ctx, "simple_select", doc1); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
doc2 := storage.Document{
|
|
"attr1": "Bar",
|
|
}
|
|
|
|
if _, err := store.Upsert(ctx, "simple_select", doc2); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Collection: "simple_select",
|
|
Filter: filter.New(
|
|
filter.NewEqOperator(map[string]interface{}{
|
|
"attr1": "Foo",
|
|
}),
|
|
),
|
|
After: func(t *testing.T, results []storage.Document, err error) {
|
|
if err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
if e, g := 1, len(results); e != g {
|
|
t.Errorf("len(results): expected '%v', got '%v'", e, g)
|
|
}
|
|
|
|
if e, g := "Foo", results[0]["attr1"]; e != g {
|
|
t.Errorf("results[0][\"Attr1\"]: expected '%v', got '%v'", e, g)
|
|
}
|
|
},
|
|
},
|
|
{
|
|
Name: "IN Operator",
|
|
Before: func(ctx context.Context, store storage.DocumentStore) error {
|
|
docs := []storage.Document{
|
|
{
|
|
"counter": 1,
|
|
"tags": []string{"foo", "bar"},
|
|
},
|
|
{
|
|
"counter": 1,
|
|
"tags": []string{"nope"},
|
|
},
|
|
}
|
|
|
|
for _, doc := range docs {
|
|
if _, err := store.Upsert(ctx, "in_operator", doc); err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
},
|
|
Collection: "in_operator",
|
|
Filter: filter.New(
|
|
filter.NewAndOperator(
|
|
filter.NewEqOperator(map[string]any{
|
|
"counter": 1,
|
|
}),
|
|
filter.NewInOperator(map[string]any{
|
|
"tags": "foo",
|
|
}),
|
|
),
|
|
),
|
|
After: func(t *testing.T, results []storage.Document, err error) {
|
|
if err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
|
|
if e, g := 1, len(results); e != g {
|
|
t.Errorf("len(results): expected '%v', got '%v'", e, g)
|
|
}
|
|
},
|
|
},
|
|
}
|
|
|
|
func testDocumentStoreQuery(t *testing.T, store storage.DocumentStore) {
|
|
for _, tc := range documentStoreQueryTestCases {
|
|
func(tc documentStoreQueryTestCase) {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
// t.Parallel()
|
|
|
|
ctx := context.Background()
|
|
|
|
if tc.Before != nil {
|
|
if err := tc.Before(ctx, store); err != nil {
|
|
t.Fatalf("%+v", errors.WithStack(err))
|
|
}
|
|
}
|
|
|
|
documents, err := store.Query(ctx, tc.Collection, tc.Filter, tc.QueryOptionsFuncs...)
|
|
|
|
tc.After(t, documents, err)
|
|
})
|
|
}(tc)
|
|
}
|
|
}
|