Skip to content

Commit

Permalink
Add test for invalid namespace delete to increase datastore coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Dec 24, 2024
1 parent eef0786 commit ba30bf1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/datastore/test/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func AllWithExceptions(t *testing.T, tester DatastoreTester, except Categories,
t.Run("TestNamespaceMultiDelete", runner(tester, NamespaceMultiDeleteTest))
t.Run("TestEmptyNamespaceDelete", runner(tester, EmptyNamespaceDeleteTest))
t.Run("TestStableNamespaceReadWrite", runner(tester, StableNamespaceReadWriteTest))
t.Run("TestNamespaceDeleteInvalidNamespace", runner(tester, NamespaceDeleteInvalidNamespaceTest))

t.Run("TestSimple", runner(tester, SimpleTest))
t.Run("TestObjectIDs", runner(tester, ObjectIDsTest))
Expand Down
41 changes: 41 additions & 0 deletions pkg/datastore/test/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,47 @@ func EmptyNamespaceDeleteTest(t *testing.T, tester DatastoreTester) {
require.True(errors.As(err, &datastore.NamespaceNotFoundError{}))
}

// NamespaceDeleteInvalidNamespaceTest tests deleting an invalid namespace in the datastore.
func NamespaceDeleteInvalidNamespaceTest(t *testing.T, tester DatastoreTester) {
require := require.New(t)

schemaString := `definition user {}
definition document {
relation viewer: user
}`

// Compile namespace to write to the datastore.
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: schemaString,
}, compiler.AllowUnprefixedObjectType())
require.NoError(err)
require.Equal(2, len(compiled.OrderedDefinitions))

// Write the namespace definition to the datastore.
ds, err := tester.New(0, veryLargeGCInterval, veryLargeGCWindow, 1)
require.NoError(err)

ctx := context.Background()
_, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
err := rwt.WriteCaveats(ctx, compiled.CaveatDefinitions)
if err != nil {
return err
}

return rwt.WriteNamespaces(ctx, compiled.ObjectDefinitions...)
})
require.NoError(err)

// Attempt to delete the invalid namespace.
_, err = ds.ReadWriteTx(ctx, func(ctx context.Context, rwt datastore.ReadWriteTransaction) error {
return rwt.DeleteNamespaces(ctx, "invalid")
})
require.Error(err)
require.ErrorContains(err, "not found")
}

// StableNamespaceReadWriteTest tests writing a namespace to the datastore and reading it back,
// ensuring that it does not change in any way and that the deserialized data matches that stored.
func StableNamespaceReadWriteTest(t *testing.T, tester DatastoreTester) {
Expand Down

0 comments on commit ba30bf1

Please sign in to comment.