mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-06-19 05:51:28 +00:00
* 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
162 lines
No EOL
5 KiB
C#
162 lines
No EOL
5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
|
using LBPUnion.ProjectLighthouse.Types.Users;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Controllers;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Xunit;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Tests.Helpers;
|
|
|
|
public static class MockHelper
|
|
{
|
|
|
|
public static UserEntity GetUnitTestUser() =>
|
|
new()
|
|
{
|
|
Username = "unittest",
|
|
UserId = 1,
|
|
};
|
|
|
|
public static GameTokenEntity GetUnitTestToken() =>
|
|
new()
|
|
{
|
|
Platform = Platform.UnitTest,
|
|
UserId = 1,
|
|
ExpiresAt = DateTime.MaxValue,
|
|
TokenId = 1,
|
|
UserLocation = "127.0.0.1",
|
|
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();
|
|
foreach (IList list in sets)
|
|
{
|
|
Type? type = list.GetType().GetGenericArguments().ElementAtOrDefault(0);
|
|
if (type == null) continue;
|
|
setDict[type] = list;
|
|
}
|
|
|
|
if (!setDict.TryGetValue(typeof(GameTokenEntity), out _))
|
|
{
|
|
setDict[typeof(GameTokenEntity)] = new List<GameTokenEntity>
|
|
{
|
|
GetUnitTestToken(),
|
|
};
|
|
}
|
|
|
|
if (!setDict.TryGetValue(typeof(UserEntity), out _))
|
|
{
|
|
setDict[typeof(UserEntity)] = new List<UserEntity>
|
|
{
|
|
GetUnitTestUser(),
|
|
};
|
|
}
|
|
|
|
|
|
DbContextOptions<DatabaseContext> options = new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseInMemoryDatabase($"{caller}_{lineNum}")
|
|
.Options;
|
|
|
|
await using DatabaseContext context = new(options);
|
|
foreach (IList list in setDict.Select(p => p.Value))
|
|
{
|
|
foreach (object item in list)
|
|
{
|
|
context.Add(item);
|
|
}
|
|
}
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
await context.DisposeAsync();
|
|
return new DatabaseContext(options);
|
|
}
|
|
|
|
public static async Task<DatabaseContext> GetTestDatabase(List<UserEntity>? users = null, List<GameTokenEntity>? tokens = null,
|
|
[CallerMemberName] string caller = "", [CallerLineNumber] int lineNum = 0
|
|
)
|
|
{
|
|
users ??= new List<UserEntity>
|
|
{
|
|
GetUnitTestUser(),
|
|
};
|
|
|
|
tokens ??= new List<GameTokenEntity>
|
|
{
|
|
GetUnitTestToken(),
|
|
};
|
|
DbContextOptions<DatabaseContext> options = new DbContextOptionsBuilder<DatabaseContext>()
|
|
.UseInMemoryDatabase($"{caller}_{lineNum}")
|
|
.Options;
|
|
await using DatabaseContext context = new(options);
|
|
context.Users.AddRange(users);
|
|
context.GameTokens.AddRange(tokens);
|
|
await context.SaveChangesAsync();
|
|
await context.DisposeAsync();
|
|
return new DatabaseContext(options);
|
|
}
|
|
|
|
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, token);
|
|
}
|
|
|
|
public static ControllerContext GetMockControllerContext() =>
|
|
new()
|
|
{
|
|
HttpContext = new DefaultHttpContext(),
|
|
};
|
|
|
|
private static ControllerContext GetMockControllerContext(string? body) =>
|
|
new()
|
|
{
|
|
HttpContext = new DefaultHttpContext
|
|
{
|
|
Request =
|
|
{
|
|
ContentLength = body?.Length ?? 0,
|
|
Body = new MemoryStream(Encoding.ASCII.GetBytes(body ?? string.Empty)),
|
|
},
|
|
},
|
|
ActionDescriptor = new ControllerActionDescriptor
|
|
{
|
|
ActionName = "",
|
|
},
|
|
};
|
|
|
|
private static void SetupTestGameToken(ControllerBase controller, GameTokenEntity token)
|
|
{
|
|
controller.HttpContext.Items["Token"] = token;
|
|
}
|
|
} |