Skip to content

Commit

Permalink
Use new test helper in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tstirrat15 committed Oct 30, 2024
1 parent 0655828 commit 4524bc9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 47 deletions.
9 changes: 4 additions & 5 deletions internal/cmd/backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestFilterSchemaDefs(t *testing.T) {
}
}

func TestBackupParseRelsCmdFunc(t *testing.T) {
func TestBackupMustParseV1RelsCmdFunc(t *testing.T) {
for _, tt := range []struct {
name string
filter string
Expand Down Expand Up @@ -300,13 +300,12 @@ func TestBackupCreateCmdFunc(t *testing.T) {
_, err = c.WriteSchema(ctx, &v1.WriteSchemaRequest{Schema: testSchema})
require.NoError(t, err)

testRelString := "test/resource:1#reader@test/user:1"
testRel, _ := tuple.ParseV1Rel(testRelString)
testRel := "test/resource:1#reader@test/user:1"
resp, err := c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: testRel,
Relationship: tuple.MustParseV1Rel(testRel),
},
},
})
Expand All @@ -325,7 +324,7 @@ func TestBackupCreateCmdFunc(t *testing.T) {
require.Equal(t, testSchema, d.Schema())
rel, err := d.Next()
require.NoError(t, err)
require.Equal(t, testRelString, tuple.MustV1StringRelationship(rel))
require.Equal(t, testRel, tuple.MustV1StringRelationship(rel))
require.Equal(t, resp.WrittenAt.Token, d.ZedToken().Token)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/cmd/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func createTestBackup(t *testing.T, schema string, relationships []string) strin
}()

for _, rel := range relationships {
r, err := tuple.ParseV1Rel(rel)
require.NoError(t, err)
r := tuple.MustParseV1Rel(rel)
require.NoError(t, avroWriter.Append(r))
}

