Skip to content

Commit 9353887

Browse files
committed
Follow bunny advice
1 parent 6363c30 commit 9353887

File tree

9 files changed

+47
-39
lines changed

9 files changed

+47
-39
lines changed

Backend.Tests/Controllers/AudioControllerTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,12 @@ public void TestDeleteAudioFileInvalidArguments()
163163
[Test]
164164
public void TestDeleteAudioFileNoWordWithAudio()
165165
{
166-
var result = _audioController.DeleteAudioFile(_projId, "not-a-word", _file.FileName).Result;
167-
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
166+
var result1 = _audioController.DeleteAudioFile(_projId, "not-a-word", _file.FileName).Result;
167+
Assert.That(result1, Is.InstanceOf<NotFoundObjectResult>());
168168

169169
var wordId = _wordRepo.Create(Util.RandomWord(_projId)).Result.Id;
170-
var ex = Assert.Throws<AggregateException>(() =>
171-
{
172-
_ = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
173-
});
174-
Assert.That(ex?.InnerException, Is.InstanceOf<ArgumentException>());
170+
var result2 = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
171+
Assert.That(result2, Is.InstanceOf<NotFoundObjectResult>());
175172
}
176173

177174
[Test]

Backend.Tests/Mocks/WordRepositoryMock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ public Task<bool> RestoreFrontier(string projectId, string wordId)
233233
return await UpdateFrontier(word, createIfNotFound: false, modifyUpdatedWord);
234234
}
235235

236-
public async Task<List<Word>?> ReplaceFrontier(string projectId, List<Word> newWords,
236+
public async Task<List<Word>> ReplaceFrontier(string projectId, List<Word> newWords,
237237
List<string> idsToDelete, Action<Word, Word?> modifyUpdatedWord, Action<Word> modifyDeletedWord)
238238
{
239239
if (newWords.Any(w => w.ProjectId != projectId))

Backend.Tests/Services/WordServiceTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ public void TestDeleteAudioBadInput()
9999
Assert.That(_wordService.DeleteAudio("non-proj-id", UserId, wordInFrontier.Id, fileName).Result, Is.Null);
100100
Assert.That(_wordService.DeleteAudio(ProjId, UserId, "non-word-id", fileName).Result, Is.Null);
101101

102-
var ex = Assert.Throws<AggregateException>(
103-
() => _wordService.DeleteAudio(ProjId, UserId, wordInFrontier.Id, "non-file-name").Wait());
104-
Assert.That(ex?.InnerException, Is.InstanceOf<ArgumentException>());
102+
var result = _wordService.DeleteAudio(ProjId, UserId, wordInFrontier.Id, "non-file-name").Result;
103+
Assert.That(result, Is.Null);
105104
}
106105

107106
[Test]
@@ -197,9 +196,9 @@ public void TestRestoreFrontierWordAlreadyInFrontierThrows()
197196
var wordInFrontier = _wordRepo.Create(new Word { ProjectId = ProjId }).Result;
198197
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(1));
199198

200-
Assert.That(
201-
() => _wordService.RestoreFrontierWord(ProjId, wordInFrontier.Id).Wait(),
202-
Throws.TypeOf<AggregateException>());
199+
var ex = Assert.Throws<AggregateException>(
200+
() => _wordService.RestoreFrontierWord(ProjId, wordInFrontier.Id).Wait());
201+
Assert.That(ex?.InnerException, Is.InstanceOf<ArgumentException>());
203202
}
204203

205204
[Test]

Backend/Contexts/MongoDbContext.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,16 @@ public MongoDbContext(IOptions<Startup.Settings> options)
3333
public async Task<IMongoTransaction> BeginTransaction()
3434
{
3535
var session = await Db.Client.StartSessionAsync();
36-
session.StartTransaction();
37-
return new MongoTransactionWrapper(session);
36+
try
37+
{
38+
session.StartTransaction();
39+
return new MongoTransactionWrapper(session);
40+
}
41+
catch
42+
{
43+
session.Dispose();
44+
throw;
45+
}
3846
}
3947

4048
/// <summary>

