Kavita/API.Tests/Services/MetadataServiceTests.cs
Robbie Davis a601942ec5
Cover generation issue on first scan flow (#517)
* Cover generation issue on first scan flow

- Fixed logic around whether a chapter cover image should be generated. New logic adds grouping priority, changes an AND to an OR and adds an additional check to see if the cover image has been lock (custom image uploaded)

* Sonar update

* Refactored out the cover image updating logic to a new call (ShouldUpdateCoverImage) and updated ONLY chapters. Added a blank slate unit test to build out conditions.

* Fixed up unit case

* Fixed some logic on when to update a cover image

* Fixed an issue where 1) we were refreshing metadata anytime we adjusted cover image on a series and 2) Cover generation wasn't properly being handled on first run.

* Cleaned up the code for when a cover image change needs to trigger a refresh metadata task

Co-authored-by: Joseph Milazzo <joseph.v.milazzo@gmail.com>
2021-08-22 11:32:38 -07:00

106 lines
4.2 KiB
C#

using System;
using System.IO;
using API.Entities;
using API.Interfaces;
using API.Interfaces.Services;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services
{
public class MetadataServiceTests
{
private readonly string _testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
private readonly MetadataService _metadataService;
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
private readonly IImageService _imageService = Substitute.For<IImageService>();
private readonly IBookService _bookService = Substitute.For<IBookService>();
private readonly IArchiveService _archiveService = Substitute.For<IArchiveService>();
private readonly ILogger<MetadataService> _logger = Substitute.For<ILogger<MetadataService>>();
public MetadataServiceTests()
{
_metadataService = new MetadataService(_unitOfWork, _logger, _archiveService, _bookService, _imageService);
}
[Fact]
public void ShouldUpdateCoverImage_OnFirstRun()
{
// Represents first run
Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = DateTime.Now
}, false, false));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_FileModified()
{
// Represents first run
Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime.Subtract(TimeSpan.FromDays(1))
}, false, false));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_CoverImageLocked()
{
// Represents first run
Assert.False(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime
}, false, true));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_ForceUpdate()
{
// Represents first run
Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime
}, true, false));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_NoFileChangeButNoCoverImage()
{
// Represents first run
Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime
}, false, false));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_FileChangeButNoCoverImage()
{
// Represents first run
Assert.True(MetadataService.ShouldUpdateCoverImage(null, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime + TimeSpan.FromDays(1)
}, false, false));
}
[Fact]
public void ShouldUpdateCoverImage_OnSecondRun_CoverImageSet()
{
// Represents first run
Assert.False(MetadataService.ShouldUpdateCoverImage(new byte[] {1}, new MangaFile()
{
FilePath = Path.Join(_testDirectory, "file in folder.zip"),
LastModified = new FileInfo(Path.Join(_testDirectory, "file in folder.zip")).LastWriteTime
}, false, false));
}
}
}