Skip to content

Commit

Permalink
Comic Rework (Part 1) (#2772)
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 authored Mar 9, 2024
1 parent 58c77b3 commit fc21073
Show file tree
Hide file tree
Showing 69 changed files with 5,085 additions and 698 deletions.
9 changes: 4 additions & 5 deletions API.Tests/Extensions/ParserInfoListExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ public class ParserInfoListExtensions
private readonly IDefaultParser _defaultParser;
public ParserInfoListExtensions()
{
_defaultParser =
new DefaultParser(new DirectoryService(Substitute.For<ILogger<DirectoryService>>(),
new MockFileSystem()));
var ds = new DirectoryService(Substitute.For<ILogger<DirectoryService>>(), new MockFileSystem());
_defaultParser = new BasicParser(ds, new ImageParser(ds));
}

[Theory]
Expand All @@ -43,7 +42,7 @@ public void HasInfoTest(string[] inputInfos, string[] inputChapters, bool expect
{
infos.Add(_defaultParser.Parse(
Path.Join("E:/Manga/Cynthia the Mission/", filename),
"E:/Manga/"));
"E:/Manga/", "E:/Manga/", LibraryType.Manga));
}

var files = inputChapters.Select(s => new MangaFileBuilder(s, MangaFormat.Archive, 199).Build()).ToList();
Expand All @@ -61,7 +60,7 @@ public void HasInfoTest_SuccessWhenSpecial()
{
_defaultParser.Parse(
"E:/Manga/Cynthia the Mission/Cynthia The Mission The Special SP01 [Desudesu&Brolen].zip",
"E:/Manga/")
"E:/Manga/", "E:/Manga/", LibraryType.Manga)
};

var files = new[] {@"E:\Manga\Cynthia the Mission\Cynthia The Mission The Special SP01 [Desudesu&Brolen].zip"}
Expand Down
217 changes: 217 additions & 0 deletions API.Tests/Parsers/BasicParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
using System.IO.Abstractions.TestingHelpers;
using API.Entities.Enums;
using API.Services;
using API.Services.Tasks.Scanner.Parser;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

namespace API.Tests.Parsers;

public class BasicParserTests
{
private readonly BasicParser _parser;
private readonly ILogger<DirectoryService> _dsLogger = Substitute.For<ILogger<DirectoryService>>();
private const string RootDirectory = "C:/Books/";

public BasicParserTests()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory("C:/Books/");
fileSystem.AddFile("C:/Books/Harry Potter/Harry Potter - Vol 1.epub", new MockFileData(""));

fileSystem.AddFile("C:/Books/Accel World/Accel World - Volume 1.cbz", new MockFileData(""));
fileSystem.AddFile("C:/Books/Accel World/Accel World - Volume 1 Chapter 2.cbz", new MockFileData(""));
fileSystem.AddFile("C:/Books/Accel World/Accel World - Chapter 3.cbz", new MockFileData(""));
fileSystem.AddFile("C:/Books/Accel World/Accel World Gaiden SP01.cbz", new MockFileData(""));


fileSystem.AddFile("C:/Books/Accel World/cover.png", new MockFileData(""));

fileSystem.AddFile("C:/Books/Batman/Batman #1.cbz", new MockFileData(""));

var ds = new DirectoryService(_dsLogger, fileSystem);
_parser = new BasicParser(ds, new ImageParser(ds));
}

#region Parse_Books



#endregion

#region Parse_Manga

/// <summary>
/// Tests that when there is a loose leaf cover in the manga library, that it is ignored
/// </summary>
[Fact]
public void Parse_MangaLibrary_JustCover_ShouldReturnNull()
{
var actual = _parser.Parse(@"C:/Books/Accel World/cover.png", "C:/Books/Accel World/",
RootDirectory, LibraryType.Manga, null);
Assert.Null(actual);
}

