Skip to content

Commit

Permalink
Implemented missing tests for FinalGameScoreCalculator
Browse files Browse the repository at this point in the history
  • Loading branch information
SparrowBrain committed Nov 11, 2023
1 parent 79a08af commit 3c07de7
Showing 1 changed file with 144 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoFixture.Xunit2;
using Moq;
using PlayNext.HowLongToBeat;
using PlayNext.Model.Data;
using PlayNext.Model.Score.GameScore;
using Playnite.SDK.Models;
Expand All @@ -17,7 +20,7 @@ public class FinalGameScoreCalculatorTests
[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(
public void TwoAttributesBetweenTwoGames(
string topAttribute,
string secondTopAttribute,
float topAttributeWeight,
Expand All @@ -33,8 +36,8 @@ public void TwoAttributes_BetweenTwoGames(
{
// Arrange
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeight(topAttribute, gameScoreCalculationWeights, topAttributeWeight);
SetWeight(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);
SetWeightForAttribute(topAttribute, gameScoreCalculationWeights, topAttributeWeight);
SetWeightForAttribute(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
Expand Down Expand Up @@ -76,7 +79,7 @@ public void SameAttributeBetweenTwoGames(
var topAttributeScores = new Dictionary<Guid, float>() { { topAttributeId, topGameAttributeSum } };
var secondTopAttributeScores = new Dictionary<Guid, float>() { { secondTopAttributeId, secondGameTopAttributeSum } };
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeight(attribute, gameScoreCalculationWeights, attributeWeight);
SetWeightForAttribute(attribute, gameScoreCalculationWeights, attributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
Expand All @@ -94,6 +97,119 @@ public void SameAttributeBetweenTwoGames(
Assert.Equal(secondTopGameScore, result.Skip(1).First().Value);
}

[Theory]
[InlineAutoMoqData(nameof(Game.CriticScore), 100, nameof(Game.TagIds), 0.50, 0.25, 100, 50)]
[InlineAutoMoqData(nameof(Game.CommunityScore), 100, nameof(Game.GenreIds), 0.50, 0.49, 100, 98)]
public void GamePropertyAndAttributeBetweenTwoGames(
string topProperty,
int propertyValue,
string secondTopAttribute,
float topWeight,
float secondTopAttributeWeight,
float topGameScore,
float secondTopGameScore,
Dictionary<Guid, float> attributeScore,
Game[] games,
int releaseYear,
TimeSpan gameLength,
FinalGameScoreCalculator sut)
{
// Arrange
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeightForProperty(topProperty, gameScoreCalculationWeights, topWeight);
SetWeightForAttribute(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
games = new[] { secondTopGame, topGame };
SetProperty(topProperty, topGame, propertyValue);
SetAttributes(secondTopAttribute, secondTopGame, attributeScore.Keys.ToArray());

// 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);
}

[Theory]
[InlineAutoMoqData(nameof(GameScoreWeights.ReleaseYear), nameof(Game.TagIds), 0.50, 0.25, 100, 50)]
public void ReleaseYearAndAttributeBetweenTwoGames(
string topProperty,
string secondTopAttribute,
float topWeight,
float secondTopAttributeWeight,
float topGameScore,
float secondTopGameScore,
Dictionary<Guid, float> attributeScore,
Game[] games,
int releaseYear,
TimeSpan gameLength,
FinalGameScoreCalculator sut)
{
// Arrange
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeightForProperty(topProperty, gameScoreCalculationWeights, topWeight);
SetWeightForAttribute(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
games = new[] { secondTopGame, topGame };
var topGameReleaseDate = new ReleaseDate(releaseYear);
SetProperty(nameof(Game.ReleaseDate), topGame, topGameReleaseDate);
SetAttributes(secondTopAttribute, secondTopGame, attributeScore.Keys.ToArray());

// 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);
}

[Theory]
[InlineAutoMoqData(nameof(GameScoreWeights.GameLength), nameof(Game.DeveloperIds), 0.50, 0.25, 100, 50)]
public void GameLengthAndAttributeBetweenTwoGames(
string topProperty,
string secondTopAttribute,
float topWeight,
float secondTopAttributeWeight,
float topGameScore,
float secondTopGameScore,
[Frozen] Mock<IHowLongToBeatExtension> howLongToBeatExtensionMock,
Dictionary<Guid, float> attributeScore,
Game[] games,
int releaseYear,
TimeSpan preferredGameLength,
FinalGameScoreCalculator sut)
{
// Arrange
var gameScoreCalculationWeights = GetEmptyWeights();
SetWeightForProperty(topProperty, gameScoreCalculationWeights, topWeight);
SetWeightForAttribute(secondTopAttribute, gameScoreCalculationWeights, secondTopAttributeWeight);

var topGame = games.Last();
var secondTopGame = games.First();
games = new[] { secondTopGame, topGame };
SetAttributes(secondTopAttribute, secondTopGame, attributeScore.Keys.ToArray());
howLongToBeatExtensionMock.Setup(x => x.GetTimeToPlay()).Returns(new Dictionary<Guid, int>()
{ { topGame.Id, (int)preferredGameLength.TotalSeconds } });

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

// 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()
{
return new GameScoreWeights
Expand All @@ -114,12 +230,34 @@ private static void SetAttributes(string attributeIdsName, Game game, params Gui
game.PublisherIds = null;
game.TagIds = null;

game.CriticScore = null;
game.CommunityScore = null;

game.GetType().GetProperty(attributeIdsName).SetValue(game, new List<Guid>(attributeIds));
}

private static void SetWeight(string attributeIdsName, GameScoreWeights weights, float value)
private static void SetProperty<T>(string propertyName, Game game, T value)
{
game.GenreIds = null;
game.CategoryIds = null;
game.DeveloperIds = null;
game.PublisherIds = null;
game.TagIds = null;

game.CriticScore = null;
game.CommunityScore = null;

game.GetType().GetProperty(propertyName).SetValue(game, value);
}

private static void SetWeightForAttribute(string attributeIdsName, GameScoreWeights weights, float value)
{
SetWeightForProperty(attributeIdsName.Substring(0, attributeIdsName.Length - 3), weights, value);
}

private static void SetWeightForProperty(string propertyName, GameScoreWeights weights, float value)
{
weights.GetType().GetProperty(attributeIdsName.Substring(0, attributeIdsName.Length - 3)).SetValue(weights, value);
weights.GetType().GetProperty(propertyName).SetValue(weights, value);
}
}
}

0 comments on commit 3c07de7

Please sign in to comment.