Skip to content

Commit

Permalink
Merge pull request #985 from Permify/storage-tests
Browse files Browse the repository at this point in the history
test: increase test coverage
  • Loading branch information
tolgaOzen committed Dec 31, 2023
2 parents b437b15 + 54fcf4b commit c784919
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 35 deletions.
11 changes: 11 additions & 0 deletions internal/schema/linkedSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,5 +910,16 @@ var _ = Describe("linked schema", func() {
},
}))
})

It("Case 18", func() {
Expect(LinkedEntrance{
Kind: RelationLinkedEntrance,
TargetEntrance: &base.RelationReference{
Type: "account",
Relation: "admin",
},
TupleSetRelation: "",
}.LinkedEntranceKind()).Should(Equal(RelationLinkedEntrance))
})
})
})
81 changes: 46 additions & 35 deletions internal/storage/postgres/dataWriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,66 +423,77 @@ var _ = Describe("DataWriter", func() {
})

Context("RunBundle", func() {
It("", func() {
It("should run the bundle successfully and return an encoded snapshot token", func() {
ctx := context.Background()

bundles := []*base.DataBundle{
{
Name: "user_created",
Arguments: []string{
"organizationID",
"userID",
},
Operations: []*base.Operation{
{
RelationshipsWrite: []string{
"organization:{{.organizationID}}#member@user:{{.userID}}",
"organization:{{.organizationID}}#admin@user:{{.userID}}",
},
RelationshipsDelete: []string{},
AttributesWrite: []string{
"organization:{{.organizationID}}$public|boolean:true",
},
AttributesDelete: []string{},
// Create a valid DataBundle
bundle := &base.DataBundle{
Name: "user_created",
Arguments: []string{
"organizationID",
"companyID",
"userID",
},
Operations: []*base.Operation{
{
RelationshipsWrite: []string{
"organization:{{.organizationID}}#member@company:{{.companyID}}#admin",
"organization:{{.organizationID}}#member@user:{{.userID}}",
"organization:{{.organizationID}}#admin@user:{{.userID}}",
},
RelationshipsDelete: []string{
"organization:{{.organizationID}}#admin@user:{{.userID}}",
},
AttributesWrite: []string{
"organization:{{.organizationID}}$public|boolean:true",
"company:{{.companyID}}$public|boolean:true",
},
AttributesDelete: []string{
"organization:{{.organizationID}}$balance|double:120.900",
},
},
},
}

var sBundles []storage.Bundle
for _, b := range bundles {
sBundles = append(sBundles, storage.Bundle{
Name: b.Name,
DataBundle: b,
_, err := bundleWriter.Write(ctx, []storage.Bundle{
{
Name: bundle.Name,
DataBundle: bundle,
TenantID: "t1",
})
}

_, err := bundleWriter.Write(ctx, sBundles)
},
})
Expect(err).ShouldNot(HaveOccurred())

bundle, err := bundleReader.Read(ctx, "t1", "user_created")
dataBundle, err := bundleReader.Read(ctx, "t1", "user_created")
Expect(err).ShouldNot(HaveOccurred())

token1, err := dataWriter.RunBundle(ctx, "t1", map[string]string{
"organizationID": "12",
"userID": "190",
}, bundle)
"organizationID": "1",
"companyID": "4",
"userID": "1",
}, dataBundle)
Expect(err).ShouldNot(HaveOccurred())

colT1, _, err := dataReader.ReadRelationships(ctx, "t1", &base.TupleFilter{
Entity: &base.EntityFilter{
Type: "organization",
Ids: []string{"12"},
Ids: []string{"1"},
},
Relation: "",
Subject: &base.SubjectFilter{
Type: "",
Ids: []string{},
Relation: "",
},
}, token1.String(), database.NewPagination(database.Size(10), database.Token("")))
Expect(len(colT1.GetTuples())).Should(Equal(2))

colA2, _, err := dataReader.ReadAttributes(ctx, "t1", &base.AttributeFilter{
Entity: &base.EntityFilter{
Type: "organization",
Ids: []string{"12"},
Type: "company",
Ids: []string{"4"},
},
Attributes: []string{},
}, token1.String(), database.NewPagination(database.Size(10), database.Token("")))
Expect(len(colA2.GetAttributes())).Should(Equal(1))
})
Expand Down
45 changes: 45 additions & 0 deletions internal/storage/postgres/snapshot/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,51 @@ var _ = Describe("token", func() {
Expect(tt.target.Encode().String()).ShouldNot(Equal(tt.expected))
}
})

It("Case 3: Eg Success", func() {
tests := []struct {
token token.SnapToken
target token.SnapToken
}{
{NewToken(types.XID8{Uint: 4, Status: pgtype.Present}), NewToken(types.XID8{Uint: 4, Status: pgtype.Present})},
}

for _, tt := range tests {
Expect(tt.token.Eg(tt.target)).Should(BeTrue())
}
})

It("Case 4: Gt Success", func() {
tests := []struct {
token token.SnapToken
target token.SnapToken
}{
{
NewToken(types.XID8{Uint: 6, Status: pgtype.Present}),
NewToken(types.XID8{Uint: 4, Status: pgtype.Present}),
},
}

for _, tt := range tests {
Expect(tt.token.Gt(tt.target)).Should(BeTrue())
}
})

It("Case 5: Lt Success", func() {
tests := []struct {
token token.SnapToken
target token.SnapToken
}{
{
NewToken(types.XID8{Uint: 4, Status: pgtype.Present}),
NewToken(types.XID8{Uint: 6, Status: pgtype.Present}),
},
}

for _, tt := range tests {
Expect(tt.token.Lt(tt.target)).Should(BeTrue())
}
})
})

Context("Decode", func() {
Expand Down

0 comments on commit c784919

Please sign in to comment.