Skip to content

Commit 0dc6255

Browse files
committed
Use MongoDB transactions
1 parent 74c3997 commit 0dc6255

24 files changed

+1253
-408
lines changed

Backend.Tests/Controllers/AudioControllerTests.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33
using Backend.Tests.Mocks;
44
using BackendFramework.Controllers;
@@ -14,7 +14,7 @@ namespace Backend.Tests.Controllers
1414
internal sealed class AudioControllerTests : IDisposable
1515
{
1616
private IProjectRepository _projRepo = null!;
17-
private IWordRepository _wordRepo = null!;
17+
private WordRepositoryMock _wordRepo = null!;
1818
private PermissionServiceMock _permissionService = null!;
1919
private WordService _wordService = null!;
2020
private AudioController _audioController = null!;
@@ -163,20 +163,19 @@ 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-
result = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
171-
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
170+
var result2 = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
171+
Assert.That(result2, Is.InstanceOf<NotFoundObjectResult>());
172172
}
173173

174174
[Test]
175175
public void TestDeleteAudioFile()
176176
{
177177
// Refill test database
178-
_wordRepo.DeleteAllWords(_projId).Wait();
179-
_wordRepo.DeleteAllFrontierWords(_projId).Wait();
178+
_wordRepo.DeleteAllWords(_projId);
180179
var origWord = Util.RandomWord(_projId);
181180
const string fileName = "a.wav";
182181
origWord.Audio.Add(new Pronunciation(fileName));

Backend.Tests/Controllers/LiftControllerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Text;
@@ -21,7 +21,7 @@ internal sealed class LiftControllerTests : IDisposable
2121
{
2222
private IProjectRepository _projRepo = null!;
2323
private ISpeakerRepository _speakerRepo = null!;
24-
private IWordRepository _wordRepo = null!;
24+
private WordRepositoryMock _wordRepo = null!;
2525
private ILiftService _liftService = null!;
2626
private IWordService _wordService = null!;
2727
private LiftController _liftController = null!;
@@ -54,8 +54,8 @@ public void Setup()
5454
var permissionService = new PermissionServiceMock();
5555
_wordService = new WordService(_wordRepo);
5656
var logger = new LoggerMock<LiftController>();
57-
_liftController = new LiftController(_projRepo, semDomRepo, _speakerRepo, _wordRepo, ackService,
58-
_liftService, notifyService, permissionService, logger);
57+
_liftController = new LiftController(_projRepo, semDomRepo, _speakerRepo, _wordRepo, _wordService,
58+
ackService, _liftService, notifyService, permissionService, logger);
5959

6060
_projId = _projRepo.Create(new Project { Name = ProjName }).Result!.Id;
6161
_file = new FormFile(_stream, 0, _stream.Length, "Name", FileName);

Backend.Tests/Controllers/WordControllerTests.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Backend.Tests.Controllers
1414
{
1515
internal sealed class WordControllerTests : IDisposable
1616
{
17-
private IWordRepository _wordRepo = null!;
17+
private WordRepositoryMock _wordRepo = null!;
1818
private IPermissionService _permissionService = null!;
1919
private IWordService _wordService = null!;
2020
private WordController _wordController = null!;
@@ -411,15 +411,13 @@ public async Task TestUpdateWordMissingWord()
411411
[Test]
412412
public async Task TestRestoreWord()
413413
{
414-
var word = await _wordRepo.Create(Util.RandomWord(ProjId));
415-
await _wordRepo.DeleteFrontier(ProjId, word.Id);
414+
var word = await _wordRepo.Add(Util.RandomWord(ProjId));
416415

417416
Assert.That(await _wordRepo.GetAllWords(ProjId), Does.Contain(word).UsingPropertiesComparer());
418417
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Is.Empty);
419418

420-
var result = await _wordController.RestoreWord(ProjId, word.Id) as OkObjectResult;
421-
Assert.That(result, Is.Not.Null);
422-
Assert.That(result.Value, Is.True);
419+
var result = await _wordController.RestoreWord(ProjId, word.Id);
420+
Assert.That(result, Is.InstanceOf<OkResult>());
423421
Assert.That(await _wordRepo.GetAllWords(ProjId), Does.Contain(word).UsingPropertiesComparer());
424422
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
425423
}
@@ -433,9 +431,7 @@ public async Task TestRestoreWordAlreadyInFrontier()
433431
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
434432
var frontierCount = await _wordRepo.GetFrontierCount(ProjId);
435433

436-
var result = await _wordController.RestoreWord(ProjId, word.Id) as OkObjectResult;
437-
Assert.That(result, Is.Not.Null);
438-
Assert.That(result.Value, Is.False);
434+
Assert.ThrowsAsync<ArgumentException>(async () => await _wordController.RestoreWord(ProjId, word.Id));
439435
Assert.That(await _wordRepo.GetFrontierCount(ProjId), Is.EqualTo(frontierCount));
440436
}
441437

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using BackendFramework.Interfaces;
4+
using MongoDB.Driver;
5+
6+
namespace Backend.Tests.Mocks
7+
{
8+
public class MongoDbContextMock : IMongoDbContext
9+
{
10+
public IMongoDatabase Db => throw new NotSupportedException();
11+
12+
public Task<IMongoTransaction> BeginTransaction()
13+
=> Task.FromResult<IMongoTransaction>(new MongoTransactionMock());
14+
15+
public Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
20+
public Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation)
21+
{
22+
throw new NotImplementedException();
23+
}
24+
25+
private sealed class MongoTransactionMock : IMongoTransaction
26+
{
27+
public IClientSessionHandle Session => null!;
28+
29+
public Task CommitTransactionAsync() => Task.CompletedTask;
30+
31+
public Task AbortTransactionAsync() => Task.CompletedTask;
32+
33+
public void Dispose() { }
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)