Skip to content

Commit

Permalink
PR(FIX): Remove the duplication introduced in sourcenetwork#3068
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Oct 20, 2024
1 parent 4bbc532 commit e013775
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 333 deletions.
150 changes: 16 additions & 134 deletions tests/integration/acp.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,15 @@ func addDocActorRelationshipACP(
s *state,
action AddDocActorRelationship,
) {
if action.NodeID.HasValue() {
nodeID := action.NodeID.Value()
collections := s.collections[nodeID]
node := s.nodes[nodeID]
nodeIDs, nodes := getNodesWithIDs(action.NodeID, s.nodes)
for index, node := range nodes {
nodeID := nodeIDs[index]

var collectionName string
if action.CollectionID == -1 {
collectionName = ""
} else {
collection := collections[action.CollectionID]
collection := s.collections[nodeID][action.CollectionID]
if !collection.Description().Name.HasValue() {
require.Fail(s.t, "Expected non-empty collection name, but it was empty.", s.testCase.Description)
}
Expand Down Expand Up @@ -245,69 +244,11 @@ func addDocActorRelationshipACP(
require.Equal(s.t, action.ExpectedError, "")
require.Equal(s.t, action.ExpectedExistence, exists.ExistedAlready)
}
} else {
for i, node := range getNodes(action.NodeID, s.nodes) {
var collectionName string
if action.CollectionID == -1 {
collectionName = ""
} else {
collection := s.collections[i][action.CollectionID]
if !collection.Description().Name.HasValue() {
require.Fail(s.t, "Expected non-empty collection name, but it was empty.", s.testCase.Description)
}
collectionName = collection.Description().Name.Value()
}

var docID string
if action.DocID == -1 || action.CollectionID == -1 {
docID = ""
} else {
docID = s.docIDs[action.CollectionID][action.DocID].String()
}

var targetIdentity string
if action.TargetIdentity == -1 {
targetIdentity = ""
} else {
optionalTargetIdentity := getIdentity(s, i, immutable.Some(action.TargetIdentity))
if !optionalTargetIdentity.HasValue() {
require.Fail(s.t, "Expected non-empty target identity, but it was empty.", s.testCase.Description)
}
targetIdentity = optionalTargetIdentity.Value().DID
}

var requestorIdentity immutable.Option[acpIdentity.Identity]
if action.RequestorIdentity == -1 {
requestorIdentity = acpIdentity.None
} else {
requestorIdentity = getIdentity(s, i, immutable.Some(action.RequestorIdentity))
if !requestorIdentity.HasValue() {
require.Fail(s.t, "Expected non-empty requestor identity, but it was empty.", s.testCase.Description)
}
}
ctx := db.SetContextIdentity(s.ctx, requestorIdentity)

exists, err := node.AddDocActorRelationship(
ctx,
collectionName,
docID,
action.Relation,
targetIdentity,
)

expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

if !expectedErrorRaised {
require.Equal(s.t, action.ExpectedError, "")
require.Equal(s.t, action.ExpectedExistence, exists.ExistedAlready)
}

// The relationship should only be added to a SourceHub chain once - there is no need to loop through
// the nodes.
if acpType == SourceHubACPType {
break
}
// The relationship should only be added to a SourceHub chain once - there is no need to loop through
// the nodes.
if acpType == SourceHubACPType {
break
}
}
}
Expand Down Expand Up @@ -363,16 +304,15 @@ func deleteDocActorRelationshipACP(
s *state,
action DeleteDocActorRelationship,
) {
if action.NodeID.HasValue() {
nodeID := action.NodeID.Value()
collections := s.collections[nodeID]
node := s.nodes[nodeID]
nodeIDs, nodes := getNodesWithIDs(action.NodeID, s.nodes)
for index, node := range nodes {
nodeID := nodeIDs[index]

var collectionName string
if action.CollectionID == -1 {
collectionName = ""
} else {
collection := collections[action.CollectionID]
collection := s.collections[nodeID][action.CollectionID]
if !collection.Description().Name.HasValue() {
require.Fail(s.t, "Expected non-empty collection name, but it was empty.", s.testCase.Description)
}
Expand Down Expand Up @@ -423,69 +363,11 @@ func deleteDocActorRelationshipACP(
require.Equal(s.t, action.ExpectedError, "")
require.Equal(s.t, action.ExpectedRecordFound, deleteDocActorRelationshipResult.RecordFound)
}
} else {
for i, node := range getNodes(action.NodeID, s.nodes) {
var collectionName string
if action.CollectionID == -1 {
collectionName = ""
} else {
collection := s.collections[i][action.CollectionID]
if !collection.Description().Name.HasValue() {
require.Fail(s.t, "Expected non-empty collection name, but it was empty.", s.testCase.Description)
}
collectionName = collection.Description().Name.Value()
}

var docID string
if action.DocID == -1 || action.CollectionID == -1 {
docID = ""
} else {
docID = s.docIDs[action.CollectionID][action.DocID].String()
}

var targetIdentity string
if action.TargetIdentity == -1 {
targetIdentity = ""
} else {
optionalTargetIdentity := getIdentity(s, i, immutable.Some(action.TargetIdentity))
if !optionalTargetIdentity.HasValue() {
require.Fail(s.t, "Expected non-empty target identity, but it was empty.", s.testCase.Description)
}
targetIdentity = optionalTargetIdentity.Value().DID
}

var requestorIdentity immutable.Option[acpIdentity.Identity]
if action.RequestorIdentity == -1 {
requestorIdentity = acpIdentity.None
} else {
requestorIdentity = getIdentity(s, i, immutable.Some(action.RequestorIdentity))
if !requestorIdentity.HasValue() {
require.Fail(s.t, "Expected non-empty requestor identity, but it was empty.", s.testCase.Description)
}
}
ctx := db.SetContextIdentity(s.ctx, requestorIdentity)

deleteDocActorRelationshipResult, err := node.DeleteDocActorRelationship(
ctx,
collectionName,
docID,
action.Relation,
targetIdentity,
)

expectedErrorRaised := AssertError(s.t, s.testCase.Description, err, action.ExpectedError)
assertExpectedErrorRaised(s.t, s.testCase.Description, action.ExpectedError, expectedErrorRaised)

if !expectedErrorRaised {
require.Equal(s.t, action.ExpectedError, "")
require.Equal(s.t, action.ExpectedRecordFound, deleteDocActorRelationshipResult.RecordFound)
}

// The relationship should only be added to a SourceHub chain once - there is no need to loop through
// the nodes.
if acpType == SourceHubACPType {
break
}
// The relationship should only be added to a SourceHub chain once - there is no need to loop through
// the nodes.
if acpType == SourceHubACPType {
break
}
}
}
Expand Down
Loading

0 comments on commit e013775

Please sign in to comment.