Rewrite gameserver slot filter system (#763)

* Initial implementation of new slot sorting and filtering system

* Initial implementation of filtering for lbp3 community tab

* Add support for organization on lbp3

* Add playlist and user categories

* Implement unit tests for all filters
Refactor more systems to use PaginationData

* Fix PlayerCountFilter test

* Add more unit tests and integration tests for the filter system

* Fix LBP2 move filter and gameFilterType

* Fix sort by likes in LBP3 category

* Add sort for total plays

* Remove extra whitespace and make styling more consistent

* Order hearted and queued levels by primary key ID

* Fix query without order warnings
This commit is contained in:
Josh 2023-05-31 16:33:39 -05:00 committed by GitHub
commit 0c1e350fa3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
106 changed files with 4040 additions and 1183 deletions

View file

@ -8,7 +8,6 @@ namespace LBPUnion.ProjectLighthouse.Tests.Helpers;
public static class IntegrationHelper
{
private static readonly Lazy<bool> dbConnected = new(IsDbConnected);
private static bool IsDbConnected() => ServerStatics.DbConnected;

View file

@ -14,6 +14,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.EntityFrameworkCore;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Helpers;
@ -38,6 +39,18 @@ public static class MockHelper
UserToken = "unittest",
};
public static T2 CastTo<T1, T2>(this IActionResult result) where T1 : ObjectResult
{
Assert.IsType<T1>(result);
T1? typedResult = result as T1;
Assert.NotNull(typedResult);
Assert.NotNull(typedResult.Value);
Assert.IsType<T2?>(typedResult.Value);
T2? finalResult = (T2?)typedResult.Value;
Assert.NotNull(finalResult);
return finalResult;
}
public static async Task<DatabaseContext> GetTestDatabase(IEnumerable<IList> sets, [CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0)
{
Dictionary<Type, IList> setDict = new();
@ -109,9 +122,14 @@ public static class MockHelper
}
public static void SetupTestController(this ControllerBase controllerBase, string? body = null)
{
SetupTestController(controllerBase, GetUnitTestToken(), body);
}
public static void SetupTestController(this ControllerBase controllerBase, GameTokenEntity token, string? body = null)
{
controllerBase.ControllerContext = GetMockControllerContext(body);
SetupTestGameToken(controllerBase, GetUnitTestToken());
SetupTestGameToken(controllerBase, token);
}
public static ControllerContext GetMockControllerContext() =>

View file

@ -88,6 +88,8 @@ public class LighthouseServerTest<TStartup> where TStartup : class
private Task<HttpResponseMessage> AuthenticatedRequest(string endpoint, string mmAuth, HttpMethod method)
{
if (!endpoint.StartsWith("/")) endpoint = $"/{endpoint}";
using HttpRequestMessage requestMessage = new(method, endpoint);
requestMessage.Headers.Add("Cookie", mmAuth);
string path = endpoint.Split("?", StringSplitOptions.RemoveEmptyEntries)[0];

View file

@ -0,0 +1,841 @@
using System;
using System.Collections.Generic;
using LBPUnion.ProjectLighthouse.Filter;
using LBPUnion.ProjectLighthouse.Filter.Filters;
using LBPUnion.ProjectLighthouse.Types.Entities.Level;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Levels;
using LBPUnion.ProjectLighthouse.Types.Users;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class FilterTests
{
[Fact]
public void QueryBuilder_DoesDeepClone()
{
SlotQueryBuilder queryBuilder = new();
queryBuilder.AddFilter(new CrossControlFilter());
SlotQueryBuilder clonedBuilder = queryBuilder.Clone();
Assert.NotEqual(queryBuilder, clonedBuilder);
}
[Fact]
public void AdventureFilter_ShouldAccept_WhenAdventure()
{
AdventureFilter adventureFilter = new();
Func<SlotEntity, bool> adventureFunc = adventureFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
IsAdventurePlanet = true,
};
Assert.True(adventureFunc(slot));
}
[Fact]
public void AdventureFilter_ShouldReject_WhenNotAdventure()
{
AdventureFilter adventureFilter = new();
Func<SlotEntity, bool> adventureFunc = adventureFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
IsAdventurePlanet = false,
};
Assert.False(adventureFunc(slot));
}
[Fact]
public void AuthorLabelFilter_ShouldAccept_WhenExactMatch()
{
string[] filters =
{
"LABEL_Test", "LABEL_Unit",
};
AuthorLabelFilter labelFilter = new(filters);
Func<SlotEntity, bool> labelFunc = labelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
AuthorLabels = "LABEL_Test,LABEL_Unit",
};
Assert.True(labelFunc(slot));
}
[Fact]
public void AuthorLabelFilter_ShouldAccept_WhenExactMatch_AndExtraLabelsPresent()
{
string[] filters =
{
"LABEL_Test", "LABEL_Unit",
};
AuthorLabelFilter labelFilter = new(filters);
Func<SlotEntity, bool> labelFunc = labelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
AuthorLabels = "LABEL_Test,LABEL_Unit,LABEL_Lighthouse,LABEL_Bruh",
};
Assert.True(labelFunc(slot));
}
[Fact]
public void AuthorLabelFilter_ShouldAccept_WhenFilterEmpty_AndLabelsEmpty()
{
string[] filters = Array.Empty<string>();
AuthorLabelFilter labelFilter = new(filters);
Func<SlotEntity, bool> labelFunc = labelFilter.GetPredicate().Compile();
SlotEntity slotWithNoLabels = new()
{
AuthorLabels = "",
};
Assert.True(labelFunc(slotWithNoLabels));
}
[Fact]
public void AuthorLabelFilter_ShouldReject_WhenNoneMatch()
{
string[] filters =
{
"LABEL_Test", "LABEL_Unit",
};
AuthorLabelFilter labelFilter = new(filters);
Func<SlotEntity, bool> labelFunc = labelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
AuthorLabels = "LABEL_Adventure,LABEL_Versus",
};
Assert.False(labelFunc(slot));
}
[Fact]
public void CreatorFilter_ShouldAccept_WhenCreatorIdMatch()
{
const int creatorId = 27;
CreatorFilter creatorFilter = new(creatorId);
Func<SlotEntity, bool> creatorFunc = creatorFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = creatorId,
};
Assert.True(creatorFunc(slot));
}
[Fact]
public void CreatorFilter_ShouldReject_WhenCreatorIdMismatch()
{
const int filterCreatorId = 27;
const int slotCreatorId = 28;
CreatorFilter creatorFilter = new(filterCreatorId);
Func<SlotEntity, bool> creatorFunc = creatorFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = slotCreatorId,
};
Assert.False(creatorFunc(slot));
}
[Fact]
public void CrossControlFilter_ShouldAccept_WhenCrossControlRequired()
{
CrossControlFilter crossControlFilter = new();
Func<SlotEntity, bool> ccFunc = crossControlFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CrossControllerRequired = true,
};
Assert.True(ccFunc(slot));
}
[Fact]
public void CrossControlFilter_ShouldReject_WhenCrossControlNotRequired()
{
CrossControlFilter crossControlFilter = new();
Func<SlotEntity, bool> ccFunc = crossControlFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CrossControllerRequired = false,
};
Assert.False(ccFunc(slot));
}
[Fact]
public void ExcludeAdventureFilter_ShouldReject_WhenAdventure()
{
ExcludeAdventureFilter excludeAdventureFilter = new();
Func<SlotEntity, bool> adventureFunc = excludeAdventureFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
IsAdventurePlanet = true,
};
Assert.False(adventureFunc(slot));
}
[Fact]
public void ExcludeAdventureFilter_ShouldAccept_WhenNotAdventure()
{
ExcludeAdventureFilter excludeAdventureFilter = new();
Func<SlotEntity, bool> adventureFunc = excludeAdventureFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
IsAdventurePlanet = false,
};
Assert.True(adventureFunc(slot));
}
[Fact]
public void ExcludeLBP1OnlyFilter_ShouldReject_WhenLbp1Only_AndTokenNotLbp1_AndNotCreator()
{
ExcludeLBP1OnlyFilter excludeLBP1 = new(10, GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> excludeFunc = excludeLBP1.GetPredicate().Compile();
SlotEntity slot = new()
{
Lbp1Only = true,
};
Assert.False(excludeFunc(slot));
}
[Fact]
public void ExcludeLBP1OnlyFilter_ShouldAccept_WhenLbp1Only_AndTokenLbp1()
{
ExcludeLBP1OnlyFilter excludeLBP1 = new(10, GameVersion.LittleBigPlanet1);
Func<SlotEntity, bool> excludeFunc = excludeLBP1.GetPredicate().Compile();
SlotEntity slot = new()
{
Lbp1Only = true,
};
Assert.True(excludeFunc(slot));
}
[Fact]
public void ExcludeLBP1OnlyFilter_ShouldAccept_WhenLbp1Only_AndTokenNotLbp1_AndIsCreator()
{
ExcludeLBP1OnlyFilter excludeLBP1 = new(10, GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> excludeFunc = excludeLBP1.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = 10,
Lbp1Only = true,
};
Assert.True(excludeFunc(slot));
}
[Fact]
public void ExcludeMovePackFilter_ShouldReject_WhenMoveRequired()
{
ExcludeMovePackFilter excludeMove = new();
Func<SlotEntity, bool> excludeFunc = excludeMove.GetPredicate().Compile();
SlotEntity slot = new()
{
MoveRequired = true,
};
Assert.False(excludeFunc(slot));
}
[Fact]
public void ExcludeMovePackFilter_ShouldAccept_WhenMoveNotRequired()
{
ExcludeMovePackFilter excludeMove = new();
Func<SlotEntity, bool> excludeFunc = excludeMove.GetPredicate().Compile();
SlotEntity slot = new()
{
MoveRequired = false,
};
Assert.True(excludeFunc(slot));
}
[Fact]
public void FirstUploadedFilter_ShouldReject_WhenOlderThanStartTime()
{
FirstUploadedFilter uploadFilter = new(1000);
Func<SlotEntity, bool> uploadFunc = uploadFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
FirstUploaded = 999,
};
Assert.False(uploadFunc(slot));
}
[Fact]
public void FirstUploadedFilter_ShouldAccept_WhenNewerThanStartTime()
{
FirstUploadedFilter uploadFilter = new(1000);
Func<SlotEntity, bool> uploadFunc = uploadFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
FirstUploaded = 1001,
};
Assert.True(uploadFunc(slot));
}
[Fact]
public void FirstUploadedFilter_ShouldReject_WhenOlderThanEndTime()
{
FirstUploadedFilter uploadFilter = new(0, 1000);
Func<SlotEntity, bool> uploadFunc = uploadFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
FirstUploaded = 1001,
};
Assert.False(uploadFunc(slot));
}
[Fact]
public void FirstUploadedFilter_ShouldAccept_WhenNewerThanEndTime()
{
FirstUploadedFilter uploadFilter = new(0, 1000);
Func<SlotEntity, bool> uploadFunc = uploadFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
FirstUploaded = 999,
};
Assert.True(uploadFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldAccept_WhenExact_AndEqual()
{
GameVersionFilter gameVersionFilter = new(GameVersion.LittleBigPlanet1, true);
Func<SlotEntity, bool> versionFunc = gameVersionFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet1,
};
Assert.True(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldReject_WhenExact_AndNotEqual()
{
GameVersionFilter gameVersionFilter = new(GameVersion.LittleBigPlanet2, true);
Func<SlotEntity, bool> versionFunc = gameVersionFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet1,
};
Assert.False(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldAccept_WhenNotExact_AndGreaterThan()
{
GameVersionFilter gameVersionFilter = new(GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> versionFunc = gameVersionFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet1,
};
Assert.True(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldAccept_WhenNotExact_AndEqual()
{
GameVersionFilter gameVersionFilter = new(GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> versionFunc = gameVersionFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet2,
};
Assert.True(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldReject_WhenNotExact_AndLessThan()
{
GameVersionFilter gameVersionFilter = new(GameVersion.LittleBigPlanet1);
Func<SlotEntity, bool> versionFunc = gameVersionFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet2,
};
Assert.False(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldReject_WhenVersionNotInList()
{
GameVersionListFilter gameVersionListFilter = new(GameVersion.LittleBigPlanet1, GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> versionFunc = gameVersionListFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet3,
};
Assert.False(versionFunc(slot));
}
[Fact]
public void GameVersionFilter_ShouldAccept_WhenVersionIsInList()
{
GameVersionListFilter gameVersionListFilter = new(GameVersion.LittleBigPlanet1, GameVersion.LittleBigPlanet2);
Func<SlotEntity, bool> versionFunc = gameVersionListFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
GameVersion = GameVersion.LittleBigPlanet1,
};
Assert.True(versionFunc(slot));
}
[Fact]
public void HiddenSlotFilter_ShouldReject_WhenHidden()
{
HiddenSlotFilter hiddenSlotFilter = new();
Func<SlotEntity, bool> hiddenFunc = hiddenSlotFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Hidden = true,
};
Assert.False(hiddenFunc(slot));
}
[Fact]
public void HiddenSlotFilter_ShouldAccept_WhenNotHidden()
{
HiddenSlotFilter hiddenSlotFilter = new();
Func<SlotEntity, bool> hiddenFunc = hiddenSlotFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Hidden = false,
};
Assert.True(hiddenFunc(slot));
}
[Fact]
public void MoveFilter_ShouldAccept_WhenMoveRequired()
{
MovePackFilter movePackFilter = new();
Func<SlotEntity, bool> moveFunc = movePackFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MoveRequired = true,
};
Assert.True(moveFunc(slot));
}
[Fact]
public void MoveFilter_ShouldReject_WhenMoveNotRequired()
{
MovePackFilter movePackFilter = new();
Func<SlotEntity, bool> moveFunc = movePackFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MoveRequired = false,
};
Assert.False(moveFunc(slot));
}
[Fact]
public void PlayerCountFilter_ShouldReject_WhenHigherThanMaxPlayers()
{
PlayerCountFilter playerCountFilter = new(maxPlayers: 2);
Func<SlotEntity, bool> countFunc = playerCountFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MinimumPlayers = 1,
MaximumPlayers = 4,
};
Assert.False(countFunc(slot));
}
[Fact]
public void PlayerCountFilter_ShouldReject_WhenLowerThanMinPlayers()
{
PlayerCountFilter playerCountFilter = new(minPlayers: 2);
Func<SlotEntity, bool> countFunc = playerCountFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MinimumPlayers = 1,
MaximumPlayers = 4,
};
Assert.False(countFunc(slot));
}
[Fact]
public void PlayerCountFilter_ShouldAccept_WhenLowerThanMaxPlayers()
{
PlayerCountFilter playerCountFilter = new(maxPlayers: 3);
Func<SlotEntity, bool> countFunc = playerCountFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MinimumPlayers = 1,
MaximumPlayers = 2,
};
Assert.True(countFunc(slot));
}
[Fact]
public void PlayerCountFilter_ShouldAccept_WhenHigherThanMinPlayers()
{
PlayerCountFilter playerCountFilter = new(minPlayers: 2);
Func<SlotEntity, bool> countFunc = playerCountFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
MinimumPlayers = 3,
MaximumPlayers = 4,
};
Assert.True(countFunc(slot));
}
[Fact]
public void ResultTypeFilter_ShouldReject_WhenSlotNotPresent()
{
ResultTypeFilter resultFilter = new();
Func<SlotEntity, bool> resultFunc = resultFilter.GetPredicate().Compile();
SlotEntity slot = new();
Assert.False(resultFunc(slot));
}
[Fact]
public void ResultTypeFilter_ShouldAccept_WhenSlotPresent()
{
ResultTypeFilter resultFilter = new("slot");
Func<SlotEntity, bool> resultFunc = resultFilter.GetPredicate().Compile();
SlotEntity slot = new();
Assert.True(resultFunc(slot));
}
[Fact]
public void SlotIdFilter_ShouldReject_WhenSlotIdNotPresent()
{
SlotIdFilter idFilter = new(new List<int>
{
2,
});
Func<SlotEntity, bool> idFunc = idFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
SlotId = 1,
};
Assert.False(idFunc(slot));
}
[Fact]
public void SlotIdFilter_ShouldAccept_WhenSlotIdPresent()
{
SlotIdFilter idFilter = new(new List<int>
{
2,
});
Func<SlotEntity, bool> idFunc = idFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
SlotId = 2,
};
Assert.True(idFunc(slot));
}
[Fact]
public void SlotTypeFilter_ShouldAccept_WhenSlotTypeMatches()
{
SlotTypeFilter slotTypeFilter = new(SlotType.User);
Func<SlotEntity, bool> typeFunc = slotTypeFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Type = SlotType.User,
};
Assert.True(typeFunc(slot));
}
[Fact]
public void SlotTypeFilter_ShouldAccept_WhenSlotTypeDoesNotMatch()
{
SlotTypeFilter slotTypeFilter = new(SlotType.User);
Func<SlotEntity, bool> typeFunc = slotTypeFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Type = SlotType.Developer,
};
Assert.False(typeFunc(slot));
}
[Fact]
public void SubLevelFilter_ShouldAccept_WhenUserIsCreator_AndNotSubLevel()
{
SubLevelFilter subLevelFilter = new(2);
Func<SlotEntity, bool> subLevelFunc = subLevelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = 2,
SubLevel = false,
};
Assert.True(subLevelFunc(slot));
}
[Fact]
public void SubLevelFilter_ShouldAccept_WhenUserIsCreator_AndSubLevel()
{
SubLevelFilter subLevelFilter = new(2);
Func<SlotEntity, bool> subLevelFunc = subLevelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = 2,
SubLevel = true,
};
Assert.True(subLevelFunc(slot));
}
[Fact]
public void SubLevelFilter_ShouldReject_WhenUserIsNotCreator_AndSubLevel()
{
SubLevelFilter subLevelFilter = new(2);
Func<SlotEntity, bool> subLevelFunc = subLevelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = 1,
SubLevel = true,
};
Assert.False(subLevelFunc(slot));
}
[Fact]
public void SubLevelFilter_ShouldAccept_WhenUserIsNotCreator_AndNotSubLevel()
{
SubLevelFilter subLevelFilter = new(2);
Func<SlotEntity, bool> subLevelFunc = subLevelFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
CreatorId = 1,
SubLevel = false,
};
Assert.True(subLevelFunc(slot));
}
[Fact]
public void TeamPickFilter_ShouldAccept_WhenTeamPick()
{
TeamPickFilter teamPickFilter = new();
Func<SlotEntity, bool> teamPickFunc = teamPickFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
TeamPick = true,
};
Assert.True(teamPickFunc(slot));
}
[Fact]
public void TeamPickFilter_ShouldReject_WhenNotTeamPick()
{
TeamPickFilter teamPickFilter = new();
Func<SlotEntity, bool> teamPickFunc = teamPickFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
TeamPick = false,
};
Assert.False(teamPickFunc(slot));
}
[Fact]
public void TextFilter_ShouldAccept_WhenDescriptionContainsText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Description = "unit test",
};
Assert.True(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldReject_WhenDescriptionDoesNotContainText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Description = "fraction exam",
};
Assert.False(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldAccept_WhenNameContainsText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Name = "unit test",
};
Assert.True(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldReject_WhenNameDoesNotContainText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Name = "fraction exam",
};
Assert.False(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldAccept_WhenIdContainsText()
{
TextFilter textFilter = new("21");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
SlotId = 21,
};
Assert.True(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldReject_WhenIdDoesNotContainText()
{
TextFilter textFilter = new("21");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
SlotId = 19,
};
Assert.False(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldAccept_WhenCreatorUsernameContainsText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Creator = new UserEntity
{
Username = "test",
},
};
Assert.True(textFunc(slot));
}
[Fact]
public void TextFilter_ShouldReject_WhenCreatorUsernameDoesNotContainText()
{
TextFilter textFilter = new("test");
Func<SlotEntity, bool> textFunc = textFilter.GetPredicate().Compile();
SlotEntity slot = new()
{
Creator = new UserEntity
{
Username = "bruh",
},
};
Assert.False(textFunc(slot));
}
}

View file

@ -0,0 +1,238 @@
using System.Collections.Generic;
using System.Linq;
using LBPUnion.ProjectLighthouse.Configuration;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Types.Filter;
using LBPUnion.ProjectLighthouse.Types.Serialization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using Xunit;
namespace LBPUnion.ProjectLighthouse.Tests.Unit;
[Trait("Category", "Unit")]
public class PaginationTests
{
[Fact]
public void GetPaginationData_IsReadFromQuery()
{
DefaultHttpContext defaultHttpContext = new()
{
Request =
{
Query = new QueryCollection(new Dictionary<string, StringValues>
{
{
"pageStart", new StringValues("10")
},
{
"pageSize", new StringValues("15")
},
}),
},
};
PaginationData pageData = defaultHttpContext.Request.GetPaginationData();
const int expectedPageStart = 10;
const int expectedPageSize = 15;
Assert.Equal(expectedPageStart, pageData.PageStart);
Assert.Equal(expectedPageSize, pageData.PageSize);
}
[Fact]
public void GetPaginationData_IsPageStartSetToDefault_WhenMissing()
{
DefaultHttpContext defaultHttpContext = new()
{
Request =
{
Query = new QueryCollection(new Dictionary<string, StringValues>
{
{
"pageSize", new StringValues("15")
},
}),
},
};
PaginationData pageData = defaultHttpContext.Request.GetPaginationData();
const int expectedPageStart = 0;
const int expectedPageSize = 15;
Assert.Equal(expectedPageStart, pageData.PageStart);
Assert.Equal(expectedPageSize, pageData.PageSize);
}
[Fact]
public void GetPaginationData_IsPageSizeSetToDefault_WhenMissing()
{
ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots = 50;
DefaultHttpContext defaultHttpContext = new()
{
Request =
{
Query = new QueryCollection(new Dictionary<string, StringValues>
{
{
"pageStart", new StringValues("10")
},
}),
},
};
PaginationData pageData = defaultHttpContext.Request.GetPaginationData();
const int expectedPageStart = 10;
const int expectedPageSize = 50;
Assert.Equal(expectedPageStart, pageData.PageStart);
Assert.Equal(expectedPageSize, pageData.PageSize);
}
[Fact]
public void GetPaginationData_NegativeValuesAreSetToZero()
{
ServerConfiguration.Instance.UserGeneratedContentLimits.EntitledSlots = 50;
DefaultHttpContext defaultHttpContext = new()
{
Request =
{
Query = new QueryCollection(new Dictionary<string, StringValues>
{
{
"pageStart", new StringValues("-10")
},
{
"pageSize", new StringValues("-10")
},
}),
},
};
PaginationData pageData = defaultHttpContext.Request.GetPaginationData();
const int expectedPageStart = 0;
const int expectedPageSize = 10;
Assert.Equal(expectedPageStart, pageData.PageStart);
Assert.Equal(expectedPageSize, pageData.PageSize);
}
[Fact]
public void ApplyPagination_ShouldApplyCorrectPagination()
{
List<GameUser> users = new();
for (int i = 0; i < 30; i++)
{
users.Add(new GameUser
{
UserId = i+1,
});
}
PaginationData pageData = new()
{
PageSize = 5,
PageStart = 6,
};
List<GameUser> pagedUsers = users.AsQueryable().ApplyPagination(pageData).ToList();
Assert.Equal(pageData.PageSize, pagedUsers.Count);
Assert.Equal(6, pagedUsers[0].UserId);
Assert.Equal(10, pagedUsers[4].UserId);
}
[Fact]
public void ApplyPagination_ShouldClampPageStart_WhenNegative()
{
List<GameUser> users = new();
for (int i = 0; i < 30; i++)
{
users.Add(new GameUser
{
UserId = i + 1,
});
}
PaginationData pageData = new()
{
PageSize = 5,
PageStart = -5,
};
List<GameUser> pagedUsers = users.AsQueryable().ApplyPagination(pageData).ToList();
Assert.Equal(pageData.PageSize, pagedUsers.Count);
Assert.Equal(1, pagedUsers[0].UserId);
Assert.Equal(5, pagedUsers[4].UserId);
}
[Fact]
public void ApplyPagination_ShouldReturnEmpty_WhenPageSizeNegative()
{
List<GameUser> users = new();
for (int i = 0; i < 30; i++)
{
users.Add(new GameUser
{
UserId = i + 1,
});
}
PaginationData pageData = new()
{
PageSize = -5,
PageStart = 0,
};
List<GameUser> pagedUsers = users.AsQueryable().ApplyPagination(pageData).ToList();
Assert.Empty(pagedUsers);
}
[Fact]
public void ApplyPagination_ShouldClampPageSize_WhenSizeExceedsMaxElements()
{
List<GameUser> users = new();
for (int i = 0; i < 30; i++)
{
users.Add(new GameUser
{
UserId = i + 1,
});
}
PaginationData pageData = new()
{
PageSize = 10,
PageStart = 0,
MaxElements = 1,
};
List<GameUser> pagedUsers = users.AsQueryable().ApplyPagination(pageData).ToList();
Assert.Single(pagedUsers);
}
[Fact]
public void ApplyPagination_ShouldClampPageSize_WhenSizeExceedsInternalLimit()
{
List<GameUser> users = new();
for (int i = 0; i < 1001; i++)
{
users.Add(new GameUser
{
UserId = i + 1,
});
}
PaginationData pageData = new()
{
PageSize = int.MaxValue,
PageStart = 0,
MaxElements = int.MaxValue,
};
List<GameUser> pagedUsers = users.AsQueryable().ApplyPagination(pageData).ToList();
Assert.Equal(1000, pagedUsers.Count);
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Files;
using LBPUnion.ProjectLighthouse.Types.Resources;
using Xunit;
@ -44,7 +45,7 @@ public class ResourceTests
}
[Fact]
public async void ShouldDeleteResourceAndImage()
public async Task ShouldDeleteResourceAndImage()
{
FileHelper.EnsureDirectoryCreated(FileHelper.ResourcePath);
FileHelper.EnsureDirectoryCreated(FileHelper.ImagePath);