mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-08-11 09:13:42 -04:00
Attempted to Test CacheService, but can't figure it out.
This commit is contained in:
parent
1d61d1057e
commit
f430595d11
@ -166,6 +166,16 @@ namespace API.Tests
|
||||
{
|
||||
Assert.Equal(expected, ParseEdition(input));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("12-14", 12)]
|
||||
[InlineData("24", 24)]
|
||||
[InlineData("18-04", 4)]
|
||||
public void MinimumNumberFromRangeTest(string input, int expected)
|
||||
{
|
||||
Assert.Equal(expected, MinimumNumberFromRange(input));
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void ParseInfoTest()
|
||||
|
62
API.Tests/Services/CacheServiceTests.cs
Normal file
62
API.Tests/Services/CacheServiceTests.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using API.Interfaces;
|
||||
using API.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace API.Tests.Services
|
||||
{
|
||||
public class CacheServiceTests
|
||||
{
|
||||
private readonly ICacheService _cacheService;
|
||||
private readonly ILogger<CacheService> _logger = Substitute.For<ILogger<CacheService>>();
|
||||
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
|
||||
private readonly IArchiveService _archiveService = Substitute.For<IArchiveService>();
|
||||
private readonly IDirectoryService _directoryService = Substitute.For<DirectoryService>();
|
||||
|
||||
public CacheServiceTests()
|
||||
{
|
||||
_cacheService = new CacheService(_logger, _unitOfWork, _archiveService, _directoryService);
|
||||
}
|
||||
|
||||
//string GetCachedPagePath(Volume volume, int page)
|
||||
[Fact]
|
||||
//[InlineData("", 0, "")]
|
||||
public void GetCachedPagePathTest_Should()
|
||||
{
|
||||
// TODO: Figure out how to test this
|
||||
// string archivePath = "flat file.zip";
|
||||
// int pageNum = 0;
|
||||
// string expected = "cache/1/pexels-photo-6551949.jpg";
|
||||
//
|
||||
// var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
|
||||
// var file = Path.Join(testDirectory, archivePath);
|
||||
// var volume = new Volume
|
||||
// {
|
||||
// Id = 1,
|
||||
// Files = new List<MangaFile>()
|
||||
// {
|
||||
// new()
|
||||
// {
|
||||
// Id = 1,
|
||||
// Chapter = 0,
|
||||
// FilePath = archivePath,
|
||||
// Format = MangaFormat.Archive,
|
||||
// NumberOfPages = 1,
|
||||
// }
|
||||
// },
|
||||
// Name = "1",
|
||||
// Number = 1
|
||||
// };
|
||||
//
|
||||
// var cacheService = Substitute.ForPartsOf<CacheService>();
|
||||
// cacheService.Configure().CacheDirectoryIsAccessible().Returns(true);
|
||||
// cacheService.Configure().GetVolumeCachePath(1, volume.Files.ElementAt(0)).Returns("cache/1/");
|
||||
// _directoryService.Configure().GetFiles("cache/1/").Returns(new string[] {"pexels-photo-6551949.jpg"});
|
||||
// Assert.Equal(expected, _cacheService.GetCachedPagePath(volume, pageNum));
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -23,6 +23,7 @@ namespace API.Extensions
|
||||
services.AddScoped<ICacheService, CacheService>();
|
||||
services.AddScoped<IUnitOfWork, UnitOfWork>();
|
||||
services.AddScoped<IScannerService, ScannerService>();
|
||||
services.AddScoped<IArchiveService, ArchiveService>();
|
||||
|
||||
|
||||
|
||||
|
@ -32,5 +32,7 @@ namespace API.Interfaces
|
||||
/// <param name="page">Page number to look for</param>
|
||||
/// <returns></returns>
|
||||
string GetCachedPagePath(Volume volume, int page);
|
||||
|
||||
bool CacheDirectoryIsAccessible();
|
||||
}
|
||||
}
|
@ -14,5 +14,6 @@ namespace API.Interfaces
|
||||
IEnumerable<string> ListDirectory(string rootPath);
|
||||
|
||||
Task<ImageDto> ReadImageAsync(string imagePath);
|
||||
string[] GetFiles(string path); // TODO: Refactor. This is currently for CacheServiceTest mocking
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using API.Entities;
|
||||
|
||||
@ -395,5 +396,11 @@ namespace API.Parser
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
return ImageFileExtensions.Contains(fileInfo.Extension);
|
||||
}
|
||||
|
||||
public static int MinimumNumberFromRange(string range)
|
||||
{
|
||||
var tokens = range.Split("-");
|
||||
return tokens.Min(Int32.Parse);
|
||||
}
|
||||
}
|
||||
}
|
@ -13,23 +13,23 @@ namespace API.Services
|
||||
{
|
||||
public class CacheService : ICacheService
|
||||
{
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly ILogger<CacheService> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IArchiveService _archiveService;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly NumericComparer _numericComparer;
|
||||
public static readonly string CacheDirectory = Path.GetFullPath(Path.Join(Directory.GetCurrentDirectory(), "../cache/"));
|
||||
|
||||
public CacheService(IDirectoryService directoryService, ILogger<CacheService> logger, IUnitOfWork unitOfWork, IArchiveService archiveService)
|
||||
public CacheService(ILogger<CacheService> logger, IUnitOfWork unitOfWork, IArchiveService archiveService, IDirectoryService directoryService)
|
||||
{
|
||||
_directoryService = directoryService;
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_archiveService = archiveService;
|
||||
_directoryService = directoryService;
|
||||
_numericComparer = new NumericComparer();
|
||||
}
|
||||
|
||||
private bool CacheDirectoryIsAccessible()
|
||||
public bool CacheDirectoryIsAccessible()
|
||||
{
|
||||
_logger.LogDebug($"Checking if valid Cache directory: {CacheDirectory}");
|
||||
var di = new DirectoryInfo(CacheDirectory);
|
||||
@ -96,7 +96,7 @@ namespace API.Services
|
||||
|
||||
|
||||
|
||||
private string GetVolumeCachePath(int volumeId, MangaFile file)
|
||||
public string GetVolumeCachePath(int volumeId, MangaFile file)
|
||||
{
|
||||
var extractPath = Path.GetFullPath(Path.Join(Directory.GetCurrentDirectory(), $"../cache/{volumeId}/"));
|
||||
if (file.Chapter > 0)
|
||||
@ -106,9 +106,9 @@ namespace API.Services
|
||||
return extractPath;
|
||||
}
|
||||
|
||||
private IEnumerable<MangaFile> GetOrderedChapters(ICollection<MangaFile> files)
|
||||
public IEnumerable<MangaFile> GetOrderedChapters(ICollection<MangaFile> files)
|
||||
{
|
||||
return files.OrderBy(f => f.Chapter).Where(f => f.Chapter != 0);
|
||||
return files.OrderBy(f => f.Chapter).Where(f => f.Chapter > 0 || f.Volume.Number != 0);
|
||||
}
|
||||
|
||||
public string GetCachedPagePath(Volume volume, int page)
|
||||
@ -122,7 +122,7 @@ namespace API.Services
|
||||
if (page + 1 < (mangaFile.NumberOfPages + pagesSoFar))
|
||||
{
|
||||
var path = GetVolumeCachePath(volume.Id, mangaFile);
|
||||
var files = DirectoryService.GetFiles(path);
|
||||
var files = _directoryService.GetFiles(path);
|
||||
Array.Sort(files, _numericComparer);
|
||||
|
||||
return files.ElementAt(page - pagesSoFar);
|
||||
|
@ -33,10 +33,9 @@ namespace API.Services
|
||||
reSearchPattern.IsMatch(Path.GetExtension(file)));
|
||||
}
|
||||
|
||||
public static string[] GetFiles(string path)
|
||||
public string[] GetFiles(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) return Array.Empty<string>();
|
||||
return Directory.GetFiles(path);
|
||||
return !Directory.Exists(path) ? Array.Empty<string>() : Directory.GetFiles(path);
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListDirectory(string rootPath)
|
||||
@ -48,7 +47,6 @@ namespace API.Services
|
||||
.Where(dir => !(dir.Attributes.HasFlag(FileAttributes.Hidden) || dir.Attributes.HasFlag(FileAttributes.System)))
|
||||
.Select(d => d.Name).ToImmutableList();
|
||||
|
||||
|
||||
return dirs;
|
||||
}
|
||||
|
||||
|
@ -224,11 +224,7 @@ namespace API.Services
|
||||
};
|
||||
}
|
||||
|
||||
private int MinimumNumberFromRange(string range)
|
||||
{
|
||||
var tokens = range.Split("-");
|
||||
return Int32.Parse(tokens.Length >= 1 ? tokens[0] : range);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Creates or Updates volumes for a given series
|
||||
@ -250,7 +246,7 @@ namespace API.Services
|
||||
var existingFile = existingVolume.Files.SingleOrDefault(f => f.FilePath == info.FullFilePath);
|
||||
if (existingFile != null)
|
||||
{
|
||||
existingFile.Chapter = MinimumNumberFromRange(info.Chapters);
|
||||
existingFile.Chapter = Parser.Parser.MinimumNumberFromRange(info.Chapters);
|
||||
existingFile.Format = info.Format;
|
||||
existingFile.NumberOfPages = _archiveService.GetNumberOfPagesFromArchive(info.FullFilePath);
|
||||
}
|
||||
@ -282,7 +278,7 @@ namespace API.Services
|
||||
var vol = new Volume()
|
||||
{
|
||||
Name = info.Volumes,
|
||||
Number = MinimumNumberFromRange(info.Volumes),
|
||||
Number = Parser.Parser.MinimumNumberFromRange(info.Volumes),
|
||||
Files = new List<MangaFile>()
|
||||
{
|
||||
CreateMangaFile(info)
|
||||
@ -316,9 +312,6 @@ namespace API.Services
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user