Files
Govor/Govor.Application.Tests/Services/PingHandlerServiceTests.cs
T
Artemy 65a43c09d3 Refactor message service and move tests to Application.Tests
Renamed IMessageService to IMessageCommandService and updated all usages accordingly. Moved and renamed test files from Govor.API.Tests to the new Govor.Application.Tests project, updating namespaces to match. Refactored message-related services and controllers to use the new naming and structure. Added Govor.Application.Tests project to the solution.
2025-07-10 18:05:10 +07:00

59 lines
1.7 KiB
C#

using Govor.Application.Services;
using Govor.Core.Models;
using Govor.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
namespace Govor.Application.Tests.Services;
[TestFixture]
public class PingHandlerServiceTests
{
private GovorDbContext _dbContext = null!;
private IMemoryCache _memoryCache = null!;
private PingHandlerService _service = null!;
private Guid _userId;
[SetUp]
public void SetUp()
{
var options = new DbContextOptionsBuilder<GovorDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
_dbContext = new GovorDbContext(options);
_memoryCache = new MemoryCache(new MemoryCacheOptions());
_service = new PingHandlerService(_dbContext, _memoryCache);
_userId = Guid.NewGuid();
_dbContext.Users.Add(new User
{
Id = _userId,
Username = "TestUser",
WasOnline = DateTime.UtcNow.AddHours(-1),
Description = "Test description",
PasswordHash = "hashed_password_here"
});
_dbContext.SaveChanges();
}
[Test]
public async Task Ping_DoesNotUpdate_WhenPingTooRecent()
{
// Arrange
var initial = DateTime.UtcNow.AddMinutes(-1);
_memoryCache.Set($"LastPing_{_userId}", DateTime.UtcNow);
var user = await _dbContext.Users.FirstAsync(u => u.Id == _userId);
var originalTime = user.WasOnline;
// Act
await _service.Ping(_userId);
var updatedUser = await _dbContext.Users.FirstAsync(u => u.Id == _userId);
// Assert
Assert.That(updatedUser.WasOnline, Is.EqualTo(originalTime));
}
}