Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Attempted to Test CacheService, but can't figure it out.
Joseph Milazzof430595d1130
diff --git a/API.Tests/ParserTest.cs b/API.Tests/ParserTest.csindex de1f77936..34b7798a3 100644--- a/API.Tests/ParserTest.cs+++ b/API.Tests/ParserTest.cs@@ -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()diff --git a/API.Tests/Services/CacheServiceTests.cs b/API.Tests/Services/CacheServiceTests.csnew file mode 100644index 000000000..e5581c98b--- /dev/null+++ b/API.Tests/Services/CacheServiceTests.cs@@ -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);+ }+++ }+}\ No newline at end of filediff --git a/API/Extensions/ApplicationServiceExtensions.cs b/API/Extensions/ApplicationServiceExtensions.csindex 318c87340..8cda03754 100644--- a/API/Extensions/ApplicationServiceExtensions.cs+++ b/API/Extensions/ApplicationServiceExtensions.cs@@ -23,6 +23,7 @@ namespace API.Extensionsservices.AddScoped<ICacheService, CacheService>();services.AddScoped<IUnitOfWork, UnitOfWork>();services.AddScoped<IScannerService, ScannerService>();+ services.AddScoped<IArchiveService, ArchiveService>();diff --git a/API/Interfaces/ICacheService.cs b/API/Interfaces/ICacheService.csindex 81a4ef5fa..4bef9bf06 100644--- a/API/Interfaces/ICacheService.cs+++ b/API/Interfaces/ICacheService.cs@@ -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();}}\ No newline at end of filediff --git a/API/Interfaces/IDirectoryService.cs b/API/Interfaces/IDirectoryService.csindex 5f9958f03..a004ac6e2 100644--- a/API/Interfaces/IDirectoryService.cs+++ b/API/Interfaces/IDirectoryService.cs@@ -14,5 +14,6 @@ namespace API.InterfacesIEnumerable<string> ListDirectory(string rootPath);Task<ImageDto> ReadImageAsync(string imagePath);+ string[] GetFiles(string path); // TODO: Refactor. This is currently for CacheServiceTest mocking}}\ No newline at end of filediff --git a/API/Parser/Parser.cs b/API/Parser/Parser.csindex 431d56330..694956202 100644--- a/API/Parser/Parser.cs+++ b/API/Parser/Parser.cs@@ -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.Parservar fileInfo = new FileInfo(filePath);return ImageFileExtensions.Contains(fileInfo.Extension);}++ public static int MinimumNumberFromRange(string range)+ {+ var tokens = range.Split("-");+ return tokens.Min(Int32.Parse);+ }}}\ No newline at end of filediff --git a/API/Services/CacheService.cs b/API/Services/CacheService.csindex 66e8c9e2c..2308261ce 100644--- a/API/Services/CacheService.cs+++ b/API/Services/CacheService.cs@@ -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.Servicesreturn 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.Servicesif (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);diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.csindex d910c5c1b..73676d1d9 100644--- a/API/Services/DirectoryService.cs+++ b/API/Services/DirectoryService.cs@@ -33,10 +33,9 @@ namespace API.ServicesreSearchPattern.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;}diff --git a/API/Services/ScannerService.cs b/API/Services/ScannerService.csindex 1b4516c94..eaf2fff5c 100644--- a/API/Services/ScannerService.cs+++ b/API/Services/ScannerService.cs@@ -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.Servicesvar 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.Servicesvar 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();}---}