Skip to content

Commit 44c4697

Browse files
authored
Merge pull request #1609 from Permify/feature/add-linked-schema-test-cases
test: add test cases for linked schema
2 parents e1442dc + 9e8be60 commit 44c4697

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed

internal/schema/linkedSchema_test.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,5 +1021,165 @@ var _ = Describe("linked schema", func() {
10211021
},
10221022
}))
10231023
})
1024+
1025+
It("Case 21", func() {
1026+
sch, err := parser.NewParser(`
1027+
entity user {}
1028+
1029+
entity organization {
1030+
relation owner @user
1031+
1032+
attribute balance integer
1033+
1034+
permission withdraw = check_balance(balance) or owner
1035+
}
1036+
1037+
entity account {
1038+
1039+
relation parent @organization
1040+
1041+
relation owner @user
1042+
1043+
attribute balance integer
1044+
1045+
permission withdraw = check_balance(balance) and owner or parent.withdraw
1046+
}
1047+
1048+
rule check_balance(balance integer) {
1049+
(balance >= 5000)
1050+
}
1051+
`).Parse()
1052+
1053+
Expect(err).ShouldNot(HaveOccurred())
1054+
1055+
c := compiler.NewCompiler(true, sch)
1056+
a, _, _ := c.Compile()
1057+
1058+
g := NewLinkedGraph(NewSchemaFromEntityAndRuleDefinitions(a, nil))
1059+
1060+
ent, err := g.LinkedEntrances(&base.Entrance{
1061+
Type: "account",
1062+
Value: "withdraw",
1063+
}, &base.Entrance{
1064+
Type: "user",
1065+
Value: "",
1066+
})
1067+
1068+
Expect(err).ShouldNot(HaveOccurred())
1069+
Expect(ent).Should(Equal([]*LinkedEntrance{
1070+
{
1071+
Kind: AttributeLinkedEntrance,
1072+
TargetEntrance: &base.Entrance{
1073+
Type: "account",
1074+
Value: "balance",
1075+
},
1076+
TupleSetRelation: "",
1077+
},
1078+
{
1079+
Kind: RelationLinkedEntrance,
1080+
TargetEntrance: &base.Entrance{
1081+
Type: "account",
1082+
Value: "owner",
1083+
},
1084+
TupleSetRelation: "",
1085+
},
1086+
{
1087+
Kind: AttributeLinkedEntrance,
1088+
TargetEntrance: &base.Entrance{
1089+
Type: "organization",
1090+
Value: "balance",
1091+
},
1092+
TupleSetRelation: "",
1093+
},
1094+
{
1095+
Kind: RelationLinkedEntrance,
1096+
TargetEntrance: &base.Entrance{
1097+
Type: "organization",
1098+
Value: "owner",
1099+
},
1100+
TupleSetRelation: "",
1101+
},
1102+
}))
1103+
})
1104+
1105+
It("Case 22", func() {
1106+
sch, err := parser.NewParser(`
1107+
entity user {}
1108+
1109+
entity organization {
1110+
relation owner @user
1111+
1112+
attribute balance integer
1113+
1114+
permission withdraw = check_balance(balance) or owner
1115+
}
1116+
1117+
entity account {
1118+
1119+
relation parent @organization
1120+
1121+
relation owner @user @account#owner
1122+
1123+
attribute balance integer
1124+
1125+
permission withdraw = check_balance(balance) and owner or parent.withdraw
1126+
}
1127+
1128+
rule check_balance(balance integer) {
1129+
(balance >= 5000)
1130+
}
1131+
`).Parse()
1132+
1133+
Expect(err).ShouldNot(HaveOccurred())
1134+
1135+
c := compiler.NewCompiler(true, sch)
1136+
a, _, _ := c.Compile()
1137+
1138+
g := NewLinkedGraph(NewSchemaFromEntityAndRuleDefinitions(a, nil))
1139+
1140+
ent, err := g.LinkedEntrances(&base.Entrance{
1141+
Type: "account",
1142+
Value: "withdraw",
1143+
}, &base.Entrance{
1144+
Type: "account",
1145+
Value: "owner",
1146+
})
1147+
1148+
Expect(err).ShouldNot(HaveOccurred())
1149+
Expect(ent).Should(Equal([]*LinkedEntrance{
1150+
{
1151+
Kind: AttributeLinkedEntrance,
1152+
TargetEntrance: &base.Entrance{
1153+
Type: "account",
1154+
Value: "balance",
1155+
},
1156+
TupleSetRelation: "",
1157+
},
1158+
{
1159+
Kind: ComputedUserSetLinkedEntrance,
1160+
TargetEntrance: &base.Entrance{
1161+
Type: "account",
1162+
Value: "withdraw",
1163+
},
1164+
TupleSetRelation: "",
1165+
},
1166+
{
1167+
Kind: RelationLinkedEntrance,
1168+
TargetEntrance: &base.Entrance{
1169+
Type: "account",
1170+
Value: "owner",
1171+
},
1172+
TupleSetRelation: "",
1173+
},
1174+
{
1175+
Kind: AttributeLinkedEntrance,
1176+
TargetEntrance: &base.Entrance{
1177+
Type: "organization",
1178+
Value: "balance",
1179+
},
1180+
TupleSetRelation: "",
1181+
},
1182+
}))
1183+
})
10241184
})
10251185
})

0 commit comments

Comments
 (0)