Skip to content

Commit

Permalink
add service and controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho committed Jun 30, 2024
1 parent f76cafb commit e22f0d0
Show file tree
Hide file tree
Showing 30 changed files with 199 additions and 49 deletions.
11 changes: 9 additions & 2 deletions EvoSC.sln
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapListModule.Tests", "test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRecordsModule", "src/Modules/LocalRecordsModule/LocalRecordsModule.csproj", "{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LocalRecordsModule.Tests", "tests\Modules\LocalRecordsModule.Tests\LocalRecordsModule.Tests.csproj", "{7401429B-B842-4316-B7A2-B77E9AD966CB}"
EndProject



Expand Down Expand Up @@ -337,6 +339,10 @@ Global
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449}.Release|Any CPU.Build.0 = Release|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7401429B-B842-4316-B7A2-B77E9AD966CB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -389,6 +395,7 @@ Global
{2D2D3AC6-C8BD-4615-BCC7-9A64007DB762} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{AB316816-F301-4693-86E8-C31E78F53A59} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{098D1F9B-054D-4158-BB6C-AC908C4595F6} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
EndGlobalSection
{1D8DBFC8-EC21-4DDA-9D88-DE7CA04C8449} = {DC47658A-F421-4BA4-B617-090A7DFB3900}
{7401429B-B842-4316-B7A2-B77E9AD966CB} = {6D75D6A2-6ECD-4DE4-96C5-CAD7D134407A}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion src/EvoSC.Manialinks/Interfaces/IManialinkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ public interface IManialinkManager : IManialinkOperations
public Task<string> PrepareAndRenderAsync(string name, dynamic data);
public string GetEffectiveName(string name);

public ManialinkTransaction CreateTransaction();
public IManialinkTransaction CreateTransaction();
}
2 changes: 1 addition & 1 deletion src/EvoSC.Manialinks/Interfaces/IManialinkTransaction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace EvoSC.Manialinks.Interfaces;

public interface IManialinkTransaction
public interface IManialinkTransaction : IManialinkOperations
{
public Task CommitAsync();
}
2 changes: 1 addition & 1 deletion src/EvoSC.Manialinks/ManialinkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,5 +485,5 @@ public string GetEffectiveName(string name) =>
? effectiveName
: name;

public ManialinkTransaction CreateTransaction() => new(this, _server);
public IManialinkTransaction CreateTransaction() => new ManialinkTransaction(this, _server);
}
2 changes: 1 addition & 1 deletion src/EvoSC.Manialinks/ManialinkTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace EvoSC.Manialinks;

public class ManialinkTransaction(IManialinkManager manialinkManager, IServerClient server) : IManialinkOperations, IManialinkTransaction
public class ManialinkTransaction(IManialinkManager manialinkManager, IServerClient server) : IManialinkTransaction
{
private readonly MultiCall _serverCalls = new MultiCall();
private readonly HashSet<string> _persistentRemovals = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Services;

public interface ILocalRecordsService
{
public Task<ILocalRecord[]> GetLocalsOfCurrentMapFromPosAsync();
public Task<ILocalRecord[]> GetLocalsOfCurrentMapAsync();
public Task ShowWidgetAsync(IPlayer player);
public Task ShowWidgetToAllAsync();
public Task UpdatePbAsync(IPlayerRecord record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class LocalRecordsService(
{
private const string WidgetName = "LocalRecordsModule.LocalRecordsWidget";

public async Task<ILocalRecord[]> GetLocalsOfCurrentMapFromPosAsync()
public async Task<ILocalRecord[]> GetLocalsOfCurrentMapAsync()
{
var currentMap = await mapService.GetCurrentMapAsync();

Expand All @@ -45,15 +45,15 @@ public async Task<ILocalRecord[]> GetLocalsOfCurrentMapFromPosAsync()

public async Task ShowWidgetAsync(IPlayer player)
{
var records = await GetLocalsOfCurrentMapFromPosAsync();
var records = await GetLocalsOfCurrentMapAsync();
var playerRecords = GetRecordsWithPlayer(player, records);
await manialinkManager.SendManialinkAsync(player, WidgetName,
new { currentPlayer = player, records = playerRecords });
}

public async Task ShowWidgetToAllAsync()
{
var records = await GetLocalsOfCurrentMapFromPosAsync();
var records = await GetLocalsOfCurrentMapAsync();
var onlinePlayers = await playerManagerService.GetOnlinePlayersAsync();
var transaction = manialinkManager.CreateTransaction();

Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.CLI.Tests/EvoSC.CLI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.Commands.Tests/EvoSC.Commands.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.Common.Tests/EvoSC.Common.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<PackageReference Include="linq2db" Version="5.3.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.Manialinks.Tests/EvoSC.Manialinks.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.Testing.Tests/EvoSC.Testing.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/EvoSC.Tests/EvoSC.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Flurl" Version="3.0.6" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -19,7 +19,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/Modules/ASayModule.Tests/ASayModule.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 2 additions & 2 deletions tests/Modules/FastestCp.Tests/FastestCp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.15.0.81779">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using EvoSC.Common.Interfaces.Models;
using EvoSC.Manialinks.Interfaces;
using EvoSC.Modules.Official.LocalRecordsModule.Controllers;
using EvoSC.Testing.Controllers;
using Moq;

namespace LocalRecordsModule.Tests.Controllers;

public class CommandsControllerTests : CommandInteractionControllerTestBase<CommandsController>
{
private Mock<IOnlinePlayer> _actor = new();
private Mock<IManialinkManager> _manialinkManager = new();

public CommandsControllerTests()
{
InitMock(_actor.Object, _manialinkManager);
}

[Fact]
public async Task Reset_Locals_Is_Confirmed()
{
await Controller.ResetLocalsAsync();

_manialinkManager.Verify(m => m.SendManialinkAsync(_actor.Object, "LocalRecordsModule.Dialogs.ConfirmResetDialog"));
AuditEventBuilder.Verify(m => m.Cancel());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using EvoSC.Common.Interfaces;
using EvoSC.Common.Interfaces.Models;
using EvoSC.Manialinks.Interfaces.Models;
using EvoSC.Modules.Official.LocalRecordsModule.Controllers;
using EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Services;
using EvoSC.Testing;
using EvoSC.Testing.Controllers;
using GbxRemoteNet.Interfaces;
using Moq;

namespace LocalRecordsModule.Tests.Controllers;

public class LocalRecordsManialinkControllerTests : ManialinkControllerTestBase<LocalRecordsManialinkController>
{
private readonly Mock<IManialinkActionContext> _manialinkActionContext = new();
private readonly Mock<IOnlinePlayer> _actor = new();
private readonly Mock<ILocalRecordsService> _localRecordsService = new();
private readonly (Mock<IServerClient> Server, Mock<IGbxRemoteClient> Remote)
_server = Mocking.NewServerClientMock();

public LocalRecordsManialinkControllerTests()
{
InitMock(_actor.Object, _manialinkActionContext.Object, _localRecordsService, _server.Server);
}

[Fact]
public async Task Reset_Records_Confirmation_Resets_All_Records()
{
await Controller.ConfirmResetAsync(true);

ManialinkManager.Verify(m => m.HideManialinkAsync("LocalRecordsModule.Dialogs.ConfirmResetDialog"));
_localRecordsService.Verify(m => m.ResetLocalRecordsAsync());
AuditEventBuilder.Verify(m => m.Success());
}

[Fact]
public async Task Reset_Records_Cancel_Does_Not_Resets_All_Records()
{
await Controller.ConfirmResetAsync(false);

ManialinkManager.Verify(m => m.HideManialinkAsync("LocalRecordsModule.Dialogs.ConfirmResetDialog"));
_localRecordsService.Verify(m => m.ResetLocalRecordsAsync(), Times.Never);
AuditEventBuilder.Verify(m => m.Success(), Times.Never);
AuditEventBuilder.Verify(m => m.Error(), Times.Never);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using EvoSC.Common.Interfaces.Models;
using EvoSC.Common.Interfaces.Models.Enums;
using EvoSC.Common.Interfaces.Services;
using EvoSC.Common.Models.Maps;
using EvoSC.Common.Models.Players;
using EvoSC.Modules.Official.LocalRecordsModule.Controllers;
using EvoSC.Modules.Official.LocalRecordsModule.Interfaces.Services;
using EvoSC.Modules.Official.PlayerRecords.Database.Models;
using EvoSC.Modules.Official.PlayerRecords.Events;
using EvoSC.Testing.Controllers;
using GbxRemoteNet.Events;
using Moq;

namespace LocalRecordsModule.Tests.Controllers;

public class WidgetUpdateControllerTests : EventControllerTestBase<WidgetUpdateController>
{
private readonly Mock<ILocalRecordsService> _localRecordsService = new();
private readonly Mock<IPlayerManagerService> _playerManagerService = new();

public WidgetUpdateControllerTests()
{
InitMock(_localRecordsService, _playerManagerService);
}

[Fact]
public async Task Widget_Shown_For_Player_On_Join()
{
var player = new OnlinePlayer
{
Id = 1,
AccountId = "a467a996-eba5-44bf-9e2b-8543b50f39ae",
NickName = "snixtho",
UbisoftName = "snixtho",
State = PlayerState.Playing
};

_playerManagerService.Setup(m => m.GetOnlinePlayerAsync("a467a996-eba5-44bf-9e2b-8543b50f39ae"))
.ReturnsAsync(player);

await Controller.OnPlayerConnectAsync(null, new PlayerConnectGbxEventArgs { Login = "pGepluulRL-eK4VDtQ85rg" });

_localRecordsService.Verify(m => m.ShowWidgetAsync(player));
}

[Fact]
public async Task Widget_Is_Updated_When_New_Map_Starts()
{
await Controller.OnBeginMapAsync(null, new MapGbxEventArgs());

_localRecordsService.Verify(m => m.ShowWidgetToAllAsync());
}

[Fact]
public async Task Local_Records_Are_Updated_When_Player_Gets_Pb()
{
var record = new DbPlayerRecord();
var player = new Player();
var map = new Map();

var args = new PbRecordUpdateEventArgs
{
Player = player, Map = map, Record = record, Status = RecordUpdateStatus.New
};

await Controller.OnPbAsync(null, args);

_localRecordsService.Verify(m => m.UpdatePbAsync(record));
}
}
4 changes: 2 additions & 2 deletions tests/Modules/MapListModule.Tests/MapListModule.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.6.3"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.6.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading

0 comments on commit e22f0d0

Please sign in to comment.