Skip to content

Commit

Permalink
Improved FinalGameScoreCalculator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SparrowBrain committed Nov 11, 2023
1 parent dfd3ddb commit 79a08af
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/crowdin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ concurrency: crowdin
on:
push:
branches: [ main ]
workflow_dispatch

jobs:
synchronize-with-crowdin:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,97 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoFixture.Xunit2;
using PlayNext.Model.Data;
using PlayNext.Model.Score.GameScore;
using Playnite.SDK.Models;
using TestTools.Shared;
using Xunit;

namespace PlayNext.UnitTests.Model.Score.GameScore
{
public class FinalGameScoreCalculatorTests
{
[Theory]
[InlineAutoData(nameof(Game.GenreIds))]
[InlineAutoData(nameof(Game.FeatureIds))]
[InlineAutoData(nameof(Game.DeveloperIds))]
[InlineAutoData(nameof(Game.PublisherIds))]
[InlineAutoData(nameof(Game.TagIds))]
public void Calculate(
string attributeIdsName,
[InlineAutoMoqData(nameof(Game.GenreIds), nameof(Game.TagIds), 0.50, 0.25, 100, 50)]
[InlineAutoMoqData(nameof(Game.FeatureIds), nameof(Game.GenreIds), 0.50, 0.49, 100, 98)]
[InlineAutoMoqData(nameof(Game.DeveloperIds), nameof(Game.FeatureIds), 0.2, 0.1, 100, 50)]
[InlineAutoMoqData(nameof(Game.PublisherIds), nameof(Game.DeveloperIds), 0.50, 0.49, 100, 98)]
[InlineAutoMoqData(nameof(Game.TagIds), nameof(Game.PublisherIds), 0.50, 0.49, 100, 98)]
public void TwoAttributes_BetweenTwoGames(
string topAttribute,
string secondTopAttribute,
float topAttributeWeight,
float secondTopAttributeWeight,
float topGameScore,
float secondTopGameScore,
Dictionary<Guid, float> topAttributeScores,
Dictionary<Guid, float> secondTopAttributeScores,
Game[] games,
int releaseYear,
TimeSpan gameLength,
Dictionary<Guid, float> attributeScore,
FinalGameScoreCalculator sut)
{
// Arrange
var gameScoreCalculationWeights = GetEmptyWeights();
gameScoreCalculationWeights.Genre = 1;
SetWeight(topAttribute, gameScoreCalculationWeights, topAttributeWeight);
SetWeight(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);

var topGame = games.Last();
SetAttributes(attributeIdsName, topGame, attributeScore.Keys.ToArray());
SetWeight(attributeIdsName, gameScoreCalculationWeights, 1);
var secondTopGame = games.First();
SetAttributes(topAttribute, topGame, topAttributeScores.Keys.ToArray());
SetAttributes(secondTopAttribute, secondTopGame, secondTopAttributeScores.Keys.ToArray());
var attributeScore = topAttributeScores.Concat(secondTopAttributeScores).ToDictionary(x => x.Key, x => x.Value);

// Act
var result = sut.Calculate(games, attributeScore, gameScoreCalculationWeights, releaseYear, gameLength);

// Assert
Assert.Equal(topGame.Id, result.First().Key);
Assert.Equal(100, result.First().Value);
Assert.Equal(topGameScore, result.First().Value);
Assert.Equal(secondTopGame.Id, result.Skip(1).First().Key);
Assert.Equal(secondTopGameScore, result.Skip(1).First().Value);
}

[Theory]
[InlineAutoMoqData(nameof(Game.GenreIds), 0.50, 100, 50, 100, 50)]
[InlineAutoMoqData(nameof(Game.FeatureIds), 0.1, 5, 1, 100, 20)]
[InlineAutoMoqData(nameof(Game.DeveloperIds), 1, 50, 25, 100, 50)]
[InlineAutoMoqData(nameof(Game.PublisherIds), 0.33, 100, 1, 100, 1)]
[InlineAutoMoqData(nameof(Game.TagIds), 0.2, 50, 49, 100, 98)]
public void SameAttributeBetweenTwoGames(
string attribute,
float attributeWeight,
float topGameAttributeSum,
float secondGameTopAttributeSum,
float topGameScore,
float secondTopGameScore,
Game[] games,
int releaseYear,
TimeSpan gameLength,
Guid topAttributeId,
Guid secondTopAttributeId,
FinalGameScoreCalculator sut)
{
// Arrange
var topAttributeScores = new Dictionary<Guid, float>() { { topAttributeId, topGameAttributeSum } };
var secondTopAttributeScores = new Dictionary<Guid, float>() { { secondTopAttributeId, secondGameTopAttributeSum } };
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeight(attribute, gameScoreCalculationWeights, attributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
SetAttributes(attribute, topGame, topAttributeScores.Keys.ToArray());
SetAttributes(attribute, secondTopGame, secondTopAttributeScores.Keys.ToArray());
var attributeScore = topAttributeScores.Concat(secondTopAttributeScores).ToDictionary(x => x.Key, x => x.Value);

// Act
var result = sut.Calculate(games, attributeScore, gameScoreCalculationWeights, releaseYear, gameLength);

// Assert
Assert.Equal(topGame.Id, result.First().Key);
Assert.Equal(topGameScore, result.First().Value);
Assert.Equal(secondTopGame.Id, result.Skip(1).First().Key);
Assert.Equal(secondTopGameScore, result.Skip(1).First().Value);
}

private GameScoreWeights GetEmptyWeights()
Expand Down
2 changes: 1 addition & 1 deletion PlayNext/HowLongToBeat/HowLongToBeatExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace PlayNext.HowLongToBeat
{
public class HowLongToBeatExtension
public class HowLongToBeatExtension : IHowLongToBeatExtension
{
private static Guid _extensionId = Guid.Parse("e08cd51f-9c9a-4ee3-a094-fde03b55492f");
private readonly ILogger _logger = LogManager.GetLogger(nameof(HowLongToBeatExtension));
Expand Down
14 changes: 14 additions & 0 deletions PlayNext/HowLongToBeat/IHowLongToBeatExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Playnite.SDK.Models;

namespace PlayNext.HowLongToBeat
{
public interface IHowLongToBeatExtension
{
bool DoesDataExist();
Task ParseFiles(IEnumerable<Game> games);
Dictionary<Guid, int> GetTimeToPlay();
}
}
4 changes: 2 additions & 2 deletions PlayNext/Model/Score/GameScore/FinalGameScoreCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace PlayNext.Model.Score.GameScore
{
public class FinalGameScoreCalculator
{
private readonly HowLongToBeatExtension _howLongToBeatExtension;
private readonly IHowLongToBeatExtension _howLongToBeatExtension;
private readonly GameScoreByAttributeCalculator _gameScoreByAttributeCalculator;
private readonly CriticScoreCalculator _criticScoreCalculator;
private readonly CommunityScoreCalculator _communityScoreCalculator;
Expand All @@ -19,7 +19,7 @@ public class FinalGameScoreCalculator
private readonly ScoreNormalizer _scoreNormalizer;
private readonly Summator _summator;

public FinalGameScoreCalculator(HowLongToBeatExtension howLongToBeatExtension,
public FinalGameScoreCalculator(IHowLongToBeatExtension howLongToBeatExtension,
GameScoreByAttributeCalculator gameScoreByAttributeCalculator,
CriticScoreCalculator criticScoreCalculator,
CommunityScoreCalculator communityScoreCalculator,
Expand Down
1 change: 1 addition & 0 deletions PlayNext/PlayNext.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Compile Include="HowLongToBeat\Data\HltbFile.cs" />
<Compile Include="HowLongToBeat\Data\HltbItem.cs" />
<Compile Include="HowLongToBeat\HowLongToBeatExtension.cs" />
<Compile Include="HowLongToBeat\IHowLongToBeatExtension.cs" />
<Compile Include="Model\Filters\UnplayedFilter.cs" />
<Compile Include="Model\Score\Attribute\GameLengthCalculator.cs" />
<Compile Include="Settings\Old\SettingsV2.cs" />
Expand Down

0 comments on commit 79a08af

Please sign in to comment.