/// <summary>
/// Tests that when there is a loose leaf cover in the manga library, that it is ignored
/// </summary>
[Fact]
public void Parse_MangaLibrary_OtherImage_ShouldReturnNull()
{
var actual = _parser.Parse(@"C:/Books/Accel World/page 01.png", "C:/Books/Accel World/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);
}

/// <summary>
/// Tests that when there is a volume and chapter in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_VolumeAndChapterInFilename()
{
var actual = _parser.Parse("C:/Books/Mujaki no Rakuen/Mujaki no Rakuen Vol12 ch76.cbz", "C:/Books/Mujaki no Rakuen/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Mujaki no Rakuen", actual.Series);
Assert.Equal("12", actual.Volumes);
Assert.Equal("76", actual.Chapters);
Assert.False(actual.IsSpecial);
}

/// <summary>
/// Tests that when there is a volume in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_JustVolumeInFilename()
{
var actual = _parser.Parse("C:/Books/Shimoneta to Iu Gainen ga Sonzai Shinai Taikutsu na Sekai Man-hen/Vol 1.cbz",
"C:/Books/Shimoneta to Iu Gainen ga Sonzai Shinai Taikutsu na Sekai Man-hen/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Shimoneta to Iu Gainen ga Sonzai Shinai Taikutsu na Sekai Man-hen", actual.Series);
Assert.Equal("1", actual.Volumes);
Assert.Equal(Parser.DefaultChapter, actual.Chapters);
Assert.False(actual.IsSpecial);
}

/// <summary>
/// Tests that when there is a chapter only in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_JustChapterInFilename()
{
var actual = _parser.Parse("C:/Books/Beelzebub/Beelzebub_01_[Noodles].zip",
"C:/Books/Beelzebub/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Beelzebub", actual.Series);
Assert.Equal(Parser.LooseLeafVolume, actual.Volumes);
Assert.Equal("1", actual.Chapters);
Assert.False(actual.IsSpecial);
}

/// <summary>
/// Tests that when there is a SP Marker in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_SpecialMarkerInFilename()
{
var actual = _parser.Parse("C:/Books/Summer Time Rendering/Specials/Record 014 (between chapter 083 and ch084) SP11.cbr",
"C:/Books/Summer Time Rendering/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Summer Time Rendering", actual.Series);
Assert.Equal(Parser.SpecialVolume, actual.Volumes);
Assert.Equal(Parser.DefaultChapter, actual.Chapters);
Assert.True(actual.IsSpecial);
}


/// <summary>
/// Tests that when the filename parses as a speical, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_SpecialInFilename()
{
var actual = _parser.Parse("C:/Books/Summer Time Rendering/Specials/Volume Omake.cbr",
"C:/Books/Summer Time Rendering/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Summer Time Rendering", actual.Series);
Assert.Equal("Volume Omake", actual.Title);
Assert.Equal(Parser.SpecialVolume, actual.Volumes);
Assert.Equal(Parser.DefaultChapter, actual.Chapters);
Assert.True(actual.IsSpecial);
}

/// <summary>
/// Tests that when there is an edition in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaLibrary_EditionInFilename()
{
var actual = _parser.Parse("C:/Books/Air Gear/Air Gear Omnibus v01 (2016) (Digital) (Shadowcat-Empire).cbz",
"C:/Books/Air Gear/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Air Gear", actual.Series);
Assert.Equal("1", actual.Volumes);
Assert.Equal(Parser.DefaultChapter, actual.Chapters);
Assert.False(actual.IsSpecial);
Assert.Equal("Omnibus", actual.Edition);
}

#endregion

#region Parse_Books
/// <summary>
/// Tests that when there is a volume in filename, it appropriately parses
/// </summary>
[Fact]
public void Parse_MangaBooks_JustVolumeInFilename()
{
var actual = _parser.Parse("C:/Books/Epubs/Harrison, Kim - The Good, The Bad, and the Undead - Hollows Vol 2.5.epub",
"C:/Books/Epubs/",
RootDirectory, LibraryType.Manga, null);
Assert.NotNull(actual);

Assert.Equal("Harrison, Kim - The Good, The Bad, and the Undead - Hollows", actual.Series);
Assert.Equal("2.5", actual.Volumes);
Assert.Equal(Parser.DefaultChapter, actual.Chapters);
}

#endregion

#region IsApplicable
/// <summary>
/// Tests that this Parser can only be used on images and Image library type
/// </summary>
[Fact]
public void IsApplicable_Fails_WhenNonMatchingLibraryType()
{
Assert.False(_parser.IsApplicable("something.cbz", LibraryType.Image));
Assert.False(_parser.IsApplicable("something.cbz", LibraryType.ComicVine));
}

/// <summary>
/// Tests that this Parser can only be used on images and Image library type
/// </summary>
[Fact]
public void IsApplicable_Success_WhenMatchingLibraryType()
{
Assert.True(_parser.IsApplicable("something.png", LibraryType.Manga));
Assert.True(_parser.IsApplicable("something.png", LibraryType.Comic));
Assert.True(_parser.IsApplicable("something.pdf", LibraryType.Book));
Assert.True(_parser.IsApplicable("something.epub", LibraryType.LightNovel));
}


#endregion
}
74 changes: 74 additions & 0 deletions API.Tests/Parsers/BookParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.IO.Abstractions.TestingHelpers;
using API.Data.Metadata;
using API.Entities.Enums;
using API.Services;
using API.Services.Tasks.Scanner.Parser;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

