-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
230db57
commit 3e304ef
Showing
11 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-local/root.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/nested-local/user/user.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
definition user {} |
1 change: 1 addition & 0 deletions
1
...mposableschemadsl/compiler/importer-test/nested-two-layer-local/definitions/user/user.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
definition user {} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/nested-two-layer-local/root.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/indirection.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .user import user |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/root.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local-with-hop/user.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
definition user {} |
6 changes: 6 additions & 0 deletions
6
pkg/composableschemadsl/compiler/importer-test/simple-local/root.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/composableschemadsl/compiler/importer-test/simple-local/user.zed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
definition user {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
} |