mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-02-15 07:52:12 -05:00
Co-authored-by: Joe Milazzo <josephmajora@gmail.com> Co-authored-by: Weblate (bot) <hosted@weblate.org> Co-authored-by: Adam Havránek <adamhavra@seznam.cz> Co-authored-by: Aindriú Mac Giolla Eoin <aindriu80@gmail.com> Co-authored-by: Christophorus Daniel Tegar P <tegar.200503@gmail.com> Co-authored-by: Frozehunter <frozehunter@me.com> Co-authored-by: Gregory.Open <gregory.open@proton.me> Co-authored-by: Havokdan <havokdan@yahoo.com.br> Co-authored-by: Kovács Gábor <naszalykmb@gmail.com> Co-authored-by: karigane <169052233+karigane-cha@users.noreply.github.com> Co-authored-by: lin49931104 <a82122794@gmail.com> Co-authored-by: Максим Горпиніч <gorpinicmaksim0@gmail.com> Co-authored-by: 無情天 <kofzhanganguo@126.com> Co-authored-by: 안세훈 <on9686@gmail.com>
123 lines
4.0 KiB
C#
123 lines
4.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using API.Data;
|
|
using API.Data.Repositories;
|
|
using API.DTOs.Progress;
|
|
using API.Entities;
|
|
using API.Entities.Enums;
|
|
using API.Entities.Progress;
|
|
using API.Extensions.QueryExtensions;
|
|
using API.Helpers.Builders;
|
|
using API.Services.Reading;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace API.Tests.Services;
|
|
|
|
public class ReadingHistoryServiceTests(ITestOutputHelper testOutputHelper) : AbstractDbTest(testOutputHelper)
|
|
{
|
|
private ReadingHistoryService Setup(DataContext context)
|
|
{
|
|
return new ReadingHistoryService(context, Substitute.For<ILogger<ReadingHistoryService>>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ActiveSession_DoNotCreateHistoryItems()
|
|
{
|
|
var (_, dataContext, _) = await CreateDatabase();
|
|
var service = Setup(dataContext);
|
|
|
|
// Setup data
|
|
var lib = await dataContext.Library.Includes(LibraryIncludes.Series).FirstAsync();
|
|
lib.Series.Add(new SeriesBuilder("Test")
|
|
.WithVolume(new VolumeBuilder("1").WithChapter(new ChapterBuilder("1").WithPages(2).Build()).Build())
|
|
.Build());
|
|
|
|
await dataContext.AppUser.AddAsync(new AppUser() { UserName = "Test" });
|
|
|
|
await dataContext.SaveChangesAsync();
|
|
|
|
// Create an active session dated for yesterday
|
|
var yesterday= DateTime.Now.Date.AddDays(-1);
|
|
var yesterdayUtc = DateTime.UtcNow.Date.AddDays(-1);
|
|
await dataContext.AppUserReadingSession.AddAsync(new AppUserReadingSession()
|
|
{
|
|
ActivityData =
|
|
[
|
|
new AppUserReadingSessionActivityData(new ProgressDto()
|
|
{
|
|
ChapterId = 1, VolumeId = 1, LibraryId = 1, PageNum = 1, SeriesId = 1
|
|
}, 1, MangaFormat.Archive)
|
|
],
|
|
AppUserId = 1,
|
|
StartTime = yesterday,
|
|
StartTimeUtc = yesterdayUtc,
|
|
IsActive = true,
|
|
});
|
|
|
|
// Run the service
|
|
await service.AggregateYesterdaysActivity();
|
|
|
|
// Check that there are no history items
|
|
Assert.False(await dataContext.AppUserReadingHistory.AnyAsync());
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public async Task CreatesForYesterdaySessions()
|
|
{
|
|
var (_, dataContext, _) = await CreateDatabase();
|
|
var service = Setup(dataContext);
|
|
|
|
// Setup data
|
|
var lib = await dataContext.Library.Includes(LibraryIncludes.Series).FirstAsync();
|
|
lib.Series.Add(new SeriesBuilder("Test")
|
|
.WithVolume(new VolumeBuilder("1").WithChapter(new ChapterBuilder("1").WithPages(2).Build()).Build())
|
|
.Build());
|
|
|
|
await dataContext.AppUser.AddAsync(new AppUser() { UserName = "Test" });
|
|
|
|
await dataContext.SaveChangesAsync();
|
|
|
|
// Create an active session dated for yesterday
|
|
var yesterday= DateTime.Now.Date.AddDays(-1);
|
|
var yesterdayUtc = DateTime.UtcNow.Date.AddDays(-1);
|
|
var activityData = new AppUserReadingSessionActivityData(new ProgressDto()
|
|
{
|
|
ChapterId = 1, VolumeId = 1, LibraryId = 1, PageNum = 1, SeriesId = 1
|
|
}, 1, MangaFormat.Archive);
|
|
|
|
activityData.StartTime = yesterday;
|
|
activityData.StartTimeUtc = yesterdayUtc;
|
|
|
|
await dataContext.AppUserReadingSession.AddAsync(new AppUserReadingSession()
|
|
{
|
|
ActivityData =
|
|
[
|
|
activityData
|
|
],
|
|
AppUserId = 1,
|
|
StartTime = yesterday,
|
|
StartTimeUtc = yesterdayUtc,
|
|
EndTime = yesterday.AddHours(1),
|
|
EndTimeUtc = yesterdayUtc.AddHours(1),
|
|
IsActive = false,
|
|
});
|
|
|
|
await dataContext.SaveChangesAsync();
|
|
|
|
// Run the service
|
|
await service.AggregateYesterdaysActivity();
|
|
|
|
// Check that there are no history items
|
|
var historyItems = await dataContext.AppUserReadingHistory.ToListAsync();
|
|
Assert.Single(historyItems);
|
|
Assert.Single(historyItems[0].Data.Activities);
|
|
}
|
|
}
|
|
|