Skip to content

Commit

Permalink
Add tests for determining consistency
Browse files Browse the repository at this point in the history
This commit adds tests for determineConsistency.

Signed-off-by: John Schaeffer <[email protected]>
  • Loading branch information
jnschaeffer committed Jan 16, 2024
1 parent 534a7bf commit a65d212
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/query/relations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.infratographer.com/permissions-api/internal/types"
)

func testEngine(ctx context.Context, t *testing.T, namespace string) Engine {
func testEngine(ctx context.Context, t *testing.T, namespace string) *engine {
config := spicedbx.Config{
Endpoint: "spicedb:50051",
Key: "infradev",
Expand Down Expand Up @@ -52,10 +52,12 @@ func testEngine(ctx context.Context, t *testing.T, namespace string) Engine {
cleanDB(ctx, t, client, namespace)
})

// We call the constructor here to ensure the engine is created appropriately, but
// then return the underlying type so we can do testing with it.
out, err := NewEngine(namespace, client, kv, WithPolicy(policy))
require.NoError(t, err)

return out
return out.(*engine)
}

func testPolicy() iapl.Policy {
Expand Down
79 changes: 79 additions & 0 deletions internal/query/zedtokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package query

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.infratographer.com/permissions-api/internal/testingx"
"go.infratographer.com/permissions-api/internal/types"
"go.infratographer.com/x/gidx"
)

func TestConsistency(t *testing.T) {
namespace := "testconsistency"
ctx := context.Background()
e := testEngine(ctx, t, namespace)

tenantID, err := gidx.NewID("tnntten")
require.NoError(t, err)
tenantRes, err := e.NewResourceFromID(tenantID)
require.NoError(t, err)

parentID, err := gidx.NewID("tnntten")
require.NoError(t, err)
parentRes, err := e.NewResourceFromID(parentID)
require.NoError(t, err)

otherID, err := gidx.NewID("tnntten")
require.NoError(t, err)
otherRes, err := e.NewResourceFromID(otherID)
require.NoError(t, err)

testCases := []testingx.TestCase[types.Resource, string]{
{
Name: "WithZedToken",
Input: tenantRes,
SetupFn: func(ctx context.Context, t *testing.T) context.Context {
rels := []types.Relationship{
{
Resource: tenantRes,
Relation: "parent",
Subject: parentRes,
},
}

err := e.CreateRelationships(ctx, rels)

require.NoError(t, err)

return ctx
},
CheckFn: func(ctx context.Context, t *testing.T, res testingx.TestResult[string]) {
assert.NoError(t, res.Err)
assert.Equal(t, consistencyAtLeastAsFresh, res.Success)
},
},
{
Name: "WithoutZedToken",
Input: otherRes,
CheckFn: func(ctx context.Context, t *testing.T, res testingx.TestResult[string]) {
assert.NoError(t, res.Err)
assert.Equal(t, consistencyMinimizeLatency, res.Success)
},
},
}

testFn := func(ctx context.Context, res types.Resource) testingx.TestResult[string] {
_, consistencyName := e.determineConsistency(ctx, res)

out := testingx.TestResult[string]{
Success: consistencyName,
}

return out
}

testingx.RunTests(ctx, t, testCases, testFn)
}

0 comments on commit a65d212

Please sign in to comment.