.NET 6 Coding Patterns + Unit Tests (#823)

* Refactored all files to have Interfaces within the same file. Started moving over to file-scoped namespaces.

* Refactored common methods for getting underlying file's cover, pages, and extracting into 1 interface.

* More refactoring around removing dependence on explicit filetype testing for getting information.

* Code is buildable, tests are broken. Huge refactor (not completed) which makes most of DirectoryService testable with a mock filesystem (and thus the services that utilize it).

* Finished porting DirectoryService to use mocked filesystem implementation.

* Added a null check

* Added a null check

* Finished all unit tests for DirectoryService.

* Some misc cleanup on the code

* Fixed up some bugs from refactoring scan loop.

* Implemented CleanupService testing and refactored more of DirectoryService to be non-static.

Fixed a bug where cover file cleanup wasn't properly finding files due to a regex bug.

* Fixed an issue in CleanupBackup() where we weren't properly selecting database files older than 30 days. Finished CleanupService Tests.

* Refactored Flatten and RemoveNonImages to directory service to allow CacheService to be testable.

* Finally have CacheService tested. Rewrote GetCachedPagePath() to be much more straightforward & performant.

* Updated DefaultParserTests.cs to contain all existing tests and follow new test layout format.

* All tests fixed up
This commit is contained in:
Joseph Milazzo 2021-12-05 10:58:53 -06:00 committed by GitHub
parent bf1876ff44
commit bbe8f800f6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
115 changed files with 6734 additions and 5370 deletions

View file

@ -1,10 +1,11 @@
using System.Diagnostics;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.IO.Compression;
using System.Linq;
using API.Archive;
using API.Data.Metadata;
using API.Interfaces.Services;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
@ -20,12 +21,12 @@ namespace API.Tests.Services
private readonly ArchiveService _archiveService;
private readonly ILogger<ArchiveService> _logger = Substitute.For<ILogger<ArchiveService>>();
private readonly ILogger<DirectoryService> _directoryServiceLogger = Substitute.For<ILogger<DirectoryService>>();
private readonly IDirectoryService _directoryService = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), new MockFileSystem());
private readonly IDirectoryService _directoryService = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), new FileSystem());
public ArchiveServiceTests(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_archiveService = new ArchiveService(_logger, _directoryService);
_archiveService = new ArchiveService(_logger, _directoryService, new ImageService(Substitute.For<ILogger<ImageService>>(), _directoryService));
}
[Theory]
@ -108,15 +109,15 @@ namespace API.Tests.Services
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives");
var extractDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ArchiveService/Archives/Extraction");
DirectoryService.ClearAndDeleteDirectory(extractDirectory);
_directoryService.ClearAndDeleteDirectory(extractDirectory);
Stopwatch sw = Stopwatch.StartNew();
var sw = Stopwatch.StartNew();
_archiveService.ExtractArchive(Path.Join(testDirectory, archivePath), extractDirectory);
var di1 = new DirectoryInfo(extractDirectory);
Assert.Equal(expectedFileCount, di1.Exists ? di1.GetFiles().Length : 0);
Assert.Equal(expectedFileCount, di1.Exists ? _directoryService.GetFiles(extractDirectory, searchOption:SearchOption.AllDirectories).Count() : 0);
_testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms");
DirectoryService.ClearAndDeleteDirectory(extractDirectory);
_directoryService.ClearAndDeleteDirectory(extractDirectory);
}
@ -167,8 +168,8 @@ namespace API.Tests.Services
var sw = Stopwatch.StartNew();
var outputDir = Path.Join(testDirectory, "output");
DirectoryService.ClearAndDeleteDirectory(outputDir);
DirectoryService.ExistOrCreate(outputDir);
_directoryService.ClearAndDeleteDirectory(outputDir);
_directoryService.ExistOrCreate(outputDir);
var coverImagePath = archiveService.GetCoverImage(Path.Join(testDirectory, inputFile),
@ -178,7 +179,7 @@ namespace API.Tests.Services
Assert.Equal(expectedBytes, actual);
_testOutputHelper.WriteLine($"Processed in {sw.ElapsedMilliseconds} ms");
DirectoryService.ClearAndDeleteDirectory(outputDir);
_directoryService.ClearAndDeleteDirectory(outputDir);
}

View file

@ -0,0 +1,143 @@
using System.Collections.Generic;
using System.Data.Common;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Entities;
using API.Entities.Enums;
using API.Extensions;
using API.Services;
using API.Services.Tasks;
using API.SignalR;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services;
public class BackupServiceTests
{
private readonly ILogger<BackupService> _logger = Substitute.For<ILogger<BackupService>>();
private readonly IUnitOfWork _unitOfWork;
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
private readonly IConfiguration _config;
private readonly DbConnection _connection;
private readonly DataContext _context;
private const string CacheDirectory = "C:/kavita/config/cache/";
private const string CoverImageDirectory = "C:/kavita/config/covers/";
private const string BackupDirectory = "C:/kavita/config/backups/";
private const string LogDirectory = "C:/kavita/config/logs/";
public BackupServiceTests()
{
var contextOptions = new DbContextOptionsBuilder()
.UseSqlite(CreateInMemoryDatabase())
.Options;
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
_context = new DataContext(contextOptions);
Task.Run(SeedDb).GetAwaiter().GetResult();
_unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
_config = Substitute.For<IConfiguration>();
}
#region Setup
private static DbConnection CreateInMemoryDatabase()
{
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
return connection;
}
public void Dispose() => _connection.Dispose();
private async Task<bool> SeedDb()
{
await _context.Database.MigrateAsync();
var filesystem = CreateFileSystem();
await Seed.SeedSettings(_context, new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
setting.Value = CacheDirectory;
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
setting.Value = BackupDirectory;
_context.ServerSetting.Update(setting);
_context.Library.Add(new Library()
{
Name = "Manga",
Folders = new List<FolderPath>()
{
new FolderPath()
{
Path = "C:/data/"
}
}
});
return await _context.SaveChangesAsync() > 0;
}
private async Task ResetDB()
{
_context.Series.RemoveRange(_context.Series.ToList());
await _context.SaveChangesAsync();
}
private static MockFileSystem CreateFileSystem()
{
var fileSystem = new MockFileSystem();
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
fileSystem.AddDirectory("C:/kavita/config/");
fileSystem.AddDirectory(CacheDirectory);
fileSystem.AddDirectory(CoverImageDirectory);
fileSystem.AddDirectory(BackupDirectory);
fileSystem.AddDirectory(LogDirectory);
fileSystem.AddDirectory("C:/data/");
return fileSystem;
}
#endregion
#region GetLogFiles
public void GetLogFiles_ExpectAllFiles_NoRollingFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{LogDirectory}kavita.log", new MockFileData(""));
filesystem.AddFile($"{LogDirectory}kavita1.log", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
// You can't mock _config extensions because they are static
_config.GetMaxRollingFiles().Returns(1);
_config.GetLoggingFileName().Returns(ds.FileSystem.Path.Join(LogDirectory, "kavita.log"));
var backupService = new BackupService(_logger, _unitOfWork, ds, _config, _messageHub);
Assert.Single(backupService.GetLogFiles(1, LogDirectory));
}
#endregion
}

View file

@ -1,5 +1,5 @@
using System.IO;
using API.Interfaces.Services;
using System.IO.Abstractions;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
@ -14,7 +14,8 @@ namespace API.Tests.Services
public BookServiceTests()
{
_bookService = new BookService(_logger);
var directoryService = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), new FileSystem());
_bookService = new BookService(_logger, directoryService, new ImageService(Substitute.For<ILogger<ImageService>>(), directoryService));
}
[Theory]