namespace API.Tests.Parsers;

public class BookParserTests
{
private readonly BookParser _parser;
private readonly ILogger<DirectoryService> _dsLogger = Substitute.For<ILogger<DirectoryService>>();
private const string RootDirectory = "C:/Books/";

public BookParserTests()
{
var fileSystem = new MockFileSystem();
fileSystem.AddDirectory("C:/Books/");
fileSystem.AddFile("C:/Books/Harry Potter/Harry Potter - Vol 1.epub", new MockFileData(""));
fileSystem.AddFile("C:/Books/Adam Freeman - Pro ASP.NET Core 6.epub", new MockFileData(""));
fileSystem.AddFile("C:/Books/My Fav Book SP01.epub", new MockFileData(""));
var ds = new DirectoryService(_dsLogger, fileSystem);
_parser = new BookParser(ds, Substitute.For<IBookService>(), new BasicParser(ds, new ImageParser(ds)));
}

#region Parse

// TODO: I'm not sure how to actually test this as it relies on an epub parser to actually do anything

/// <summary>
/// Tests that if there is a Series Folder then Chapter folder, the code appropriately identifies the Series name and Chapter
/// </summary>
// [Fact]
// public void Parse_SeriesWithDirectoryName()
// {
// var actual = _parser.Parse("C:/Books/Harry Potter/Harry Potter - Vol 1.epub", "C:/Books/Birds of Prey/",
// RootDirectory, LibraryType.Book, new ComicInfo()
// {
// Series = "Harry Potter",
// Volume = "1"
// });
//
// Assert.NotNull(actual);
// Assert.Equal("Harry Potter", actual.Series);
// Assert.Equal("1", actual.Volumes);
// }

#endregion

#region IsApplicable
/// <summary>
/// Tests that this Parser can only be used on images and Image library type
/// </summary>
[Fact]
public void IsApplicable_Fails_WhenNonMatchingLibraryType()
{
Assert.False(_parser.IsApplicable("something.cbz", LibraryType.Manga));
Assert.False(_parser.IsApplicable("something.cbz", LibraryType.Book));

}

/// <summary>
/// Tests that this Parser can only be used on images and Image library type
/// </summary>
[Fact]
public void IsApplicable_Success_WhenMatchingLibraryType()
{
Assert.True(_parser.IsApplicable("something.epub", LibraryType.Image));
}
#endregion
}
Loading

0 comments on commit fc21073

Please sign in to comment.