Misc Fixes (#914)

* Fixed the book reader off by one issue with loading last page

* Fixed a case where scanner would not delete a series if another series with same name but different format was added in that same scan.

* Added some missing tag generation (chapter language and summary)
This commit is contained in:
Joseph Milazzo 2022-01-08 11:36:47 -08:00 committed by GitHub
parent 1557c2f528
commit c5e5aa19d5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 232 additions and 110 deletions

View file

@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using API.Entities;
using API.Helpers;
using API.Services;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;
namespace API.Tests.Helpers;

View file

@ -1,6 +1,10 @@
using System.IO;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using API.Entities.Enums;
using API.Parser;
using API.Services.Tasks.Scanner;
namespace API.Tests.Helpers
{
@ -21,5 +25,49 @@ namespace API.Tests.Helpers
Volumes = volumes
};
}
public static void AddToParsedInfo(IDictionary<ParsedSeries, List<ParserInfo>> collectedSeries, ParserInfo info)
{
var existingKey = collectedSeries.Keys.FirstOrDefault(ps =>
ps.Format == info.Format && ps.NormalizedName == API.Parser.Parser.Normalize(info.Series));
existingKey ??= new ParsedSeries()
{
Format = info.Format,
Name = info.Series,
NormalizedName = API.Parser.Parser.Normalize(info.Series)
};
if (collectedSeries.GetType() == typeof(ConcurrentDictionary<,>))
{
((ConcurrentDictionary<ParsedSeries, List<ParserInfo>>) collectedSeries).AddOrUpdate(existingKey, new List<ParserInfo>() {info}, (_, oldValue) =>
{
oldValue ??= new List<ParserInfo>();
if (!oldValue.Contains(info))
{
oldValue.Add(info);
}
return oldValue;
});
}
else
{
if (!collectedSeries.ContainsKey(existingKey))
{
collectedSeries.Add(existingKey, new List<ParserInfo>() {info});
}
else
{
var list = collectedSeries[existingKey];
if (!list.Contains(info))
{
list.Add(info);
}
collectedSeries[existingKey] = list;
}
}
}
}
}
}

View file

@ -0,0 +1,75 @@
using System.Collections.Generic;
using API.Entities;
using API.Entities.Enums;
using API.Entities.Metadata;
using API.Helpers;
using API.Parser;
using API.Services.Tasks.Scanner;
using Xunit;
namespace API.Tests.Helpers;
public class ParserInfoHelperTests
{
#region SeriesHasMatchingParserInfoFormat
[Fact]
public void SeriesHasMatchingParserInfoFormat_ShouldBeFalse()
{
var infos = new Dictionary<ParsedSeries, List<ParserInfo>>();
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Archive});
//AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Epub});
var series = new Series()
{
Name = "Darker Than Black",
LocalizedName = "Darker Than Black",
OriginalName = "Darker Than Black",
Volumes = new List<Volume>()
{
new Volume()
{
Number = 1,
Name = "1"
}
},
NormalizedName = API.Parser.Parser.Normalize("Darker Than Black"),
Metadata = new SeriesMetadata(),
Format = MangaFormat.Epub
};
Assert.False(ParserInfoHelpers.SeriesHasMatchingParserInfoFormat(series, infos));
}
[Fact]
public void SeriesHasMatchingParserInfoFormat_ShouldBeTrue()
{
var infos = new Dictionary<ParsedSeries, List<ParserInfo>>();
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Archive});
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Epub});
var series = new Series()
{
Name = "Darker Than Black",
LocalizedName = "Darker Than Black",
OriginalName = "Darker Than Black",
Volumes = new List<Volume>()
{
new Volume()
{
Number = 1,
Name = "1"
}
},
NormalizedName = API.Parser.Parser.Normalize("Darker Than Black"),
Metadata = new SeriesMetadata(),
Format = MangaFormat.Epub
};
Assert.True(ParserInfoHelpers.SeriesHasMatchingParserInfoFormat(series, infos));
}
#endregion
}

View file