View file

@ -1,115 +1,440 @@
namespace API.Tests.Services
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Entities;
using API.Entities.Enums;
using API.Services;
using API.SignalR;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services
{
public class CacheServiceTests
{
// private readonly CacheService _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);
// }
private readonly ILogger<CacheService> _logger = Substitute.For<ILogger<CacheService>>();
private readonly IUnitOfWork _unitOfWork;
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
private readonly DbConnection _connection;
private readonly DataContext _context;
private const string CacheDirectory = "C:/kavita/config/cache/";
private const string CoverImageDirectory = "C:/kavita/config/covers/";
private const string BackupDirectory = "C:/kavita/config/backups/";
private const string DataDirectory = "C:/data/";
public CacheServiceTests()
{
var contextOptions = new DbContextOptionsBuilder()
.UseSqlite(CreateInMemoryDatabase())
.Options;
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
_context = new DataContext(contextOptions);
Task.Run(SeedDb).GetAwaiter().GetResult();
_unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
}
#region Setup
private static DbConnection CreateInMemoryDatabase()
{
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
return connection;
}
public void Dispose() => _connection.Dispose();
private async Task<bool> SeedDb()
{
await _context.Database.MigrateAsync();
var filesystem = CreateFileSystem();
await Seed.SeedSettings(_context, new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
setting.Value = CacheDirectory;
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
setting.Value = BackupDirectory;
_context.ServerSetting.Update(setting);
_context.Library.Add(new Library()
{
Name = "Manga",
Folders = new List<FolderPath>()
{
new FolderPath()
{
Path = "C:/data/"
}
}
});
return await _context.SaveChangesAsync() > 0;
}
private async Task ResetDB()
{
_context.Series.RemoveRange(_context.Series.ToList());
await _context.SaveChangesAsync();
}
private static MockFileSystem CreateFileSystem()
{
var fileSystem = new MockFileSystem();
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
fileSystem.AddDirectory("C:/kavita/config/");
fileSystem.AddDirectory(CacheDirectory);
fileSystem.AddDirectory(CoverImageDirectory);
fileSystem.AddDirectory(BackupDirectory);
fileSystem.AddDirectory(DataDirectory);
return fileSystem;
}
#endregion
#region Ensure
[Fact]
public async Task Ensure_DirectoryAlreadyExists_DontExtractAnything()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{DataDirectory}Test v1.zip", new MockFileData(""));
filesystem.AddDirectory($"{CacheDirectory}1/");
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
await ResetDB();
var s = DbFactory.Series("Test");
var v = DbFactory.Volume("1");
var c = new Chapter()
{
Number = "1",
Files = new List<MangaFile>()
{
new MangaFile()
{
Format = MangaFormat.Archive,
FilePath = $"{DataDirectory}Test v1.zip",
}
}
};
v.Chapters.Add(c);
s.Volumes.Add(v);
s.LibraryId = 1;
_context.Series.Add(s);
await _context.SaveChangesAsync();
await cleanupService.Ensure(1);
Assert.Empty(ds.GetFiles(filesystem.Path.Join(CacheDirectory, "1"), searchOption:SearchOption.AllDirectories));
}
// [Fact]
// public async void Ensure_ShouldExtractArchive(int chapterId)
// public async Task Ensure_DirectoryAlreadyExists_ExtractsImages()
// {
//
// // CacheDirectory needs to be customized.
// _unitOfWork.VolumeRepository.GetChapterAsync(chapterId).Returns(new Chapter
// // TODO: Figure out a way to test this
// var filesystem = CreateFileSystem();
// filesystem.AddFile($"{DataDirectory}Test v1.zip", new MockFileData(""));
// filesystem.AddDirectory($"{CacheDirectory}1/");
// var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
// var archiveService = Substitute.For<IArchiveService>();
// archiveService.ExtractArchive($"{DataDirectory}Test v1.zip",
// filesystem.Path.Join(CacheDirectory, "1"));
// var cleanupService = new CacheService(_logger, _unitOfWork, ds,
// new ReadingItemService(archiveService, Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
//
// await ResetDB();
// var s = DbFactory.Series("Test");
// var v = DbFactory.Volume("1");
// var c = new Chapter()
// {
// Id = 1,
// Number = "1",
// Files = new List<MangaFile>()
// {
// new MangaFile()
// {
// FilePath = ""
// Format = MangaFormat.Archive,
// FilePath = $"{DataDirectory}Test v1.zip",
// }
// }
// });
//
// await _cacheService.Ensure(1);
//
// var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/CacheService/Archives");
// };
// v.Chapters.Add(c);
// s.Volumes.Add(v);
// s.LibraryId = 1;
// _context.Series.Add(s);
//
// }
//string GetCachedPagePath(Volume volume, int page)
// [Fact]
// //[InlineData("", 0, "")]
// public void GetCachedPagePathTest_Should()
// {
//
// // 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,
// // Pages = 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().GetFilesWithExtension("cache/1/").Returns(new string[] {"pexels-photo-6551949.jpg"});
// // Assert.Equal(expected, _cacheService.GetCachedPagePath(volume, pageNum));
// //Assert.True(true);
// }
// await _context.SaveChangesAsync();
//
// [Fact]
// public void GetOrderedChaptersTest()
// {
// // var files = new List<Chapter>()
// // {
// // new()
// // {
// // Number = "1"
// // },
// // new()
// // {
// // Chapter = 2
// // },
// // new()
// // {
// // Chapter = 0
// // },
// // };
// // var expected = new List<MangaFile>()
// // {
// // new()
// // {
// // Chapter = 1
// // },
// // new()
// // {
// // Chapter = 2
// // },
// // new()
// // {
// // Chapter = 0
// // },
// // };
// // Assert.NotStrictEqual(expected, _cacheService.GetOrderedChapters(files));
// await cleanupService.Ensure(1);
// Assert.Empty(ds.GetFiles(filesystem.Path.Join(CacheDirectory, "1"), searchOption:SearchOption.AllDirectories));
// }
//
#endregion
#region CleanupChapters
[Fact]
public void CleanupChapters_AllFilesShouldBeDeleted()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{CacheDirectory}1/001.jpg", new MockFileData(""));
filesystem.AddFile($"{CacheDirectory}1/002.jpg", new MockFileData(""));
filesystem.AddFile($"{CacheDirectory}3/003.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
cleanupService.CleanupChapters(new []{1, 3});
Assert.Empty(ds.GetFiles(CacheDirectory, searchOption:SearchOption.AllDirectories));
}
#endregion
#region GetCachedEpubFile
[Fact]
public void GetCachedEpubFile_ShouldReturnFirstEpub()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{DataDirectory}1.epub", new MockFileData(""));
filesystem.AddFile($"{DataDirectory}2.epub", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cs = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
var c = new Chapter()
{
Files = new List<MangaFile>()
{
new MangaFile()
{
FilePath = $"{DataDirectory}1.epub"
},
new MangaFile()
{
FilePath = $"{DataDirectory}2.epub"
}
}
};
cs.GetCachedEpubFile(1, c);
Assert.Same($"{DataDirectory}1.epub", cs.GetCachedEpubFile(1, c));
}
#endregion
#region GetCachedPagePath
[Fact]
public void GetCachedPagePath_ReturnNullIfNoFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{DataDirectory}1.zip", new MockFileData(""));
filesystem.AddFile($"{DataDirectory}2.zip", new MockFileData(""));
var c = new Chapter()
{
Id = 1,
Files = new List<MangaFile>()
};
var fileIndex = 0;
foreach (var file in c.Files)
{
for (var i = 0; i < file.Pages - 1; i++)
{
filesystem.AddFile($"{CacheDirectory}1/{fileIndex}/{i+1}.jpg", new MockFileData(""));
}
fileIndex++;
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cs = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
// Flatten to prepare for how GetFullPath expects
ds.Flatten($"{CacheDirectory}1/");
var path = cs.GetCachedPagePath(c, 11);
Assert.Equal(string.Empty, path);
}
[Fact]
public void GetCachedPagePath_GetFileFromFirstFile()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{DataDirectory}1.zip", new MockFileData(""));
filesystem.AddFile($"{DataDirectory}2.zip", new MockFileData(""));
var c = new Chapter()
{
Id = 1,
Files = new List<MangaFile>()
{
new MangaFile()
{
Id = 1,
FilePath = $"{DataDirectory}1.zip",
Pages = 10
},
new MangaFile()
{
Id = 2,
FilePath = $"{DataDirectory}2.zip",
Pages = 5
}
}
};
var fileIndex = 0;
foreach (var file in c.Files)
{
for (var i = 0; i < file.Pages; i++)
{
filesystem.AddFile($"{CacheDirectory}1/00{fileIndex}_00{i+1}.jpg", new MockFileData(""));
}
fileIndex++;
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cs = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
// Flatten to prepare for how GetFullPath expects
ds.Flatten($"{CacheDirectory}1/");
Assert.Equal(ds.FileSystem.Path.GetFullPath($"{CacheDirectory}/1/000_001.jpg"), ds.FileSystem.Path.GetFullPath(cs.GetCachedPagePath(c, 0)));
}
[Fact]
public void GetCachedPagePath_GetLastPageFromSingleFile()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{DataDirectory}1.zip", new MockFileData(""));
var c = new Chapter()
{
Id = 1,
Files = new List<MangaFile>()
{
new MangaFile()
{
Id = 1,
FilePath = $"{DataDirectory}1.zip",
Pages = 10
}
}
};
c.Pages = c.Files.Sum(f => f.Pages);
var fileIndex = 0;
foreach (var file in c.Files)
{
for (var i = 0; i < file.Pages; i++)
{
filesystem.AddFile($"{CacheDirectory}1/{fileIndex}/{i+1}.jpg", new MockFileData(""));
}
fileIndex++;
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cs = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
// Flatten to prepare for how GetFullPath expects
ds.Flatten($"{CacheDirectory}1/");
// Remember that we start at 0, so this is the 10th file
var path = cs.GetCachedPagePath(c, c.Pages);
Assert.Equal(ds.FileSystem.Path.GetFullPath($"{CacheDirectory}/1/000_0{c.Pages}.jpg"), ds.FileSystem.Path.GetFullPath(path));
}
[Fact]
public void GetCachedPagePath_GetFileFromSecondFile()
{
var filesystem = CreateFileSystem();
filesystem.AddDirectory($"{CacheDirectory}1/");
filesystem.AddFile($"{DataDirectory}1.zip", new MockFileData(""));
filesystem.AddFile($"{DataDirectory}2.zip", new MockFileData(""));
var c = new Chapter()
{
Id = 1,
Files = new List<MangaFile>()
{
new MangaFile()
{
Id = 1,
FilePath = $"{DataDirectory}1.zip",
Pages = 10
},
new MangaFile()
{
Id = 2,
FilePath = $"{DataDirectory}2.zip",
Pages = 5
}
}
};
var fileIndex = 0;
foreach (var file in c.Files)
{
for (var i = 0; i < file.Pages; i++)
{
filesystem.AddFile($"{CacheDirectory}1/{fileIndex}/{i+1}.jpg", new MockFileData(""));
}
fileIndex++;
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cs = new CacheService(_logger, _unitOfWork, ds,
new ReadingItemService(Substitute.For<IArchiveService>(), Substitute.For<IBookService>(), Substitute.For<IImageService>(), ds));
// Flatten to prepare for how GetFullPath expects
ds.Flatten($"{CacheDirectory}1/");
// Remember that we start at 0, so this is the page + 1 file
var path = cs.GetCachedPagePath(c, 10);
Assert.Equal(ds.FileSystem.Path.GetFullPath($"{CacheDirectory}/1/001_001.jpg"), ds.FileSystem.Path.GetFullPath(path));
}
#endregion
}
}
}

