/
stores.go
119 lines (99 loc) · 3.03 KB
/
stores.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package test
import (
"context"
"errors"
"testing"
"time"
"github.com/oklog/ulid/v2"
"github.com/openfga/openfga/pkg/testutils"
"github.com/openfga/openfga/storage"
openfgapb "go.buf.build/openfga/go/openfga/api/openfga/v1"
"google.golang.org/protobuf/types/known/timestamppb"
)
func StoreTest(t *testing.T, datastore storage.OpenFGADatastore) {
ctx := context.Background()
// Create some stores
numStores := 10
var stores []*openfgapb.Store
for i := 0; i < numStores; i++ {
store := &openfgapb.Store{
Id: ulid.Make().String(),
Name: testutils.CreateRandomString(10),
CreatedAt: timestamppb.New(time.Now()),
}
if _, err := datastore.CreateStore(ctx, store); err != nil {
t.Fatal(err)
}
stores = append(stores, store)
}
t.Run("inserting store in twice fails", func(t *testing.T) {
if _, err := datastore.CreateStore(ctx, stores[0]); !errors.Is(err, storage.ErrCollision) {
t.Fatalf("got '%v', expected '%v'", err, storage.ErrCollision)
}
})
t.Run("list stores succeeds", func(t *testing.T) {
gotStores, ct, err := datastore.ListStores(ctx, storage.PaginationOptions{PageSize: 1})
if err != nil {
t.Fatal(err)
}
if len(gotStores) != 1 {
t.Fatalf("expected one store, got %d", len(gotStores))
}
if len(ct) == 0 {
t.Fatal("expected a continuation token but did not get one")
}
_, ct, err = datastore.ListStores(ctx, storage.PaginationOptions{PageSize: 100, From: string(ct)})
if err != nil {
t.Fatal(err)
}
// This will fail if there are actually over 101 stores in the DB at the time of running
if len(ct) != 0 {
t.Fatalf("did not expect a continuation token but got: %s", string(ct))
}
})
t.Run("get store succeeds", func(t *testing.T) {
store := stores[0]
gotStore, err := datastore.GetStore(ctx, store.Id)
if err != nil {
t.Fatal(err)
}
if gotStore.Id != store.Id || gotStore.Name != store.Name {
t.Errorf("got '%v', expected '%v'", gotStore, store)
}
})
t.Run("get non-existent store returns not found", func(t *testing.T) {
_, err := datastore.GetStore(ctx, "foo")
if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("got '%v', expected '%v'", err, storage.ErrNotFound)
}
})
t.Run("delete store succeeds", func(t *testing.T) {
store := stores[1]
err := datastore.DeleteStore(ctx, store.Id)
if err != nil {
t.Fatal(err)
}
// Should not be able to get the store now
_, err = datastore.GetStore(ctx, store.Id)
if !errors.Is(err, storage.ErrNotFound) {
t.Errorf("got '%v', expected '%v'", err, storage.ErrNotFound)
}
})
t.Run("deleted store does not appear in list", func(t *testing.T) {
store := stores[2]
err := datastore.DeleteStore(ctx, store.Id)
if err != nil {
t.Fatal(err)
}
// Store id should not appear in the list of store ids
gotStores, _, err := datastore.ListStores(ctx, storage.PaginationOptions{PageSize: storage.DefaultPageSize})
if err != nil {
t.Fatal(err)
}
for _, s := range gotStores {
if s.Id == store.Id {
t.Errorf("deleted store '%s' appears in ListStores", s)
}
}
})
}