Backend/Interfaces/IWordRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IWordRepository
2525
Task<bool> RestoreFrontier(string projectId, string wordId);
2626
Task<Word?> UpdateFrontier(string projectId, string wordId, Action<Word> modifyUpdatedWord);
2727
Task<Word?> UpdateFrontier(Word word, Action<Word, Word?> modifyUpdatedWord);
28-
Task<List<Word>?> ReplaceFrontier(string projectId, List<Word> newWords, List<string> idsToDelete,
28+
Task<List<Word>> ReplaceFrontier(string projectId, List<Word> newWords, List<string> idsToDelete,
2929
Action<Word, Word?> modifyUpdatedWord, Action<Word> modifyDeletedWord);
3030
Task<bool> RevertReplaceFrontier(string projectId, List<string> idsToRestore, List<string> idsToDelete,
3131
Action<Word> modifyDeletedWord);

Backend/Interfaces/IWordService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface IWordService
1313
Task<bool> RestoreFrontierWord(string projectId, string wordId);
1414
Task<Word?> Update(string userId, Word word);
1515
Task<string?> FindContainingWord(Word word);
16-
Task<List<Word>?> MergeReplaceFrontier(
16+
Task<List<Word>> MergeReplaceFrontier(
1717
string projectId, string userId, List<Word> parents, List<string> idsToDelete);
1818
Task<bool> RevertMergeReplaceFrontier(
1919
string projectId, string userId, List<string> idsToRestore, List<string> idsToDelete);

Backend/Repositories/WordRepository.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task<List<Word>> Create(List<Word> words)
124124
OtelService.StartActivityWithTag(otelTagName, "creating words in WordsCollection and Frontier");
125125

126126
return words.Count == 0
127-
? []
127+
? words
128128
: await _dbContext.ExecuteInTransaction(async s => await CreateWithSession(s, words));
129129
}
130130

@@ -335,15 +335,13 @@ public async Task<bool> RestoreFrontier(string projectId, string wordId)
335335
/// <param name="modifyDeletedWord">
336336
/// Action applied to words deleted from Frontier before inserting into WordsCollection.
337337
/// </param>
338-
/// <returns>
339-
/// The replacement words when successful, an empty list if no work is needed, or null if replacement fails.
340-
/// </returns>
341-
public async Task<List<Word>?> ReplaceFrontier(string projectId, List<Word> newWords,
338+
/// <returns>The replacement words when successful, an empty list if no work is needed.</returns>
339+
public async Task<List<Word>> ReplaceFrontier(string projectId, List<Word> newWords,
342340
List<string> idsToDelete, Action<Word, Word?> modifyUpdatedWord, Action<Word> modifyDeletedWord)
343341
{
344342
return (newWords.Count == 0 && idsToDelete.Count == 0)
345-
? []
346-
: await _dbContext.ExecuteInTransactionAllowNull(async s => await ReplaceFrontierWithSession(
343+
? newWords
344+
: await _dbContext.ExecuteInTransaction(async s => await ReplaceFrontierWithSession(
347345
s, projectId, newWords, idsToDelete, modifyUpdatedWord, modifyDeletedWord));
348346
}
349347

@@ -469,8 +467,9 @@ private async Task<List<Word>> CreateWithSession(IClientSessionHandle session, L
469467
/// <param name="modifyDeletedWord">Action applied on deleted Frontier words added to WordsCollection.</param>
470468
/// <returns>True when all requested restores succeed; otherwise false.</returns>
471469
/// <exception cref="ArgumentException">Thrown when restore and delete id sets are not disjoint.</exception>
472-
private async Task<bool> RevertReplaceFrontierWithSession(IClientSessionHandle session, string projectId,
473-
IEnumerable<string> idsToRestore, IEnumerable<string> idsToDelete, Action<Word> modifyDeletedWord)
470+
private async Task<bool> RevertReplaceFrontierWithSession(IClientSessionHandle session,
471+
string projectId, IEnumerable<string> idsToRestore, IEnumerable<string> idsToDelete,
472+
Action<Word> modifyDeletedWord)
474473
{
475474
var restoreSet = idsToRestore.ToHashSet(); // Remove duplicates
476475
if (restoreSet.Intersect(idsToDelete).Any())
@@ -498,9 +497,9 @@ private async Task<bool> RevertReplaceFrontierWithSession(IClientSessionHandle s
498497
/// <param name="oldWordIds">Ids of Frontier words that will be replaced or deleted.</param>
499498
/// <param name="modifyUpdatedWord">Action applied when building each replacement word.</param>
500499
/// <param name="modifyDeletedWord">Action applied on deleted Frontier words added to WordsCollection.</param>
501-
/// <returns>The replacement words, or null if a required update fails.</returns>
500+
/// <returns>The replaced words.</returns>
502501
/// <exception cref="ArgumentException">Thrown when a replacement word has a different project id.</exception>
503-
private async Task<List<Word>?> ReplaceFrontierWithSession(IClientSessionHandle session,
502+
private async Task<List<Word>> ReplaceFrontierWithSession(IClientSessionHandle session,
504503
string projectId, List<Word> newWords, IEnumerable<string> oldWordIds,
505504
Action<Word, Word?> modifyUpdatedWord, Action<Word> modifyDeletedWord)
506505
{
@@ -509,15 +508,14 @@ private async Task<bool> RevertReplaceFrontierWithSession(IClientSessionHandle s
509508
throw new ArgumentException("All new words must have the specified projectId");
510509
}
511510

512-
var oldIdSet = oldWordIds.ToHashSet(); // Remove duplicates
511+
var oldIdSet = oldWordIds.ToHashSet(); // Remove duplicates and allow easy removal for each update.
513512

514513
foreach (var word in newWords)
515514
{
515+
// Remove the id from the old ids (if present) before the word is updated and given a new id.
516516
oldIdSet.Remove(word.Id);
517-
if (await UpdateFrontierWithSession(session, word, createIfNotFound: true, modifyUpdatedWord) is null)
518-
{
519-
return null;
520-
}
517+
// `createIfNotFound: true` so the word is created even if the id isn't in the Frontier.
518+
await UpdateFrontierWithSession(session, word, createIfNotFound: true, modifyUpdatedWord);
521519
}
522520

523521
// Delete any remaining old words that weren't updated with a new word

Backend/Services/MergeService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ public async Task<List<Word>> Merge(string projectId, string userId, List<MergeW
139139

140140
// Consolidate children ids
141141
var childrenIds = mergeWordsList.SelectMany(m => m.Children.Select(c => c.SrcWordId)).ToHashSet();
142-
return await _wordService.MergeReplaceFrontier(
143-
projectId, userId, parents.ToList(), childrenIds.ToList()) ?? [];
142+
return await _wordService.MergeReplaceFrontier(projectId, userId, parents.ToList(), childrenIds.ToList());
144143
}
145144

146145
/// <summary> Undo merge </summary>

Backend/Services/WordService.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ public async Task<Word> Create(string userId, Word word)
151151
{
152152
using var activity = OtelService.StartActivityWithTag(otelTagName, "deleting an audio");
153153

154-
return await _wordRepo.UpdateFrontier(projectId, wordId, CreateDeleteAudioAction(userId, fileName));
154+
try
155+
{
156+
return await _wordRepo.UpdateFrontier(projectId, wordId, CreateDeleteAudioAction(userId, fileName));
157+
}
158+
catch (ArgumentException)
159+
{
160+
return null;
161+
}
155162
}
156163

157164
/// <summary> Removes word from Frontier and adds a Deleted copy in the words collection </summary>
@@ -208,8 +215,8 @@ public async Task<bool> RestoreFrontierWord(string projectId, string wordId)
208215
/// <param name="userId">Id of the user performing the merge.</param>
209216
/// <param name="parents">Parent words to create or use as replacements.</param>
210217
/// <param name="idsToDelete">Ids of merge children to delete from Frontier.</param>
211-
/// <returns>The updated parent words, or null if replacement fails.</returns>
212-
public async Task<List<Word>?> MergeReplaceFrontier(
218+
/// <returns>The updated parent words.</returns>
219+
public async Task<List<Word>> MergeReplaceFrontier(
213220
string projectId, string userId, List<Word> parents, List<string> idsToDelete)
214221
{
215222
using var activity = OtelService.StartActivityWithTag(otelTagName, "replacing frontier words for merge");

0 commit comments

Comments
 (0)