View file

@ -0,0 +1,359 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Entities;
using API.Entities.Enums;
using API.Services;
using API.Services.Tasks;
using API.SignalR;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services;
public class CleanupServiceTests
{
private readonly ILogger<CleanupService> _logger = Substitute.For<ILogger<CleanupService>>();
private readonly IUnitOfWork _unitOfWork;
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
private readonly DbConnection _connection;
private readonly DataContext _context;
private const string CacheDirectory = "C:/kavita/config/cache/";
private const string CoverImageDirectory = "C:/kavita/config/covers/";
private const string BackupDirectory = "C:/kavita/config/backups/";
public CleanupServiceTests()
{
var contextOptions = new DbContextOptionsBuilder()
.UseSqlite(CreateInMemoryDatabase())
.Options;
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
_context = new DataContext(contextOptions);
Task.Run(SeedDb).GetAwaiter().GetResult();
_unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
}
#region Setup
private static DbConnection CreateInMemoryDatabase()
{
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
return connection;
}
public void Dispose() => _connection.Dispose();
private async Task<bool> SeedDb()
{
await _context.Database.MigrateAsync();
var filesystem = CreateFileSystem();
await Seed.SeedSettings(_context, new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
setting.Value = CacheDirectory;
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
setting.Value = BackupDirectory;
_context.ServerSetting.Update(setting);
_context.Library.Add(new Library()
{
Name = "Manga",
Folders = new List<FolderPath>()
{
new FolderPath()
{
Path = "C:/data/"
}
}
});
return await _context.SaveChangesAsync() > 0;
}
private async Task ResetDB()
{
_context.Series.RemoveRange(_context.Series.ToList());
await _context.SaveChangesAsync();
}
private static MockFileSystem CreateFileSystem()
{
var fileSystem = new MockFileSystem();
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
fileSystem.AddDirectory("C:/kavita/config/");
fileSystem.AddDirectory(CacheDirectory);
fileSystem.AddDirectory(CoverImageDirectory);
fileSystem.AddDirectory(BackupDirectory);
fileSystem.AddDirectory("C:/data/");
return fileSystem;
}
#endregion
#region DeleteSeriesCoverImages
[Fact]
public async Task DeleteSeriesCoverImages_ShouldDeleteAll()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CoverImageDirectory}series_01.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}series_03.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}series_1000.jpg", new MockFileData(""));
// Delete all Series to reset state
await ResetDB();
var s = DbFactory.Series("Test 1");
s.CoverImage = "series_01.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
s = DbFactory.Series("Test 2");
s.CoverImage = "series_03.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
s = DbFactory.Series("Test 3");
s.CoverImage = "series_1000.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
await cleanupService.DeleteSeriesCoverImages();
Assert.Empty(ds.GetFiles(CoverImageDirectory));
}
[Fact]
public async Task DeleteSeriesCoverImages_ShouldNotDeleteLinkedFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CoverImageDirectory}series_01.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}series_03.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}series_1000.jpg", new MockFileData(""));
// Delete all Series to reset state
await ResetDB();
// Add 2 series with cover images
var s = DbFactory.Series("Test 1");
s.CoverImage = "series_01.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
s = DbFactory.Series("Test 2");
s.CoverImage = "series_03.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
await _context.SaveChangesAsync();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
await cleanupService.DeleteSeriesCoverImages();
Assert.Equal(2, ds.GetFiles(CoverImageDirectory).Count());
}
#endregion
#region DeleteChapterCoverImages
[Fact]
public async Task DeleteChapterCoverImages_ShouldNotDeleteLinkedFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CoverImageDirectory}v01_c01.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}v01_c03.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}v01_c1000.jpg", new MockFileData(""));
// Delete all Series to reset state
await ResetDB();
// Add 2 series with cover images
var s = DbFactory.Series("Test 1");
var v = DbFactory.Volume("1");
v.Chapters.Add(new Chapter()
{
CoverImage = "v01_c01.jpg"
});
v.CoverImage = "v01_c01.jpg";
s.Volumes.Add(v);
s.CoverImage = "series_01.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
s = DbFactory.Series("Test 2");
v = DbFactory.Volume("1");
v.Chapters.Add(new Chapter()
{
CoverImage = "v01_c03.jpg"
});
v.CoverImage = "v01_c03jpg";
s.Volumes.Add(v);
s.CoverImage = "series_03.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
await _context.SaveChangesAsync();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
await cleanupService.DeleteChapterCoverImages();
Assert.Equal(2, ds.GetFiles(CoverImageDirectory).Count());
}
#endregion
#region DeleteTagCoverImages
[Fact]
public async Task DeleteTagCoverImages_ShouldNotDeleteLinkedFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CoverImageDirectory}tag_01.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}tag_02.jpg", new MockFileData(""));
filesystem.AddFile($"{CoverImageDirectory}tag_1000.jpg", new MockFileData(""));
// Delete all Series to reset state
await ResetDB();
// Add 2 series with cover images
var s = DbFactory.Series("Test 1");
s.Metadata.CollectionTags = new List<CollectionTag>();
s.Metadata.CollectionTags.Add(new CollectionTag()
{
Title = "Something",
CoverImage ="tag_01.jpg"
});
s.CoverImage = "series_01.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
s = DbFactory.Series("Test 2");
s.Metadata.CollectionTags = new List<CollectionTag>();
s.Metadata.CollectionTags.Add(new CollectionTag()
{
Title = "Something 2",
CoverImage ="tag_02.jpg"
});
s.CoverImage = "series_03.jpg";
s.LibraryId = 1;
_context.Series.Add(s);
await _context.SaveChangesAsync();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
await cleanupService.DeleteTagCoverImages();
Assert.Equal(2, ds.GetFiles(CoverImageDirectory).Count());
}
#endregion
#region CleanupCacheDirectory
[Fact]
public void CleanupCacheDirectory_ClearAllFiles()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CacheDirectory}01.jpg", new MockFileData(""));
filesystem.AddFile($"{CacheDirectory}02.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
cleanupService.CleanupCacheDirectory();
Assert.Empty(ds.GetFiles(CacheDirectory, searchOption: SearchOption.AllDirectories));
}
[Fact]
public void CleanupCacheDirectory_ClearAllFilesInSubDirectory()
{
var filesystem = CreateFileSystem();
filesystem.AddFile($"{CacheDirectory}01.jpg", new MockFileData(""));
filesystem.AddFile($"{CacheDirectory}subdir/02.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
cleanupService.CleanupCacheDirectory();
Assert.Empty(ds.GetFiles(CacheDirectory, searchOption: SearchOption.AllDirectories));
}
#endregion
#region CleanupBackups
[Fact]
public void CleanupBackups_LeaveOneFile_SinceAllAreExpired()
{
var filesystem = CreateFileSystem();
var filesystemFile = new MockFileData("")
{
CreationTime = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(31))
};
filesystem.AddFile($"{BackupDirectory}kavita_backup_11_29_2021_12_00_13 AM.zip", filesystemFile);
filesystem.AddFile($"{BackupDirectory}kavita_backup_12_3_2021_9_27_58 AM.zip", filesystemFile);
filesystem.AddFile($"{BackupDirectory}randomfile.zip", filesystemFile);
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
cleanupService.CleanupBackups();
Assert.Single(ds.GetFiles(BackupDirectory, searchOption: SearchOption.AllDirectories));
}
[Fact]
public void CleanupBackups_LeaveLestExpired()
{
var filesystem = CreateFileSystem();
var filesystemFile = new MockFileData("")
{
CreationTime = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(31))
};
filesystem.AddFile($"{BackupDirectory}kavita_backup_11_29_2021_12_00_13 AM.zip", filesystemFile);
filesystem.AddFile($"{BackupDirectory}kavita_backup_12_3_2021_9_27_58 AM.zip", filesystemFile);
filesystem.AddFile($"{BackupDirectory}randomfile.zip", new MockFileData("")
{
CreationTime = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(14))
});
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem);
var cleanupService = new CleanupService(_logger, _unitOfWork, _messageHub,
ds);
cleanupService.CleanupBackups();
Assert.True(filesystem.File.Exists($"{BackupDirectory}randomfile.zip"));
}
#endregion
}

View file

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
@ -18,87 +21,607 @@ namespace API.Tests.Services
public DirectoryServiceTests()
{
_directoryService = new DirectoryService(_logger, new MockFileSystem());
var filesystem = new MockFileSystem()
{
};
_directoryService = new DirectoryService(_logger, filesystem);
}
#region TraverseTreeParallelForEach
[Fact]
public void GetFilesTest_Should_Be28()
public void TraverseTreeParallelForEach_JustArchives_ShouldBe28()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ScannerService/Manga");
// ReSharper disable once CollectionNeverQueried.Local
var testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 28; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = new List<string>();
var fileCount = _directoryService.TraverseTreeParallelForEach(testDirectory, s => files.Add(s),
var fileCount = ds.TraverseTreeParallelForEach(testDirectory, s => files.Add(s),
API.Parser.Parser.ArchiveFileExtensions, _logger);
Assert.Equal(28, fileCount);
Assert.Equal(28, files.Count);
}
[Fact]
public void GetFiles_WithCustomRegex_ShouldPass_Test()
public void TraverseTreeParallelForEach_DontCountExcludedDirectories_ShouldBe28()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService/regex");
var files = DirectoryService.GetFiles(testDirectory, @"file\d*.txt");
Assert.Equal(2, files.Count());
var testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 28; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{Path.Join(testDirectory, "@eaDir")}file_{29}.jpg", new MockFileData(""));
fileSystem.AddFile($"{Path.Join(testDirectory, ".DS_Store")}file_{30}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = new List<string>();
var fileCount = ds.TraverseTreeParallelForEach(testDirectory, s => files.Add(s),
API.Parser.Parser.ArchiveFileExtensions, _logger);
Assert.Equal(28, fileCount);
Assert.Equal(28, files.Count);
}
#endregion
#region GetFilesWithCertainExtensions
[Fact]
public void GetFilesWithCertainExtensions_ShouldBe10()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFilesWithExtension(testDirectory, API.Parser.Parser.ArchiveFileExtensions);
Assert.Equal(10, files.Length);
Assert.All(files, s => fileSystem.Path.GetExtension(s).Equals(".zip"));
}
[Fact]
public void GetFiles_TopLevel_ShouldBeEmpty_Test()
public void GetFilesWithCertainExtensions_OnlyArchives()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService");
var files = DirectoryService.GetFiles(testDirectory);
Assert.Empty(files);
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}file_{29}.rar", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFilesWithExtension(testDirectory, ".zip|.rar");
Assert.Equal(11, files.Length);
}
#endregion
#region GetFiles
[Fact]
public void GetFiles_ArchiveOnly_ShouldBe10()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory, API.Parser.Parser.ArchiveFileExtensions).ToList();
Assert.Equal(10, files.Count());
Assert.All(files, s => fileSystem.Path.GetExtension(s).Equals(".zip"));
}
[Fact]
public void GetFilesWithExtensions_ShouldBeEmpty_Test()
public void GetFiles_All_ShouldBe11()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService/extensions");
var files = DirectoryService.GetFiles(testDirectory, "*.txt");
Assert.Empty(files);
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory).ToList();
Assert.Equal(11, files.Count());
}
[Fact]
public void GetFilesWithExtensions_Test()
public void GetFiles_All_MixedPathSeparators()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService/extension");
var files = DirectoryService.GetFiles(testDirectory, ".cbz|.rar");
Assert.Equal(3, files.Count());
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"/manga\\file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory).ToList();
Assert.Equal(11, files.Count());
}
[Fact]
public void GetFilesWithExtensions_BadDirectory_ShouldBeEmpty_Test()
public void GetFiles_All_TopDirectoryOnly_ShouldBe10()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService/doesntexist");
var files = DirectoryService.GetFiles(testDirectory, ".cbz|.rar");
Assert.Empty(files);
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}/SubDir/file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory).ToList();
Assert.Equal(10, files.Count());
}
[Fact]
public void ListDirectory_SubDirectory_Test()
public void GetFiles_WithSubDirectories_ShouldCountOnlyTopLevel()
{
var testDirectory = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/DirectoryService/");
var dirs = _directoryService.ListDirectory(testDirectory);
Assert.Contains(dirs, s => s.Contains("regex"));
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}/SubDir/file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory).ToList();
Assert.Equal(10, files.Count());
}
[Fact]
public void ListDirectory_NoSubDirectory_Test()
public void GetFiles_ShouldNotReturnFilesThatAreExcluded()
{
var dirs = _directoryService.ListDirectory("");
Assert.DoesNotContain(dirs, s => s.Contains("regex"));
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}/._file_{29}.jpg", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory).ToList();
Assert.Equal(10, files.Count());
}
[Fact]
public void GetFiles_WithCustomRegex_ShouldBe10()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}data-{i}.txt", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}joe.txt", new MockFileData(""));
fileSystem.AddFile($"{testDirectory}0d.txt", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory, @".*d.*\.txt");
Assert.Equal(11, files.Count());
}
[Fact]
public void GetFiles_WithCustomRegexThatContainsFolder_ShouldBe10()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file/data-{i}.txt", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}joe.txt", new MockFileData(""));
fileSystem.AddFile($"{testDirectory}0d.txt", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var files = ds.GetFiles(testDirectory, @".*d.*\.txt", SearchOption.AllDirectories);
Assert.Equal(11, files.Count());
}
#endregion
#region GetTotalSize
[Fact]
public void GetTotalSize_ShouldBeGreaterThan0()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file/data-{i}.txt", new MockFileData("abc"));
}
fileSystem.AddFile($"{testDirectory}joe.txt", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var fileSize = ds.GetTotalSize(fileSystem.AllFiles);
Assert.True(fileSize > 0);
}
#endregion
#region CopyFileToDirectory
[Fact]
public void CopyFileToDirectory_ShouldCopyFileToNonExistentDirectory()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFileToDirectory($"{testDirectory}file/data-0.txt", "/manga/output/");
Assert.True(fileSystem.FileExists("manga/output/data-0.txt"));
Assert.True(fileSystem.FileExists("manga/file/data-0.txt"));
}
[Fact]
public void CopyFileToDirectory_ShouldCopyFileToExistingDirectoryAndOverwrite()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}output/data-0.txt", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFileToDirectory($"{testDirectory}file/data-0.txt", "/manga/output/");
Assert.True(fileSystem.FileExists("/manga/output/data-0.txt"));
Assert.True(fileSystem.FileExists("/manga/file/data-0.txt"));
Assert.True(fileSystem.FileInfo.FromFileName("/manga/file/data-0.txt").Length == fileSystem.FileInfo.FromFileName("/manga/output/data-0.txt").Length);
}
#endregion
#region CopyDirectoryToDirectory
[Fact]
public void CopyDirectoryToDirectory_ShouldThrowWhenSourceDestinationDoesntExist()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}output/data-0.txt", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var ex = Assert.Throws<DirectoryNotFoundException>(() => ds.CopyDirectoryToDirectory("/comics/", "/manga/output/"));
Assert.Equal(ex.Message, "Source directory does not exist or could not be found: " + "/comics/");
}
[Fact]
public void CopyDirectoryToDirectory_ShouldCopyEmptyDirectory()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
fileSystem.AddDirectory($"{testDirectory}empty/");
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyDirectoryToDirectory($"{testDirectory}empty/", "/manga/output/");
Assert.Empty(fileSystem.DirectoryInfo.FromDirectoryName("/manga/output/").GetFiles());
}
[Fact]
public void CopyDirectoryToDirectory_ShouldCopyAllFileAndNestedDirectoriesOver()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-1.txt", new MockFileData("abc"));
fileSystem.AddDirectory($"{testDirectory}empty/");
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyDirectoryToDirectory($"{testDirectory}", "/manga/output/");
Assert.Equal(2, ds.GetFiles("/manga/output/", searchOption: SearchOption.AllDirectories).Count());
}
#endregion
#region IsDriveMounted
[Fact]
public void IsDriveMounted_DriveIsNotMounted()
{
const string testDirectory = "c:/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}data-0.txt", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.False(ds.IsDriveMounted("d:/manga/"));
}
[Fact]
public void IsDriveMounted_DriveIsMounted()
{
const string testDirectory = "c:/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}data-0.txt", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.True(ds.IsDriveMounted("c:/manga/file"));
}
#endregion
#region ExistOrCreate
[Fact]
public void ExistOrCreate_ShouldCreate()
{
var fileSystem = new MockFileSystem();
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.ExistOrCreate("c:/manga/output/");
Assert.True(ds.FileSystem.DirectoryInfo.FromDirectoryName("c:/manga/output/").Exists);
}
#endregion
#region ClearAndDeleteDirectory
[Fact]
public void ClearAndDeleteDirectory_ShouldDeleteSelfAndAllFilesAndFolders()
{
const string testDirectory = "/manga/base/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file/data-{i}.txt", new MockFileData("abc"));
}
fileSystem.AddFile($"{testDirectory}data-a.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-b.txt", new MockFileData("abc"));
fileSystem.AddDirectory($"{testDirectory}empty/");
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.ClearAndDeleteDirectory($"{testDirectory}");
Assert.Empty(ds.GetFiles("/manga/", searchOption: SearchOption.AllDirectories));
Assert.Empty(ds.FileSystem.DirectoryInfo.FromDirectoryName("/manga/").GetDirectories());
Assert.True(ds.FileSystem.DirectoryInfo.FromDirectoryName("/manga/").Exists);
Assert.False(ds.FileSystem.DirectoryInfo.FromDirectoryName("/manga/base").Exists);
}
#endregion
#region ClearDirectory
[Fact]
public void ClearDirectory_ShouldDeleteAllFilesAndFolders_LeaveSelf()
{
const string testDirectory = "/manga/base/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file/data-{i}.txt", new MockFileData("abc"));
}
fileSystem.AddFile($"{testDirectory}data-a.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-b.txt", new MockFileData("abc"));
fileSystem.AddDirectory($"{testDirectory}file/empty/");
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.ClearDirectory($"{testDirectory}file/");
Assert.Empty(ds.FileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}file/").GetDirectories());
Assert.True(ds.FileSystem.DirectoryInfo.FromDirectoryName("/manga/").Exists);
Assert.True(ds.FileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}file/").Exists);
}
[Fact]
public void ClearDirectory_ShouldDeleteFoldersWithOneFileInside()
{
const string testDirectory = "/manga/base/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file/data-{i}.txt", new MockFileData("abc"));
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.ClearDirectory($"{testDirectory}");
Assert.Empty(ds.FileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}").GetDirectories());
Assert.True(ds.FileSystem.DirectoryInfo.FromDirectoryName(testDirectory).Exists);
Assert.False(ds.FileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}file/").Exists);
}
#endregion
#region CopyFilesToDirectory
[Fact]
public void CopyFilesToDirectory_ShouldMoveAllFiles()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFilesToDirectory(new []{$"{testDirectory}file_{0}.zip", $"{testDirectory}file_{1}.zip"}, "/manga/output/");
Assert.Equal(2, ds.GetFiles("/manga/output/").Count());
}
[Fact]
public void CopyFilesToDirectory_ShouldMoveAllFiles_InclFilesInNestedFolders()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
fileSystem.AddFile($"{testDirectory}nested/file_11.zip", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFilesToDirectory(new []{$"{testDirectory}file_{0}.zip", $"{testDirectory}file_{1}.zip", $"{testDirectory}nested/file_11.zip"}, "/manga/output/");
Assert.Equal(3, ds.GetFiles("/manga/output/").Count());
}
[Fact]
public void CopyFilesToDirectory_ShouldMoveAllFiles_WithPrepend()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFilesToDirectory(new []{$"{testDirectory}file_{0}.zip", $"{testDirectory}file_{1}.zip", $"{testDirectory}nested/file_11.zip"},
"/manga/output/", "mangarocks_");
Assert.Equal(2, ds.GetFiles("/manga/output/").Count());
Assert.All(ds.GetFiles("/manga/output/"), filepath => ds.FileSystem.Path.GetFileName(filepath).StartsWith("mangarocks_"));
}
[Fact]
public void CopyFilesToDirectory_ShouldMoveOnlyFilesThatExist()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
for (var i = 0; i < 10; i++)
{
fileSystem.AddFile($"{testDirectory}file_{i}.zip", new MockFileData(""));
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.CopyFilesToDirectory(new []{$"{testDirectory}file_{0}.zip", $"{testDirectory}file_{1}.zip", $"{testDirectory}nested/file_11.zip"},
"/manga/output/");
Assert.Equal(2, ds.GetFiles("/manga/output/").Count());
}
#endregion
#region ListDirectory
[Fact]
public void ListDirectory_EmptyForNonExistent()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file_0.zip", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.Empty(ds.ListDirectory("/comics/"));
}
[Fact]
public void ListDirectory_ListsAllDirectories()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory($"{testDirectory}dir1");
fileSystem.AddDirectory($"{testDirectory}dir2");
fileSystem.AddDirectory($"{testDirectory}dir3");
fileSystem.AddFile($"{testDirectory}file_0.zip", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.Equal(3, ds.ListDirectory(testDirectory).Count());
}
[Fact]
public void ListDirectory_ListsOnlyNonSystemAndHiddenOnly()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory($"{testDirectory}dir1");
var di = fileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}dir1");
di.Attributes |= FileAttributes.System;
fileSystem.AddDirectory($"{testDirectory}dir2");
di = fileSystem.DirectoryInfo.FromDirectoryName($"{testDirectory}dir2");
di.Attributes |= FileAttributes.Hidden;
fileSystem.AddDirectory($"{testDirectory}dir3");
fileSystem.AddFile($"{testDirectory}file_0.zip", new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
Assert.Equal(1, ds.ListDirectory(testDirectory).Count());
}
#endregion
#region ReadFileAsync
[Fact]
public async Task ReadFileAsync_ShouldGetBytes()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file_1.zip", new MockFileData("Hello"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var bytes = await ds.ReadFileAsync($"{testDirectory}file_1.zip");
Assert.Equal(Encoding.UTF8.GetBytes("Hello"), bytes);
}
[Fact]
public async Task ReadFileAsync_ShouldReadNothingFromNonExistent()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddFile($"{testDirectory}file_1.zip", new MockFileData("Hello"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var bytes = await ds.ReadFileAsync($"{testDirectory}file_32123.zip");
Assert.Empty(bytes);
}
#endregion
#region FindHighestDirectoriesFromFiles
[Theory]
[InlineData(new [] {"C:/Manga/"}, new [] {"C:/Manga/Love Hina/Vol. 01.cbz"}, "C:/Manga/Love Hina")]
public void FindHighestDirectoriesFromFilesTest(string[] rootDirectories, string[] folders, string expectedDirectory)
[InlineData(new [] {"C:/Manga/Dir 1/", "c://Manga/Dir 2/"}, new [] {"C:/Manga/Dir 1/Love Hina/Vol. 01.cbz"}, "C:/Manga/Dir 1/Love Hina")]
[InlineData(new [] {"C:/Manga/Dir 1/", "c://Manga/"}, new [] {"D:/Manga/Love Hina/Vol. 01.cbz", "D:/Manga/Vol. 01.cbz"}, "")]
public void FindHighestDirectoriesFromFilesTest(string[] rootDirectories, string[] files, string expectedDirectory)
{
var actual = DirectoryService.FindHighestDirectoriesFromFiles(rootDirectories, folders);
var expected = new Dictionary<string, string> {{expectedDirectory, ""}};
var fileSystem = new MockFileSystem();
foreach (var directory in rootDirectories)
{
fileSystem.AddDirectory(directory);
}
foreach (var f in files)
{
fileSystem.AddFile(f, new MockFileData(""));
}
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var actual = ds.FindHighestDirectoriesFromFiles(rootDirectories, files);
var expected = new Dictionary<string, string>();
if (!string.IsNullOrEmpty(expectedDirectory))
{
expected = new Dictionary<string, string> {{expectedDirectory, ""}};
}
Assert.Equal(expected, actual);
}
#endregion
#region GetFoldersTillRoot
[Theory]
[InlineData("C:/Manga/", "C:/Manga/Love Hina/Specials/Omake/", "Omake,Specials,Love Hina")]
[InlineData("C:/Manga/", "C:/Manga/Love Hina/Specials/Omake", "Omake,Specials,Love Hina")]
@ -115,12 +638,78 @@ namespace API.Tests.Services
[InlineData(@"M:\", @"M:\Toukyou Akazukin\Vol. 01 Ch. 005.cbz", @"Toukyou Akazukin")]
public void GetFoldersTillRoot_Test(string rootPath, string fullpath, string expectedArray)
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(rootPath);
fileSystem.AddFile(fullpath, new MockFileData(""));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
var expected = expectedArray.Split(",");
if (expectedArray.Equals(string.Empty))
{
expected = Array.Empty<string>();
expected = Array.Empty<string>();
}
Assert.Equal(expected, DirectoryService.GetFoldersTillRoot(rootPath, fullpath));
Assert.Equal(expected, ds.GetFoldersTillRoot(rootPath, fullpath));
}
#endregion
#region RemoveNonImages
[Fact]
public void RemoveNonImages()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(testDirectory);
fileSystem.AddFile($"{testDirectory}file/data-0.txt", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-1.jpg", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-2.png", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-3.webp", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.RemoveNonImages($"{testDirectory}");
Assert.False(fileSystem.FileExists($"{testDirectory}file/data-0.txt"));
Assert.Equal(3, ds.GetFiles($"{testDirectory}", searchOption:SearchOption.AllDirectories).Count());
}
#endregion
#region Flatten
[Fact]
public void Flatten_ShouldDoNothing()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(testDirectory);
fileSystem.AddFile($"{testDirectory}data-1.jpg", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-2.png", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}data-3.webp", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.Flatten($"{testDirectory}");
Assert.True(fileSystem.FileExists($"{testDirectory}data-1.jpg"));
Assert.True(fileSystem.FileExists($"{testDirectory}data-2.png"));
Assert.True(fileSystem.FileExists($"{testDirectory}data-3.webp"));
}
[Fact]
public void Flatten_ShouldFlatten()
{
const string testDirectory = "/manga/";
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory(testDirectory);
fileSystem.AddFile($"{testDirectory}data-1.jpg", new MockFileData("abc"));
fileSystem.AddFile($"{testDirectory}subdir/data-3.webp", new MockFileData("abc"));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), fileSystem);
ds.Flatten($"{testDirectory}");
Assert.Equal(2, ds.GetFiles(testDirectory).Count());
Assert.False(fileSystem.FileExists($"{testDirectory}subdir/data-3.webp"));
Assert.True(fileSystem.Directory.Exists($"{testDirectory}subdir/"));
}
#endregion
}
}

