From 80493d892b47892fcc6f395c31b4c8854503b3d0 Mon Sep 17 00:00:00 2001 From: Tolga Ozen Date: Sat, 16 Sep 2023 18:22:54 +0300 Subject: [PATCH] test: EntityAndCallOrAttributeToString --- pkg/attribute/attribute_test.go | 64 +++++++++++++++++++++++++++++++++ pkg/tuple/tuple_test.go | 48 +++---------------------- 2 files changed, 69 insertions(+), 43 deletions(-) diff --git a/pkg/attribute/attribute_test.go b/pkg/attribute/attribute_test.go index f05a2afcc..3788763b8 100644 --- a/pkg/attribute/attribute_test.go +++ b/pkg/attribute/attribute_test.go @@ -100,5 +100,69 @@ var _ = Describe("attribute", func() { Expect(Attribute(tt.target)).Should(Equal(tt.attribute)) } }) + + It("EntityAndCallOrAttributeToString", func() { + tests := []struct { + entity *base.Entity + attributeOrCall string + arguments []*base.Argument + result string + }{ + { + entity: &base.Entity{ + Type: "organization", + Id: "1", + }, + attributeOrCall: "check_credit", + arguments: []*base.Argument{ + { + Type: &base.Argument_ComputedAttribute{ + ComputedAttribute: &base.ComputedAttribute{ + Name: "credit", + }, + }, + }, + }, + result: "organization:1$check_credit(credit)", + }, + { + entity: &base.Entity{ + Type: "organization", + Id: "1", + }, + attributeOrCall: "is_public", + arguments: nil, + result: "organization:1$is_public", + }, + { + entity: &base.Entity{ + Type: "organization", + Id: "877", + }, + attributeOrCall: "check_balance", + arguments: []*base.Argument{ + { + Type: &base.Argument_ContextAttribute{ + ContextAttribute: &base.ContextAttribute{ + Name: "amount", + }, + }, + }, + { + Type: &base.Argument_ComputedAttribute{ + ComputedAttribute: &base.ComputedAttribute{ + Name: "balance", + }, + }, + }, + }, + result: "organization:877$check_balance(request.amount,balance)", + }, + } + + for _, tt := range tests { + Expect(EntityAndCallOrAttributeToString(tt.entity, tt.attributeOrCall, tt.arguments...)).Should(Equal(tt.result)) + } + }) }) }) diff --git a/pkg/tuple/tuple_test.go b/pkg/tuple/tuple_test.go index d5ce93170..5eccf48b9 100644 --- a/pkg/tuple/tuple_test.go +++ b/pkg/tuple/tuple_test.go @@ -20,9 +20,8 @@ var _ = Describe("tuple", func() { Context("TupleToString", func() { It("EntityAndRelationToString", func() { tests := []struct { - target *base.EntityAndRelation - arguments []*base.Argument - expected string + target *base.EntityAndRelation + expected string }{ {&base.EntityAndRelation{ Entity: &base.Entity{ @@ -30,55 +29,18 @@ var _ = Describe("tuple", func() { Id: "1", }, Relation: "admin", - }, nil, "repository:1#admin"}, + }, "repository:1#admin"}, {&base.EntityAndRelation{ Entity: &base.Entity{ Type: "doc", Id: "1", }, Relation: "viewer", - }, nil, "doc:1#viewer"}, - {&base.EntityAndRelation{ - Entity: &base.Entity{ - Type: "doc", - Id: "1", - }, - Relation: "check_balance", - }, []*base.Argument{ - { - Type: &base.Argument_ComputedAttribute{ - ComputedAttribute: &base.ComputedAttribute{ - Name: "balance", - }, - }, - }, - }, "doc:1#check_balance(balance)"}, - {&base.EntityAndRelation{ - Entity: &base.Entity{ - Type: "doc", - Id: "1", - }, - Relation: "check_balance", - }, []*base.Argument{ - { - Type: &base.Argument_ContextAttribute{ - ContextAttribute: &base.ContextAttribute{ - Name: "amount", - }, - }, - }, - { - Type: &base.Argument_ComputedAttribute{ - ComputedAttribute: &base.ComputedAttribute{ - Name: "balance", - }, - }, - }, - }, "doc:1#check_balance(request.amount,balance)"}, + }, "doc:1#viewer"}, } for _, tt := range tests { - Expect(EntityAndRelationToString(tt.target, tt.arguments...)).Should(Equal(tt.expected)) + Expect(EntityAndRelationToString(tt.target.GetEntity(), tt.target.GetRelation())).Should(Equal(tt.expected)) } }) })