Files
releases/v1/storage/storage_test.go
T
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00

74 lines
1.3 KiB
Go

package storage_test
import (
"bytes"
"context"
"strings"
"testing"
"github.com/open-policy-agent/opa/v1/storage"
"github.com/open-policy-agent/opa/v1/storage/inmem"
)
func TestNonEmpty(t *testing.T) {
cases := []struct {
content string
path string
exp bool
}{
{
content: `{}`,
path: "a/b/c",
exp: false,
},
{
content: `{"a": {}}`,
path: "a/b/c",
exp: false,
},
{
content: `{"a": {"b": {}}}`,
path: "a/b/c",
exp: false,
},
{
content: `{"a": {"b": {"c": {}}}}`,
path: "a/b/c",
exp: true,
},
{
content: `{"a": {"b": "x"}}`,
path: "a/b/c",
exp: true,
},
{
content: `{"a": "x"}`,
path: "a/b/c",
exp: true,
},
}
ctx := context.Background()
for _, tc := range cases {
t.Run(tc.content, func(t *testing.T) {
store := inmem.NewFromReader(bytes.NewBufferString(tc.content))
err := storage.Txn(ctx, store, storage.TransactionParams{}, func(txn storage.Transaction) error {
nonEmpty, err := storage.NonEmpty(ctx, store, txn)(strings.Split(tc.path, "/"))
if err != nil {
t.Fatal(err)
}
if nonEmpty != tc.exp {
t.Errorf("Expected %v for %v on %v but got", tc.exp, tc.path, tc.content)
}
return nil
})
if err != nil {
t.Error(err)
}
})
}
}