View file

@ -0,0 +1,164 @@
using System.Collections.Generic;
using System.Data.Common;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.Data.Metadata;
using API.Entities;
using API.Entities.Enums;
using API.Parser;
using API.Services;
using API.Services.Tasks.Scanner;
using API.SignalR;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Services;
internal class MockReadingItemService : IReadingItemService
{
public ComicInfo GetComicInfo(string filePath, MangaFormat format)
{
throw new System.NotImplementedException();
}
public int GetNumberOfPages(string filePath, MangaFormat format)
{
throw new System.NotImplementedException();
}
public string GetCoverImage(string fileFilePath, string fileName, MangaFormat format)
{
throw new System.NotImplementedException();
}
public void Extract(string fileFilePath, string targetDirectory, MangaFormat format, int imageCount = 1)
{
throw new System.NotImplementedException();
}
public ParserInfo Parse(string path, string rootPath, LibraryType type)
{
throw new System.NotImplementedException();
}
}
public class ParseScannedFilesTests
{
private readonly ILogger<ParseScannedFiles> _logger = Substitute.For<ILogger<ParseScannedFiles>>();
private readonly IUnitOfWork _unitOfWork;
private readonly DbConnection _connection;
private readonly DataContext _context;
private const string CacheDirectory = "C:/kavita/config/cache/";
private const string CoverImageDirectory = "C:/kavita/config/covers/";
private const string BackupDirectory = "C:/kavita/config/backups/";
private const string DataDirectory = "C:/data/";
public ParseScannedFilesTests()
{
var contextOptions = new DbContextOptionsBuilder()
.UseSqlite(CreateInMemoryDatabase())
.Options;
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
_context = new DataContext(contextOptions);
Task.Run(SeedDb).GetAwaiter().GetResult();
_unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
// Since ProcessFile relies on _readingItemService, we can implement our own versions of _readingItemService so we have control over how the calls work
}
#region Setup
private static DbConnection CreateInMemoryDatabase()
{
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
return connection;
}
public void Dispose() => _connection.Dispose();
private async Task<bool> SeedDb()
{
await _context.Database.MigrateAsync();
var filesystem = CreateFileSystem();
await Seed.SeedSettings(_context, new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), filesystem));
var setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.CacheDirectory).SingleAsync();
setting.Value = CacheDirectory;
setting = await _context.ServerSetting.Where(s => s.Key == ServerSettingKey.BackupDirectory).SingleAsync();
setting.Value = BackupDirectory;
_context.ServerSetting.Update(setting);
_context.Library.Add(new Library()
{
Name = "Manga",
Folders = new List<FolderPath>()
{
new FolderPath()
{
Path = DataDirectory
}
}
});
return await _context.SaveChangesAsync() > 0;
}
private async Task ResetDB()
{
_context.Series.RemoveRange(_context.Series.ToList());
await _context.SaveChangesAsync();
}
private static MockFileSystem CreateFileSystem()
{
var fileSystem = new MockFileSystem();
fileSystem.Directory.SetCurrentDirectory("C:/kavita/");
fileSystem.AddDirectory("C:/kavita/config/");
fileSystem.AddDirectory(CacheDirectory);
fileSystem.AddDirectory(CoverImageDirectory);
fileSystem.AddDirectory(BackupDirectory);
fileSystem.AddDirectory(DataDirectory);
return fileSystem;
}
#endregion
#region GetInfosByName
[Fact]
public void GetInfosByName()
{
}
#endregion
#region MergeName
[Fact]
public void MergeName_()
{
}
#endregion
}

