mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-06 11:58:38 +00:00
Use SQLite in-memory in lieu of EF In-Memory for testing
Also replaces usages of DateTime.Now with DateTime.UtcNow for internal time storage
This commit is contained in:
parent
23cb1bef1c
commit
1737a16f38
10 changed files with 93 additions and 23 deletions
|
@ -28,11 +28,7 @@ public class ApiStartup
|
|||
}
|
||||
);
|
||||
|
||||
services.AddDbContext<DatabaseContext>(builder =>
|
||||
{
|
||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
||||
MySqlServerVersion.LatestSupportedServerVersion);
|
||||
});
|
||||
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||
|
||||
services.AddSwaggerGen
|
||||
(
|
||||
|
|
|
@ -54,11 +54,7 @@ public class GameServerStartup
|
|||
}
|
||||
);
|
||||
|
||||
services.AddDbContext<DatabaseContext>(builder =>
|
||||
{
|
||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
||||
MySqlServerVersion.LatestSupportedServerVersion);
|
||||
});
|
||||
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||
|
||||
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
||||
? new MailQueueService(new SmtpMailSender())
|
||||
|
|
|
@ -64,7 +64,7 @@ public class CompleteEmailVerificationPage : BaseLayout
|
|||
webToken.UserToken,
|
||||
new CookieOptions
|
||||
{
|
||||
Expires = DateTimeOffset.Now.AddDays(7),
|
||||
Expires = DateTimeOffset.UtcNow.AddDays(7),
|
||||
});
|
||||
return this.Redirect("/passwordReset");
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
|
||||
<div class="column">
|
||||
<h2 class="ui black image header centered">
|
||||
<img src="~/@(ServerConfiguration.Instance.WebsiteConfiguration.PrideEventEnabled && DateTime.Now.Month == 6 ? "logo-pride.png" : "logo-color.png")"
|
||||
<img src="~/@(ServerConfiguration.Instance.WebsiteConfiguration.PrideEventEnabled && DateTime.UtcNow.Month == 6 ? "logo-pride.png" : "logo-color.png")"
|
||||
alt="Instance logo"
|
||||
class="image"
|
||||
style="width: 128px;"/>
|
||||
|
|
|
@ -100,7 +100,7 @@ public class LoginForm : BaseLayout
|
|||
webToken.UserToken,
|
||||
new CookieOptions
|
||||
{
|
||||
Expires = DateTimeOffset.Now.AddDays(7),
|
||||
Expires = DateTimeOffset.UtcNow.AddDays(7),
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ using LBPUnion.ProjectLighthouse.Services;
|
|||
using LBPUnion.ProjectLighthouse.Types.Mail;
|
||||
using Microsoft.AspNetCore.HttpOverrides;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
#if !DEBUG
|
||||
|
@ -39,11 +38,7 @@ public class WebsiteStartup
|
|||
services.AddControllers();
|
||||
services.AddRazorPages().WithRazorPagesAtContentRoot();
|
||||
|
||||
services.AddDbContext<DatabaseContext>(builder =>
|
||||
{
|
||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
||||
MySqlServerVersion.LatestSupportedServerVersion);
|
||||
});
|
||||
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||
|
||||
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
||||
? new MailQueueService(new SmtpMailSender())
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Database;
|
||||
using LBPUnion.ProjectLighthouse.Tests.Helpers;
|
||||
using LBPUnion.ProjectLighthouse.Types.Activity;
|
||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace ProjectLighthouse.Tests.GameApiTests.Unit.Activity;
|
||||
|
||||
[Trait("Category", "Unit")]
|
||||
public class ActivityInterceptorTests
|
||||
{
|
||||
private static async Task<DatabaseContext> GetTestDatabase(IMock<IEntityEventHandler> eventHandlerMock)
|
||||
{
|
||||
DbContextOptionsBuilder<DatabaseContext> optionsBuilder = await MockHelper.GetInMemoryDbOptions();
|
||||
|
||||
optionsBuilder.AddInterceptors(new ActivityInterceptor(eventHandlerMock.Object));
|
||||
DatabaseContext database = new(optionsBuilder.Options);
|
||||
await database.Database.EnsureCreatedAsync();
|
||||
return database;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveChangesWithNewEntity_ShouldCallEntityInserted()
|
||||
{
|
||||
Mock<IEntityEventHandler> eventHandlerMock = new();
|
||||
DatabaseContext database = await GetTestDatabase(eventHandlerMock);
|
||||
|
||||
database.Users.Add(new UserEntity
|
||||
{
|
||||
UserId = 1,
|
||||
Username = "test",
|
||||
});
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
eventHandlerMock.Verify(x => x.OnEntityInserted(It.IsAny<DatabaseContext>(), It.Is<object>(user => user is UserEntity)), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveChangesWithModifiedEntity_ShouldCallEntityChanged()
|
||||
{
|
||||
Mock<IEntityEventHandler> eventHandlerMock = new();
|
||||
DatabaseContext database = await GetTestDatabase(eventHandlerMock);
|
||||
|
||||
UserEntity user = new()
|
||||
{
|
||||
Username = "test",
|
||||
};
|
||||
|
||||
database.Users.Add(user);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
user.Username = "test2";
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
eventHandlerMock.Verify(x => x.OnEntityChanged(It.IsAny<DatabaseContext>(),
|
||||
It.Is<object>(u => u is UserEntity && ((UserEntity)u).Username == "test"),
|
||||
It.Is<object>(u => u is UserEntity && ((UserEntity)u).Username == "test2")),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SaveChangesWithModifiedEntity_ShouldCallEntityDeleted()
|
||||
{
|
||||
Mock<IEntityEventHandler> eventHandlerMock = new();
|
||||
DatabaseContext database = await GetTestDatabase(eventHandlerMock);
|
||||
|
||||
UserEntity user = new()
|
||||
{
|
||||
Username = "test",
|
||||
};
|
||||
|
||||
database.Users.Add(user);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
database.Users.Remove(user);
|
||||
await database.SaveChangesAsync();
|
||||
|
||||
eventHandlerMock.Verify(x => x.OnEntityDeleted(It.IsAny<DatabaseContext>(), It.Is<object>(u => u is UserEntity)), Times.Once);
|
||||
}
|
||||
}
|
|
@ -5,8 +5,8 @@ namespace LBPUnion.ProjectLighthouse.Extensions;
|
|||
public static class DateTimeExtensions
|
||||
{
|
||||
public static long ToUnixTimeMilliseconds(this DateTime dateTime) =>
|
||||
new DateTimeOffset(dateTime).ToUniversalTime().ToUnixTimeMilliseconds();
|
||||
((DateTimeOffset)DateTime.SpecifyKind(dateTime, DateTimeKind.Utc)).ToUnixTimeMilliseconds();
|
||||
|
||||
public static DateTime FromUnixTimeMilliseconds(long timestamp) =>
|
||||
DateTimeOffset.FromUnixTimeMilliseconds(timestamp).ToUniversalTime().DateTime;
|
||||
DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime;
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using LBPUnion.ProjectLighthouse.Configuration;
|
||||
using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.StorableLists.Stores;
|
||||
|
@ -10,7 +11,7 @@ public static class RoomStore
|
|||
|
||||
public static StorableList<Room> GetRooms()
|
||||
{
|
||||
if (RedisDatabase.Initialized)
|
||||
if (!ServerStatics.IsUnitTesting && RedisDatabase.Initialized)
|
||||
{
|
||||
return new RedisStorableList<Room>(RedisDatabase.GetRooms());
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ public struct MinimalUserListResponse : ILbpSerializable
|
|||
|
||||
public class MinimalUserProfile : ILbpSerializable
|
||||
{
|
||||
|
||||
[XmlElement("npHandle")]
|
||||
public NpHandle UserHandle { get; set; } = new();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue