811b6ef51a
Backend CI / build-and-test (push) Successful in 52s
Frontend CI / build-and-check (push) Failing after 5m15s
🚀 Create and publish a Docker image / Detect changes in backend and frontend (push) Successful in 16s
🚀 Create and publish a Docker image / Build & publish backend image (push) Successful in 1m0s
🚀 Create and publish a Docker image / Build & publish frontend image (push) Successful in 32s
🚀 Create and publish a Docker image / Update stack on Portainer (push) Successful in 13s
81 lines
2.8 KiB
C#
81 lines
2.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using UniVerse.Application.DTOs.Notifications;
|
|
using UniVerse.Application.Interfaces;
|
|
using UniVerse.Domain.Entities;
|
|
using UniVerse.Infrastructure.Data;
|
|
using UniVerse.Infrastructure.Services;
|
|
using Xunit;
|
|
|
|
namespace UniVerse.Api.Tests.Users;
|
|
|
|
public class UserServiceTests
|
|
{
|
|
[Fact]
|
|
public async Task GetStatsAsync_ReturnsLevelProgressThresholds()
|
|
{
|
|
await using var db = CreateDbContext();
|
|
SeedLevelThresholds(db);
|
|
db.Users.Add(new User { Id = 1, Email = "student@test.local", Xp = 120 });
|
|
await db.SaveChangesAsync();
|
|
var service = CreateService(db);
|
|
|
|
var stats = await service.GetStatsAsync(1);
|
|
|
|
Assert.Equal(2, stats.Level);
|
|
Assert.Equal(100, stats.CurrentLevelXp);
|
|
Assert.Equal(300, stats.NextLevelXp);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetStatsAsync_ReturnsNullNextLevelAtMaxConfiguredLevel()
|
|
{
|
|
await using var db = CreateDbContext();
|
|
SeedLevelThresholds(db);
|
|
db.Users.Add(new User { Id = 1, Email = "student@test.local", Xp = 350 });
|
|
await db.SaveChangesAsync();
|
|
var service = CreateService(db);
|
|
|
|
var stats = await service.GetStatsAsync(1);
|
|
|
|
Assert.Equal(3, stats.Level);
|
|
Assert.Equal(300, stats.CurrentLevelXp);
|
|
Assert.Null(stats.NextLevelXp);
|
|
}
|
|
|
|
private static AppDbContext CreateDbContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseInMemoryDatabase($"UserServiceTests_{Guid.NewGuid()}")
|
|
.Options;
|
|
return new AppDbContext(options);
|
|
}
|
|
|
|
private static UserService CreateService(AppDbContext db)
|
|
{
|
|
var notifications = Substitute.For<INotificationService>();
|
|
notifications.CreateUserNotificationAsync(
|
|
Arg.Any<int>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<string>(),
|
|
Arg.Any<CancellationToken>())
|
|
.Returns(callInfo => new UserNotificationDto(1, callInfo.ArgAt<string>(1), callInfo.ArgAt<string>(2), callInfo.ArgAt<string>(3), false, DateTime.UtcNow));
|
|
notifications.SendAsync(Arg.Any<NotificationMessage>(), Arg.Any<CancellationToken>())
|
|
.Returns(Task.CompletedTask);
|
|
|
|
var gamification = new GamificationService(db, notifications, NullLogger<GamificationService>.Instance);
|
|
return new UserService(db, gamification);
|
|
}
|
|
|
|
private static void SeedLevelThresholds(AppDbContext db)
|
|
{
|
|
db.LevelThresholds.AddRange(
|
|
new LevelThreshold { Level = 1, RequiredXp = 0 },
|
|
new LevelThreshold { Level = 2, RequiredXp = 100 },
|
|
new LevelThreshold { Level = 3, RequiredXp = 300 });
|
|
db.SaveChanges();
|
|
}
|
|
}
|