Skip to content

Commit

Permalink
Add test for calculations from cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
Syriiin committed Nov 21, 2023
1 parent 138e68c commit 4a41bfd
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Difficalcy.Catch.Tests/CatchCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Difficalcy.Catch.Tests;

public class CatchCalculatorServiceTest : CalculatorServiceTest<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation>
{
protected override CalculatorService<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation> CalculatorService => new CatchCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Catch"));
protected override CalculatorService<CatchScore, CatchDifficulty, CatchPerformance, CatchCalculation> CalculatorService { get; } = new CatchCalculatorService(new TestCache(), new TestBeatmapProvider("osu.Game.Rulesets.Catch"));

[Theory]
[InlineData(4.0505463516206195d, 164.5770866821372d, "diffcalc-test", 0)]
Expand Down
2 changes: 1 addition & 1 deletion Difficalcy.Mania.Tests/ManiaCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Difficalcy.Mania.Tests;

public class ManiaCalculatorServiceTest : CalculatorServiceTest<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation>
{
protected override CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation> CalculatorService => new ManiaCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Mania"));
protected override CalculatorService<ManiaScore, ManiaDifficulty, ManiaPerformance, ManiaCalculation> CalculatorService { get; } = new ManiaCalculatorService(new TestCache(), new TestBeatmapProvider("osu.Game.Rulesets.Mania"));

[Theory]
[InlineData(2.3493769750220914d, 45.71911573894849d, "diffcalc-test", 0)]
Expand Down
2 changes: 1 addition & 1 deletion Difficalcy.Osu.Tests/OsuCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Difficalcy.Osu.Tests;

public class OsuCalculatorServiceTest : CalculatorServiceTest<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation>
{
protected override CalculatorService<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation> CalculatorService => new OsuCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Osu"));
protected override CalculatorService<OsuScore, OsuDifficulty, OsuPerformance, OsuCalculation> CalculatorService { get; } = new OsuCalculatorService(new TestCache(), new TestBeatmapProvider("osu.Game.Rulesets.Osu"));

[Theory]
[InlineData(6.710442985146793d, 288.27290484349686d, "diffcalc-test", 0)]
Expand Down
2 changes: 1 addition & 1 deletion Difficalcy.Taiko.Tests/TaikoCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Difficalcy.Taiko.Tests;

public class TaikoCalculatorServiceTest : CalculatorServiceTest<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation>
{
protected override CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation> CalculatorService => new TaikoCalculatorService(new DummyCache(), new TestBeatmapProvider("osu.Game.Rulesets.Taiko"));
protected override CalculatorService<TaikoScore, TaikoDifficulty, TaikoPerformance, TaikoCalculation> CalculatorService { get; } = new TaikoCalculatorService(new TestCache(), new TestBeatmapProvider("osu.Game.Rulesets.Taiko"));

[Theory]
[InlineData(3.0920212594351191d, 108.45361131093136d, "diffcalc-test", 0)]
Expand Down
5 changes: 5 additions & 0 deletions Difficalcy.Tests/CalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@ public async void TestGetCalculationReturnsCorrectValues(double expectedDifficul

Assert.Equal(expectedDifficultyTotal, calculation.Difficulty.Total, 4);
Assert.Equal(expectedPerformanceTotal, calculation.Performance.Total, 4);

var calculationFromCache = await CalculatorService.GetCalculation(score);

Assert.Equal(calculationFromCache.Difficulty.Total, calculation.Difficulty.Total);
Assert.Equal(calculationFromCache.Performance.Total, calculation.Performance.Total);
}
}
2 changes: 1 addition & 1 deletion Difficalcy.Tests/DummyCalculatorServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Difficalcy.Tests;

public class DummyCalculatorServiceTest : CalculatorServiceTest<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation>
{
protected override CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation> CalculatorService => new DummyCalculatorService(new DummyCache());
protected override CalculatorService<DummyScore, DummyDifficulty, DummyPerformance, DummyCalculation> CalculatorService { get; } = new DummyCalculatorService(new TestCache());

[Theory]
[InlineData(15, 1500, "test 1", 150)]
Expand Down
2 changes: 1 addition & 1 deletion Difficalcy/DifficalcyStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void ConfigureServices(IServiceCollection services)
var redisConfig = Configuration["REDIS_CONFIGURATION"];
ICache cache;
if (redisConfig == null)
cache = new DummyCache();
cache = new TestCache();
else
cache = new RedisCache(ConnectionMultiplexer.Connect(redisConfig));
services.AddSingleton<ICache>(cache);
Expand Down
18 changes: 0 additions & 18 deletions Difficalcy/Services/DummyCache.cs

This file was deleted.

File renamed without changes.
21 changes: 21 additions & 0 deletions Difficalcy/Tests/TestCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Difficalcy.Services
{
public class TestCacheDatabase : ICacheDatabase
{
private readonly Dictionary<string, string> dictionary = [];

public Task<string> GetAsync(string key) => Task.FromResult(dictionary.GetValueOrDefault(key, null));

public void Set(string key, string value) => dictionary.Add(key, value);
}

public class TestCache : ICache
{
private TestCacheDatabase _database = new TestCacheDatabase();

public ICacheDatabase GetDatabase() => _database;
}
}

0 comments on commit 4a41bfd

Please sign in to comment.