Expand Down
9 changes: 2 additions & 7 deletions internal/cmd/restorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ func TestRestorer(t *testing.T) {

expectedFilteredRels := make([]string, 0, len(tt.relationships))
for _, rel := range tt.relationships {
v1Relation, err := tuple.ParseV1Rel(rel)
require.NoError(err)
if !hasRelPrefix(v1Relation, tt.prefixFilter) {
if !hasRelPrefix(tuple.MustParseV1Rel(rel), tt.prefixFilter) {
continue
}

Expand Down Expand Up @@ -228,10 +226,7 @@ func (m *mockClient) Send(req *v1.BulkImportRelationshipsRequest) error {
for i, rel := range req.Relationships {
// This is a gosec115 false positive which should be fixed in a future version.
uinti, _ := safecast.ToUint(i)
relationString := m.expectedRels[((m.receivedBatches-1)*m.requestedBatchSize)+uinti]
parsedRelation, err := tuple.ParseV1Rel(relationString)
require.NoError(m.t, err)
require.True(m.t, proto.Equal(rel, parsedRelation))
require.True(m.t, proto.Equal(rel, tuple.MustParseV1Rel(m.expectedRels[((m.receivedBatches-1)*m.requestedBatchSize)+uinti])))
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions internal/commands/permission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ func TestLookupResourcesCommand(t *testing.T) {

var updates []*v1.RelationshipUpdate
for i := 0; i < 10; i++ {
relationship, _ := tuple.ParseV1Rel(fmt.Sprintf("test/resource:%d#reader@test/user:1", i))
updates = append(updates, &v1.RelationshipUpdate{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: relationship,
Relationship: tuple.MustParseV1Rel(fmt.Sprintf("test/resource:%d#reader@test/user:1", i)),
})
}

Expand Down
47 changes: 16 additions & 31 deletions internal/commands/relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func TestRelationshipToString(t *testing.T) {
} {
tt := tt
t.Run(tt.rawRel, func(t *testing.T) {
rel, err := tuple.ParseV1Rel(tt.rawRel)
require.NoError(t, err)
rel := tuple.MustParseV1Rel(tt.rawRel)
out, err := relationshipToString(rel)
require.NoError(t, err)
require.Equal(t, tt.expected, out)
Expand Down Expand Up @@ -154,7 +153,7 @@ func TestArgsToRelationship(t *testing.T) {
}
}

func TestParseRelationshipLine(t *testing.T) {
func TestMustParseV1RelationshipLine(t *testing.T) {
for _, tt := range []struct {
input string
expected []string
Expand Down Expand Up @@ -241,12 +240,11 @@ func TestWriteRelationshipsArgs(t *testing.T) {

func TestWriteRelationshipCmdFuncFromTTY(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
relationship, _ := tuple.ParseV1Rel(`resource:1#view@user:1[cav:{"letters": ["a", "b", "c"]}]`)
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: relationship,
Relationship: tuple.MustParseV1Rel(`resource:1#view@user:1[cav:{"letters": ["a", "b", "c"]}]`),
},
},
}}}, nil
Expand Down Expand Up @@ -285,12 +283,11 @@ func TestWriteRelationshipCmdFuncFromTTY(t *testing.T) {

func TestWriteRelationshipCmdFuncArgsTakePrecedence(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
relationship, _ := tuple.ParseV1Rel("resource:1#viewer@user:1")
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: relationship,
Relationship: tuple.MustParseV1Rel("resource:1#viewer@user:1"),
},
},
}}}, nil
Expand Down Expand Up @@ -332,17 +329,15 @@ func TestWriteRelationshipCmdFuncArgsTakePrecedence(t *testing.T) {

func TestWriteRelationshipCmdFuncFromStdin(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
one, _ := tuple.ParseV1Rel("resource:1#viewer@user:1")
two, _ := tuple.ParseV1Rel("resource:1#viewer@user:2")
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: one,
Relationship: tuple.MustParseV1Rel("resource:1#viewer@user:1"),
},
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: two,
Relationship: tuple.MustParseV1Rel("resource:1#viewer@user:2"),
},
},
}}}, nil
Expand Down Expand Up @@ -377,22 +372,20 @@ func TestWriteRelationshipCmdFuncFromStdin(t *testing.T) {

func TestWriteRelationshipCmdFuncFromStdinBatch(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
one, _ := tuple.ParseV1Rel(`resource:1#viewer@user:1[cav:{"letters": ["a", "b", "c"]}]`)
two, _ := tuple.ParseV1Rel(`resource:1#viewer@user:2[cav:{"letters": ["a", "b", "c"]}]`)
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{
{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: one,
Relationship: tuple.MustParseV1Rel(`resource:1#viewer@user:1[cav:{"letters": ["a", "b", "c"]}]`),
},
},
},
{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: two,
Relationship: tuple.MustParseV1Rel(`resource:1#viewer@user:2[cav:{"letters": ["a", "b", "c"]}]`),
},
},
},
Expand Down Expand Up @@ -428,13 +421,12 @@ func TestWriteRelationshipCmdFuncFromStdinBatch(t *testing.T) {

func TestWriteRelationshipCmdFuncFromFailsWithCaveatArg(t *testing.T) {
mock := func(*cobra.Command) (client.Client, error) {
relationship, _ := tuple.ParseV1Rel(`resource:1#viewer@user:1[cav:{"letters": ["a", "b", "c"]}]`)
return &mockClient{t: t, expectedWrites: []*v1.WriteRelationshipsRequest{
{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: relationship,
Relationship: tuple.MustParseV1Rel(`resource:1#viewer@user:1[cav:{"letters": ["a", "b", "c"]}]`),
},
},
},
Expand Down Expand Up @@ -597,22 +589,19 @@ func TestBulkDeleteForcing(t *testing.T) {
_, err = c.WriteSchema(ctx, &v1.WriteSchemaRequest{Schema: testSchema})
require.NoError(t, err)

one, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:1")
two, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:2")
three, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:3")
_, err = c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: one,
Relationship: tuple.MustParseV1Rel("test/resource:1#reader@test/user:1"),
},
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: two,
Relationship: tuple.MustParseV1Rel("test/resource:1#writer@test/user:2"),
},
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: three,
Relationship: tuple.MustParseV1Rel("test/resource:1#writer@test/user:3"),
},
},
})
Expand Down Expand Up @@ -652,10 +641,9 @@ func TestBulkDeleteManyForcing(t *testing.T) {

var updates []*v1.RelationshipUpdate
for i := 0; i < 200; i++ {
relationship, _ := tuple.ParseV1Rel(fmt.Sprintf("test/resource:%d#reader@test/user:1", i))
updates = append(updates, &v1.RelationshipUpdate{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: relationship,
Relationship: tuple.MustParseV1Rel(fmt.Sprintf("test/resource:%d#reader@test/user:1", i)),
})
}
_, err = c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{Updates: updates})
Expand Down Expand Up @@ -693,22 +681,19 @@ func TestBulkDeleteNotForcing(t *testing.T) {
_, err = c.WriteSchema(ctx, &v1.WriteSchemaRequest{Schema: testSchema})
require.NoError(t, err)

one, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:1")
two, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:2")
three, _ := tuple.ParseV1Rel("test/resource:1#reader@test/user:3")
_, err = c.WriteRelationships(ctx, &v1.WriteRelationshipsRequest{
Updates: []*v1.RelationshipUpdate{
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: one,
Relationship: tuple.MustParseV1Rel("test/resource:1#reader@test/user:1"),
},
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: two,
Relationship: tuple.MustParseV1Rel("test/resource:1#writer@test/user:2"),
},
{
Operation: v1.RelationshipUpdate_OPERATION_TOUCH,
Relationship: three,
Relationship: tuple.MustParseV1Rel("test/resource:1#writer@test/user:3"),
},
},
})
Expand Down

0 comments on commit 4524bc9

Please sign in to comment.