Skip to content

Commit

Permalink
Add test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Nov 5, 2024
1 parent 230db57 commit 3e304ef
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/composableschemadsl/compiler/importer-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## Test Structure
Every folder should have a `root.zed`. This is the target for compilation.

Every folder will have an `expected.zed`, which is the output of the compilation process.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .user.user import user

definition resource {
relation viewer: user
permission view = viewer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition user {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition user {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .definitions.user.user import user

definition resource {
relation viewer: user
permission view = viewer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .user import user
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .indirection import user

definition resource {
relation viewer: user
permission view = viewer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition user {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from .user import user

definition resource {
relation viewer: user
permission view = viewer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
definition user {}
80 changes: 80 additions & 0 deletions pkg/composableschemadsl/compiler/importer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package compiler_test

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/assert"

"github.com/authzed/spicedb/pkg/composableschemadsl/input"
"github.com/authzed/spicedb/pkg/composableschemadsl/compiler"
"github.com/authzed/spicedb/pkg/composableschemadsl/generator"
)

type importerTest struct {
name string
folder string
}

func (it *importerTest) input() string {
b, err := os.ReadFile(fmt.Sprintf("importer-test/%s/root.zed", it.folder))
if err != nil {
panic(err)
}

return string(b)
}

func (it *importerTest) expected() string {
b, err := os.ReadFile(fmt.Sprintf("importer-test/%s/expected.zed", it.folder))
if err != nil {
panic(err)
}

return string(b)
}

func (it *importerTest) writeExpected(schema string) {
err := os.WriteFile(fmt.Sprintf("importer-test/%s/expected.zed", it.folder), []byte(schema), 0o_600)
if err != nil {
panic(err)
}
}

func TestImporter(t *testing.T) {
importerTests := []importerTest{
{"simple local import", "simple-local"},
{"simple local import", "simple-local-with-hop"},
{"nested local import", "nested-local"},
{"nested local two layers deep import", "nested-two-layer-local"},
}

for _, test := range importerTests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

inputSchema := test.input()

compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source("schema"),
SchemaString: inputSchema,
}, compiler.AllowUnprefixedObjectType())
require.NoError(t, err)

generated, _, err := generator.GenerateSchema(compiled.OrderedDefinitions)
require.NoError(t, err)

if os.Getenv("REGEN") == "true" {
test.writeExpected(generated)
} else {
expected := test.expected()
if !assert.Equal(t, expected, generated, test.name) {
t.Log(generated)
}
}
})
}

}

0 comments on commit 3e304ef

Please sign in to comment.