Skip to content

Commit

Permalink
fix(reactions): prevent setting reactions from creating duplicates du…
Browse files Browse the repository at this point in the history
…e to Cartesian product
  • Loading branch information
undrcrxwn committed Nov 17, 2024
1 parent 8a70a64 commit 20558da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ OPTIONAL MATCH (viewer)-[reaction:REACTED_TO]->(subject)
DELETE reaction
WITH viewer, subject
WITH DISTINCT viewer, subject
FOREACH (newReactionValue IN $reactions |
CREATE (viewer)-[:REACTED_TO { Value: newReactionValue }]->(subject)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,33 @@ public async Task SetReactions_OnlyWorksWithAllowedReactionSets()
await reactWithNewInvalid.Should().ThrowAsync<ForbiddenException>();
reactions.Should().BeEquivalentTo(thumbs);
}

[Fact(DisplayName = "Setting reactions overwrites existing reactions")]
public async Task SetReactions_OverwritesExistingReactions()
{
// Arrange
await using var scope = _services.CreateAsyncScope();
var authorsRepository = scope.ServiceProvider.GetRequiredService<IAuthorsRepository>();
var discussionsRepository = scope.ServiceProvider.GetRequiredService<IDiscussionsRepository>();
var reactionsRepository = scope.ServiceProvider.GetRequiredService<IReactionsRepository>();

var viewerId = Guid.NewGuid();
await authorsRepository.EnsureCreatedAsync(viewerId);
var discussionId = await discussionsRepository.CreateAsync(viewerId, "Title", "Description");

// Act
await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet<string> { "a" });
await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet<string> { "a", "b" });
await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet<string> { "a", "b", "c" });
await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet<string> { "b", "d" });

// Assert
var discussion = await discussionsRepository.GetByIdAsync(discussionId, viewerId);
discussion.ViewerReactions.Should().BeEquivalentTo(["b", "d"]);
discussion.ReactionCounters.Should().BeEquivalentTo(new Dictionary<string, int>
{
{ "b", 1 },
{ "d", 1 }
});
}
}

0 comments on commit 20558da

Please sign in to comment.