@ -124,6 +124,8 @@ namespace API.Tests.Parser
[InlineData("Conquistador_Tome_2", "2")]
[InlineData("Max_l_explorateur-_Tome_0", "0")]
[InlineData("Chevaliers d'Héliopolis T3 - Rubedo, l'oeuvre au rouge (Jodorowsky & Jérémy)", "3")]
[InlineData("Adventure Time (2012)/Adventure Time #1 (2012)", "0")]
[InlineData("Adventure Time TPB (2012)/Adventure Time v01 (2012).cbz", "1")]
public void ParseComicVolumeTest(string filename, string expected)
{
Assert.Equal(expected, API.Parser.Parser.ParseComicVolume(filename));
@ -167,6 +169,8 @@ namespace API.Tests.Parser
[InlineData("2000 AD 0366 [1984-04-28] (flopbie)", "366")]
[InlineData("Daredevil - v6 - 10 - (2019)", "10")]
[InlineData("Batman Beyond 2016 - Chapter 001.cbz", "1")]
[InlineData("Adventure Time (2012)/Adventure Time #1 (2012)", "1")]
[InlineData("Adventure Time TPB (2012)/Adventure Time v01 (2012).cbz", "0")]
public void ParseComicChapterTest(string filename, string expected)
{
Assert.Equal(expected, API.Parser.Parser.ParseComicChapter(filename));

View file

@ -20,7 +20,7 @@ namespace API.Tests.Parser
Title = "darker than black",
Volumes = "0"
};
var p2 = new ParserInfo()
{
Chapters = "1",
@ -32,7 +32,7 @@ namespace API.Tests.Parser
Title = "Darker Than Black",
Volumes = "0"
};
var expected = new ParserInfo()
{
Chapters = "1",
@ -45,11 +45,11 @@ namespace API.Tests.Parser
Volumes = "0"
};
p1.Merge(p2);
AssertSame(expected, p1);
}
[Fact]
public void MergeFromTest2()
{
@ -64,7 +64,7 @@ namespace API.Tests.Parser
Title = "darker than black",
Volumes = "0"
};
var p2 = new ParserInfo()
{
Chapters = "0",
@ -76,7 +76,7 @@ namespace API.Tests.Parser
Title = "Darker Than Black",
Volumes = "1"
};
var expected = new ParserInfo()
{
Chapters = "1",
@ -93,9 +93,9 @@ namespace API.Tests.Parser
AssertSame(expected, p1);
}
private void AssertSame(ParserInfo expected, ParserInfo actual)
private static void AssertSame(ParserInfo expected, ParserInfo actual)
{
Assert.Equal(expected.Chapters, actual.Chapters);
Assert.Equal(expected.Volumes, actual.Volumes);
@ -107,4 +107,4 @@ namespace API.Tests.Parser
Assert.Equal(expected.FullFilePath, actual.FullFilePath);
}
}
}
}

View file

@ -16,20 +16,8 @@ namespace API.Tests.Services
public class DirectoryServiceTests
{
private readonly DirectoryService _directoryService;
private readonly ILogger<DirectoryService> _logger = Substitute.For<ILogger<DirectoryService>>();
public DirectoryServiceTests()
{
var filesystem = new MockFileSystem()
{
};
_directoryService = new DirectoryService(_logger, filesystem);
}
#region TraverseTreeParallelForEach
[Fact]
public void TraverseTreeParallelForEach_JustArchives_ShouldBe28()

View file

@ -16,6 +16,7 @@ using API.Services;
using API.Services.Tasks;
using API.Services.Tasks.Scanner;
using API.SignalR;
using API.Tests.Helpers;
using AutoMapper;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Data.Sqlite;
@ -30,9 +31,35 @@ namespace API.Tests.Services
public class ScannerServiceTests
{
[Fact]
public void AddOrUpdateFileForChapter()
public void FindSeriesNotOnDisk_Should_Remove1()
{
// TODO: This can be tested, it has _filesystem mocked
var infos = new Dictionary<ParsedSeries, List<ParserInfo>>();
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Archive});
//AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Volumes = "1", Format = MangaFormat.Epub});
var existingSeries = new List<Series>
{
new Series()
{
Name = "Darker Than Black",
LocalizedName = "Darker Than Black",
OriginalName = "Darker Than Black",
Volumes = new List<Volume>()
{
new Volume()
{
Number = 1,
Name = "1"
}
},
NormalizedName = API.Parser.Parser.Normalize("Darker Than Black"),
Metadata = new SeriesMetadata(),
Format = MangaFormat.Epub
}
};
Assert.Equal(1, ScannerService.FindSeriesNotOnDisk(existingSeries, infos).Count());
}
[Fact]
@ -40,9 +67,9 @@ namespace API.Tests.Services
{
var infos = new Dictionary<ParsedSeries, List<ParserInfo>>();
AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Format = MangaFormat.Archive});
AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "1", Format = MangaFormat.Archive});
AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "10", Format = MangaFormat.Archive});
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black", Format = MangaFormat.Archive});
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "1", Format = MangaFormat.Archive});
ParserInfoFactory.AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "10", Format = MangaFormat.Archive});
var existingSeries = new List<Series>
{
@ -114,48 +141,6 @@ namespace API.Tests.Services
// Assert.Equal(missingSeries.Count, removeCount);
// }
private void AddToParsedInfo(IDictionary<ParsedSeries, List<ParserInfo>> collectedSeries, ParserInfo info)
{
var existingKey = collectedSeries.Keys.FirstOrDefault(ps =>
ps.Format == info.Format && ps.NormalizedName == API.Parser.Parser.Normalize(info.Series));
existingKey ??= new ParsedSeries()
{
Format = info.Format,
Name = info.Series,
NormalizedName = API.Parser.Parser.Normalize(info.Series)
};
if (collectedSeries.GetType() == typeof(ConcurrentDictionary<,>))
{
((ConcurrentDictionary<ParsedSeries, List<ParserInfo>>) collectedSeries).AddOrUpdate(existingKey, new List<ParserInfo>() {info}, (_, oldValue) =>
{
oldValue ??= new List<ParserInfo>();
if (!oldValue.Contains(info))
{
oldValue.Add(info);
}
return oldValue;
});
}
else
{
if (!collectedSeries.ContainsKey(existingKey))
{
collectedSeries.Add(existingKey, new List<ParserInfo>() {info});
}
else
{
var list = collectedSeries[existingKey];
if (!list.Contains(info))
{
list.Add(info);
}
collectedSeries[existingKey] = list;
}
}
}
}
}