mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-07 12:28:39 +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 =>
|
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||||
{
|
|
||||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
|
||||||
MySqlServerVersion.LatestSupportedServerVersion);
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddSwaggerGen
|
services.AddSwaggerGen
|
||||||
(
|
(
|
||||||
|
|
|
@ -54,11 +54,7 @@ public class GameServerStartup
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
services.AddDbContext<DatabaseContext>(builder =>
|
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||||
{
|
|
||||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
|
||||||
MySqlServerVersion.LatestSupportedServerVersion);
|
|
||||||
});
|
|
||||||
|
|
||||||
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
||||||
? new MailQueueService(new SmtpMailSender())
|
? new MailQueueService(new SmtpMailSender())
|
||||||
|
|
|
@ -64,7 +64,7 @@ public class CompleteEmailVerificationPage : BaseLayout
|
||||||
webToken.UserToken,
|
webToken.UserToken,
|
||||||
new CookieOptions
|
new CookieOptions
|
||||||
{
|
{
|
||||||
Expires = DateTimeOffset.Now.AddDays(7),
|
Expires = DateTimeOffset.UtcNow.AddDays(7),
|
||||||
});
|
});
|
||||||
return this.Redirect("/passwordReset");
|
return this.Redirect("/passwordReset");
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
<div class="column">
|
<div class="column">
|
||||||
<h2 class="ui black image header centered">
|
<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"
|
alt="Instance logo"
|
||||||
class="image"
|
class="image"
|
||||||
style="width: 128px;"/>
|
style="width: 128px;"/>
|
||||||
|
|
|
@ -100,7 +100,7 @@ public class LoginForm : BaseLayout
|
||||||
webToken.UserToken,
|
webToken.UserToken,
|
||||||
new CookieOptions
|
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 LBPUnion.ProjectLighthouse.Types.Mail;
|
||||||
using Microsoft.AspNetCore.HttpOverrides;
|
using Microsoft.AspNetCore.HttpOverrides;
|
||||||
using Microsoft.AspNetCore.Localization;
|
using Microsoft.AspNetCore.Localization;
|
||||||
using Microsoft.EntityFrameworkCore;
|
|
||||||
using Microsoft.Extensions.FileProviders;
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
|
@ -39,11 +38,7 @@ public class WebsiteStartup
|
||||||
services.AddControllers();
|
services.AddControllers();
|
||||||
services.AddRazorPages().WithRazorPagesAtContentRoot();
|
services.AddRazorPages().WithRazorPagesAtContentRoot();
|
||||||
|
|
||||||
services.AddDbContext<DatabaseContext>(builder =>
|
services.AddDbContext<DatabaseContext>(DatabaseContext.ConfigureBuilder());
|
||||||
{
|
|
||||||
builder.UseMySql(ServerConfiguration.Instance.DbConnectionString,
|
|
||||||
MySqlServerVersion.LatestSupportedServerVersion);
|
|
||||||
});
|
|
||||||
|
|
||||||
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
IMailService mailService = ServerConfiguration.Instance.Mail.MailEnabled
|
||||||
? new MailQueueService(new SmtpMailSender())
|
? 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 class DateTimeExtensions
|
||||||
{
|
{
|
||||||
public static long ToUnixTimeMilliseconds(this DateTime dateTime) =>
|
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) =>
|
public static DateTime FromUnixTimeMilliseconds(long timestamp) =>
|
||||||
DateTimeOffset.FromUnixTimeMilliseconds(timestamp).ToUniversalTime().DateTime;
|
DateTimeOffset.FromUnixTimeMilliseconds(timestamp).UtcDateTime;
|
||||||
}
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using LBPUnion.ProjectLighthouse.Configuration;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms;
|
using LBPUnion.ProjectLighthouse.Types.Matchmaking.Rooms;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.StorableLists.Stores;
|
namespace LBPUnion.ProjectLighthouse.StorableLists.Stores;
|
||||||
|
@ -10,7 +11,7 @@ public static class RoomStore
|
||||||
|
|
||||||
public static StorableList<Room> GetRooms()
|
public static StorableList<Room> GetRooms()
|
||||||
{
|
{
|
||||||
if (RedisDatabase.Initialized)
|
if (!ServerStatics.IsUnitTesting && RedisDatabase.Initialized)
|
||||||
{
|
{
|
||||||
return new RedisStorableList<Room>(RedisDatabase.GetRooms());
|
return new RedisStorableList<Room>(RedisDatabase.GetRooms());
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,6 @@ public struct MinimalUserListResponse : ILbpSerializable
|
||||||
|
|
||||||
public class MinimalUserProfile : ILbpSerializable
|
public class MinimalUserProfile : ILbpSerializable
|
||||||
{
|
{
|
||||||
|
|
||||||
[XmlElement("npHandle")]
|
[XmlElement("npHandle")]
|
||||||
public NpHandle UserHandle { get; set; } = new();
|
public NpHandle UserHandle { get; set; } = new();
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue