Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Session.vim
scripts/*.js
!scripts/frontendScripts.js
!scripts/jestTest.js
!scripts/setupMongo.js
!scripts/startDatabase.js
database/*.js
*.log
*-debug.log*
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
},
{
"label": "run-mongo",
"command": "mongod",
"command": "npm",
"type": "process",
"args": ["--dbpath", "${workspaceFolder}/mongo_database"],
"args": ["run", "database"],
"problemMatcher": "$tsc"
}
]
Expand Down
3 changes: 3 additions & 0 deletions Backend.Tests/Backend.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<NoWarn>$(NoWarn);CA1305;CA1859;CS1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EphemeralMongo7.runtime.linux-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Linux'))" />
<PackageReference Include="EphemeralMongo7.runtime.osx-arm64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('OSX'))" />
<PackageReference Include="EphemeralMongo7.runtime.win-x64" Version="2.0.0" Condition="$([MSBuild]::IsOSPlatform('Windows'))" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="Moq.Contrib.HttpClient" Version="1.4.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
Expand Down
15 changes: 7 additions & 8 deletions Backend.Tests/Controllers/AudioControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;
using Backend.Tests.Mocks;
using BackendFramework.Controllers;
Expand All @@ -14,7 +14,7 @@ namespace Backend.Tests.Controllers
internal sealed class AudioControllerTests : IDisposable
{
private IProjectRepository _projRepo = null!;
private IWordRepository _wordRepo = null!;
private WordRepositoryMock _wordRepo = null!;
private PermissionServiceMock _permissionService = null!;
private WordService _wordService = null!;
private AudioController _audioController = null!;
Expand Down Expand Up @@ -163,20 +163,19 @@ public void TestDeleteAudioFileInvalidArguments()
[Test]
public void TestDeleteAudioFileNoWordWithAudio()
{
var result = _audioController.DeleteAudioFile(_projId, "not-a-word", _file.FileName).Result;
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
var result1 = _audioController.DeleteAudioFile(_projId, "not-a-word", _file.FileName).Result;
Assert.That(result1, Is.InstanceOf<NotFoundObjectResult>());

var wordId = _wordRepo.Create(Util.RandomWord(_projId)).Result.Id;
result = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
Assert.That(result, Is.InstanceOf<NotFoundObjectResult>());
var result2 = _audioController.DeleteAudioFile(_projId, wordId, _file.FileName).Result;
Assert.That(result2, Is.InstanceOf<NotFoundObjectResult>());
}

[Test]
public void TestDeleteAudioFile()
{
// Refill test database
_wordRepo.DeleteAllWords(_projId).Wait();
_wordRepo.DeleteAllFrontierWords(_projId).Wait();
_wordRepo.DeleteAllWords(_projId);
var origWord = Util.RandomWord(_projId);
const string fileName = "a.wav";
origWord.Audio.Add(new Pronunciation(fileName));
Expand Down
8 changes: 4 additions & 4 deletions Backend.Tests/Controllers/LiftControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
Expand All @@ -21,7 +21,7 @@ internal sealed class LiftControllerTests : IDisposable
{
private IProjectRepository _projRepo = null!;
private ISpeakerRepository _speakerRepo = null!;
private IWordRepository _wordRepo = null!;
private WordRepositoryMock _wordRepo = null!;
private ILiftService _liftService = null!;
private IWordService _wordService = null!;
private LiftController _liftController = null!;
Expand Down Expand Up @@ -54,8 +54,8 @@ public void Setup()
var permissionService = new PermissionServiceMock();
_wordService = new WordService(_wordRepo);
var logger = new LoggerMock<LiftController>();
_liftController = new LiftController(_projRepo, semDomRepo, _speakerRepo, _wordRepo, ackService,
_liftService, notifyService, permissionService, logger);
_liftController = new LiftController(_projRepo, semDomRepo, _speakerRepo, _wordRepo, _wordService,
ackService, _liftService, notifyService, permissionService, logger);

_projId = _projRepo.Create(new Project { Name = ProjName }).Result!.Id;
_file = new FormFile(_stream, 0, _stream.Length, "Name", FileName);
Expand Down
14 changes: 5 additions & 9 deletions Backend.Tests/Controllers/WordControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Backend.Tests.Controllers
{
internal sealed class WordControllerTests : IDisposable
{
private IWordRepository _wordRepo = null!;
private WordRepositoryMock _wordRepo = null!;
private IPermissionService _permissionService = null!;
private IWordService _wordService = null!;
private WordController _wordController = null!;
Expand Down Expand Up @@ -411,15 +411,13 @@ public async Task TestUpdateWordMissingWord()
[Test]
public async Task TestRestoreWord()
{
var word = await _wordRepo.Create(Util.RandomWord(ProjId));
await _wordRepo.DeleteFrontier(ProjId, word.Id);
var word = await _wordRepo.Add(Util.RandomWord(ProjId));

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

var result = await _wordController.RestoreWord(ProjId, word.Id) as OkObjectResult;
Assert.That(result, Is.Not.Null);
Assert.That(result.Value, Is.True);
var result = await _wordController.RestoreWord(ProjId, word.Id);
Assert.That(result, Is.InstanceOf<OkResult>());
Assert.That(await _wordRepo.GetAllWords(ProjId), Does.Contain(word).UsingPropertiesComparer());
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
}
Expand All @@ -433,9 +431,7 @@ public async Task TestRestoreWordAlreadyInFrontier()
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
var frontierCount = await _wordRepo.GetFrontierCount(ProjId);

var result = await _wordController.RestoreWord(ProjId, word.Id) as OkObjectResult;
Assert.That(result, Is.Not.Null);
Assert.That(result.Value, Is.False);
Assert.ThrowsAsync<ArgumentException>(async () => await _wordController.RestoreWord(ProjId, word.Id));
Assert.That(await _wordRepo.GetFrontierCount(ProjId), Is.EqualTo(frontierCount));
}

Expand Down
36 changes: 36 additions & 0 deletions Backend.Tests/Mocks/MongoDbContextMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using MongoDB.Driver;

namespace Backend.Tests.Mocks
{
public class MongoDbContextMock : IMongoDbContext
{
public IMongoDatabase Db => throw new NotSupportedException();

public Task<IMongoTransaction> BeginTransaction()
=> Task.FromResult<IMongoTransaction>(new MongoTransactionMock());

public Task<T> ExecuteInTransaction<T>(Func<IClientSessionHandle, Task<T>> operation)
{
throw new NotImplementedException();
}

public Task<T?> ExecuteInTransactionAllowNull<T>(Func<IClientSessionHandle, Task<T?>> operation)
{
throw new NotImplementedException();
}

private sealed class MongoTransactionMock : IMongoTransaction
{
public IClientSessionHandle Session => null!;

public Task CommitTransactionAsync() => Task.CompletedTask;

public Task AbortTransactionAsync() => Task.CompletedTask;

public void Dispose() { }
}
}
}
Loading
Loading