View file

@ -11,8 +11,6 @@ using API.Entities;
using API.Entities.Enums;
using API.Entities.Metadata;
using API.Helpers;
using API.Interfaces;
using API.Interfaces.Services;
using API.Parser;
using API.Services;
using API.Services.Tasks;
@ -29,76 +27,8 @@ using Xunit;
namespace API.Tests.Services
{
public class ScannerServiceTests : IDisposable
public class ScannerServiceTests
{
private readonly ScannerService _scannerService;
private readonly ILogger<ScannerService> _logger = Substitute.For<ILogger<ScannerService>>();
private readonly IArchiveService _archiveService = Substitute.For<IArchiveService>();
private readonly IBookService _bookService = Substitute.For<IBookService>();
private readonly IImageService _imageService = Substitute.For<IImageService>();
private readonly IDirectoryService _directoryService = Substitute.For<IDirectoryService>();
private readonly ILogger<MetadataService> _metadataLogger = Substitute.For<ILogger<MetadataService>>();
private readonly ICacheService _cacheService;
private readonly IHubContext<MessageHub> _messageHub = Substitute.For<IHubContext<MessageHub>>();
private readonly DbConnection _connection;
private readonly DataContext _context;
public ScannerServiceTests()
{
var contextOptions = new DbContextOptionsBuilder()
.UseSqlite(CreateInMemoryDatabase())
.Options;
_connection = RelationalOptionsExtension.Extract(contextOptions).Connection;
_context = new DataContext(contextOptions);
Task.Run(SeedDb).GetAwaiter().GetResult();
IUnitOfWork unitOfWork = new UnitOfWork(_context, Substitute.For<IMapper>(), null);
var file = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now.Subtract(TimeSpan.FromMinutes(1))
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "/data/Darker than Black.zip", file },
{ "/data/Cage of Eden - v10.cbz", file },
{ "/data/Cage of Eden - v1.cbz", file },
});
var fileService = new FileService(fileSystem);
ICacheHelper cacheHelper = new CacheHelper(fileService);
IMetadataService metadataService =
Substitute.For<MetadataService>(unitOfWork, _metadataLogger, _archiveService,
_bookService, _imageService, _messageHub, cacheHelper);
_scannerService = new ScannerService(unitOfWork, _logger, _archiveService, metadataService, _bookService,
_cacheService, _messageHub, fileService, _directoryService);
}
private async Task<bool> SeedDb()
{
await _context.Database.MigrateAsync();
await Seed.SeedSettings(_context);
_context.Library.Add(new Library()
{
Name = "Manga",
Folders = new List<FolderPath>()
{
new FolderPath()
{
Path = Path.Join(Directory.GetCurrentDirectory(), "../../../Services/Test Data/ScannerService/Manga")
}
}
});
return await _context.SaveChangesAsync() > 0;
}
[Fact]
public void AddOrUpdateFileForChapter()
{
@ -227,16 +157,5 @@ namespace API.Tests.Services
}
}
private static DbConnection CreateInMemoryDatabase()
{
var connection = new SqliteConnection("Filename=:memory:");
connection.Open();
return connection;
}
public void Dispose() => _connection.Dispose();
}
}