diff --git a/Backend.Tests/Services/MergeServiceTests.cs b/Backend.Tests/Services/MergeServiceTests.cs index 2f0580975c..c5373b552f 100644 --- a/Backend.Tests/Services/MergeServiceTests.cs +++ b/Backend.Tests/Services/MergeServiceTests.cs @@ -199,8 +199,6 @@ public void UndoMergeMultiChildTest() [Test] public void AddMergeToBlacklistTest() { - _ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result; - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List { "1", "2" }; // Adding to blacklist should clear from graylist @@ -219,7 +217,6 @@ public void AddMergeToBlacklistTest() [Test] public void AddMergeToBlacklistErrorTest() { - _ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result; var wordIds0 = new List(); var wordIds1 = new List { "1" }; Assert.That( @@ -231,7 +228,6 @@ public void AddMergeToBlacklistErrorTest() [Test] public void IsInMergeBlacklistTest() { - _ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List { "1", "2", "3" }; var subWordIds = new List { "3", "2" }; @@ -243,7 +239,6 @@ public void IsInMergeBlacklistTest() [Test] public void IsInMergeBlacklistErrorTest() { - _ = _mergeBlacklistRepo.DeleteAllSets(ProjId).Result; var wordIds0 = new List(); var wordIds1 = new List { "1" }; Assert.That( @@ -298,7 +293,6 @@ public void UpdateMergeBlacklistTest() [Test] public void AddMergeToGraylistTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List { "1", "2" }; _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result; var graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result; @@ -307,10 +301,24 @@ public void AddMergeToGraylistTest() Assert.That(expectedEntry.ContentEquals(graylist.First()), Is.True); } + [Test] + public void AddMergeToGraylistSupersetTest() + { + var wordIds12 = new List { "1", "2" }; + var wordIds13 = new List { "1", "3" }; + var wordIds123 = new List { "1", "2", "3" }; + + _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds12).Result; + _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds13).Result; + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(2)); + + _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds123).Result; + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1)); + } + [Test] public void AddMergeToGraylistErrorTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List(); var wordIds1 = new List { "1" }; Assert.That( @@ -324,24 +332,28 @@ public void AddMergeToGraylistErrorTest() [Test] public void RemoveFromMergeGraylistTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; - var wordIds12 = new List { "1", "2" }; - var wordIds13 = new List { "1", "3" }; - _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds12).Result; - _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds13).Result; - var graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result; - Assert.That(graylist, Has.Count.EqualTo(2)); - var wordIds123 = new List { "1", "2", "3" }; - var removed = _mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds123).Result; - Assert.That(removed, Has.Count.EqualTo(2)); - graylist = _mergeGraylistRepo.GetAllSets(ProjId).Result; - Assert.That(graylist, Is.Empty); + var wordIds = new List { "1", "2", "3" }; + _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result; + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1)); + Assert.That(_mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds).Result, Is.True); + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(0)); + } + + [Test] + public void RemoveFromMergeGraylistSupersetTest() + { + var wordIds = new List { "1", "2" }; + _ = _mergeService.AddToMergeGraylist(ProjId, UserId, wordIds).Result; + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1)); + + wordIds.Add("3"); + Assert.That(_mergeService.RemoveFromMergeGraylist(ProjId, UserId, wordIds).Result, Is.False); + Assert.That(_mergeGraylistRepo.GetAllSets(ProjId).Result, Has.Count.EqualTo(1)); } [Test] public void RemoveFromMergeGraylistErrorTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List(); var wordIds1 = new List { "1" }; Assert.That( @@ -355,7 +367,6 @@ public void RemoveFromMergeGraylistErrorTest() [Test] public void IsInMergeGraylistTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds = new List { "1", "2", "3" }; var subWordIds = new List { "3", "2" }; @@ -367,7 +378,6 @@ public void IsInMergeGraylistTest() [Test] public void IsInMergeGraylistErrorTest() { - _ = _mergeGraylistRepo.DeleteAllSets(ProjId).Result; var wordIds0 = new List(); var wordIds1 = new List { "1" }; Assert.That( diff --git a/Backend/Interfaces/IMergeService.cs b/Backend/Interfaces/IMergeService.cs index 71e6562588..40beae11b5 100644 --- a/Backend/Interfaces/IMergeService.cs +++ b/Backend/Interfaces/IMergeService.cs @@ -10,7 +10,7 @@ public interface IMergeService Task UndoMerge(string projectId, MergeUndoIds ids); Task AddToMergeBlacklist(string projectId, string userId, List wordIds); Task AddToMergeGraylist(string projectId, string userId, List wordIds); - Task> RemoveFromMergeGraylist(string projectId, string userId, List wordIds); + Task RemoveFromMergeGraylist(string projectId, string userId, List wordIds); Task IsInMergeBlacklist(string projectId, List wordIds, string? userId = null); Task IsInMergeGraylist(string projectId, List wordIds, string? userId = null); Task UpdateMergeBlacklist(string projectId); diff --git a/Backend/Services/MergeService.cs b/Backend/Services/MergeService.cs index 9e5903dbd0..01be6ef365 100644 --- a/Backend/Services/MergeService.cs +++ b/Backend/Services/MergeService.cs @@ -148,6 +148,7 @@ public async Task AddToMergeBlacklist( public async Task AddToMergeGraylist( string projectId, string userId, List wordIds) { + wordIds = wordIds.Distinct().ToList(); if (wordIds.Count < 2) { throw new InvalidMergeWordSetException("Cannot graylist a list of fewer than 2 wordIds."); @@ -169,27 +170,27 @@ public async Task AddToMergeGraylist( /// Remove a List of wordIds from MergeGraylist of specified . /// Throws when wordIds has count less than 2. - /// List of removed ids. - public async Task> RemoveFromMergeGraylist( + /// Boolean indicating whether anything was removed. + public async Task RemoveFromMergeGraylist( string projectId, string userId, List wordIds) { + wordIds = wordIds.Distinct().ToList(); if (wordIds.Count < 2) { throw new InvalidMergeWordSetException("Cannot have a graylist entry with fewer than 2 wordIds."); } - // Remove all graylist entries fully contained in the input List. + // Remove only the set of wordIds, if in graylist (not any subsets) var graylist = await _mergeGraylistRepo.GetAllSets(projectId, userId); - var removed = new List(); foreach (var entry in graylist) { - if (entry.WordIds.All(wordIds.Contains)) + if (entry.WordIds.All(wordIds.Contains) && wordIds.Count == entry.WordIds.Count) { await _mergeGraylistRepo.Delete(projectId, entry.Id); - removed.Add(entry.Id); + return true; } } - return removed; + return false; } /// Check if List of wordIds is in MergeBlacklist for specified .