forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
73 lines (66 loc) · 1.3 KB
/
storage_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package storage_test
import (
"bytes"
"context"
"strings"
"testing"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/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)
}
})
}
}