-
Notifications
You must be signed in to change notification settings - Fork 171
/
stores.go
91 lines (72 loc) · 2.52 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
package test
import (
"context"
"testing"
"time"
"github.com/oklog/ulid/v2"
openfgav1 "github.com/openfga/api/proto/openfga/v1"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/openfga/openfga/pkg/storage"
"github.com/openfga/openfga/pkg/testutils"
)
func StoreTest(t *testing.T, datastore storage.OpenFGADatastore) {
ctx := context.Background()
// Create some stores.
numStores := 10
var stores []*openfgav1.Store
for i := 0; i < numStores; i++ {
store := &openfgav1.Store{
Id: ulid.Make().String(),
Name: testutils.CreateRandomString(10),
CreatedAt: timestamppb.New(time.Now()),
}
_, err := datastore.CreateStore(ctx, store)
require.NoError(t, err)
stores = append(stores, store)
}
t.Run("inserting_store_in_twice_fails", func(t *testing.T) {
_, err := datastore.CreateStore(ctx, stores[0])
require.ErrorIs(t, err, storage.ErrCollision)
})
t.Run("list_stores_succeeds", func(t *testing.T) {
gotStores, ct, err := datastore.ListStores(ctx, storage.PaginationOptions{PageSize: 1})
require.NoError(t, err)
require.Len(t, gotStores, 1)
require.NotEmpty(t, len(ct))
_, ct, err = datastore.ListStores(ctx, storage.PaginationOptions{PageSize: 100, From: string(ct)})
require.NoError(t, err)
// This will fail if there are actually over 101 stores in the DB at the time of running.
require.Zero(t, len(ct))
})
t.Run("get_store_succeeds", func(t *testing.T) {
store := stores[0]
gotStore, err := datastore.GetStore(ctx, store.Id)
require.NoError(t, err)
require.Equal(t, store.Id, gotStore.Id)
require.Equal(t, store.Name, gotStore.Name)
})
t.Run("get_non-existent_store_returns_not_found", func(t *testing.T) {
_, err := datastore.GetStore(ctx, "foo")
require.ErrorIs(t, err, storage.ErrNotFound)
})
t.Run("delete_store_succeeds", func(t *testing.T) {
store := stores[1]
err := datastore.DeleteStore(ctx, store.Id)
require.NoError(t, err)
// Should not be able to get the store now.
_, err = datastore.GetStore(ctx, store.Id)
require.ErrorIs(t, 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)
require.NoError(t, err)
// Store id should not appear in the list of store ids.
gotStores, _, err := datastore.ListStores(ctx, storage.PaginationOptions{PageSize: storage.DefaultPageSize})
require.NoError(t, err)
for _, s := range gotStores {
require.NotEqual(t, store.Id, s.Id)
}
})
}