Feature/unit tests (#171)
* Removed a duplicate loop that was already done earlier in method. * Normalize now replaces underscores * Added more Parser cases, Added test case for SeriesExtension (Name in List), and added MergeNameTest and some TODOs for where tests should go * Added a test for removal * Fixed bad merge Co-authored-by: Andrew Song <asong641@gmail.com>
This commit is contained in:
parent
6ba00477e7
commit
d59d60d9ec
6 changed files with 205 additions and 54 deletions
|
@ -1,12 +1,17 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using API.Interfaces.Services;
|
||||
using API.Parser;
|
||||
using API.Services;
|
||||
using API.Services.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using NSubstitute.Extensions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
|
@ -28,33 +33,126 @@ namespace API.Tests.Services
|
|||
_testOutputHelper = testOutputHelper;
|
||||
_scannerService = new ScannerService(_unitOfWork, _logger, _archiveService, _metadataService);
|
||||
_metadataService= Substitute.For<MetadataService>(_unitOfWork, _metadataLogger, _archiveService);
|
||||
_libraryMock = new Library()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "Manga",
|
||||
Folders = new List<FolderPath>()
|
||||
{
|
||||
new FolderPath()
|
||||
{
|
||||
Id = 1,
|
||||
LastScanned = DateTime.Now,
|
||||
LibraryId = 1,
|
||||
Path = "E:/Manga"
|
||||
}
|
||||
},
|
||||
LastModified = DateTime.Now,
|
||||
Series = new List<Series>()
|
||||
{
|
||||
new Series()
|
||||
{
|
||||
Id = 0,
|
||||
Name = "Darker Than Black"
|
||||
}
|
||||
}
|
||||
};
|
||||
// _libraryMock = new Library()
|
||||
// {
|
||||
// Id = 1,
|
||||
// Name = "Manga",
|
||||
// Folders = new List<FolderPath>()
|
||||
// {
|
||||
// new FolderPath()
|
||||
// {
|
||||
// Id = 1,
|
||||
// LastScanned = DateTime.Now,
|
||||
// LibraryId = 1,
|
||||
// Path = "E:/Manga"
|
||||
// }
|
||||
// },
|
||||
// LastModified = DateTime.Now,
|
||||
// Series = new List<Series>()
|
||||
// {
|
||||
// new Series()
|
||||
// {
|
||||
// Id = 0,
|
||||
// Name = "Darker Than Black"
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void FindSeriesNotOnDisk_Should_RemoveNothing_Test()
|
||||
{
|
||||
var scannerService = new ScannerService(_unitOfWork, _logger, _archiveService, _metadataService);
|
||||
var infos = new Dictionary<string, List<ParserInfo>>();
|
||||
|
||||
AddToParsedInfo(infos, new ParserInfo() {Series = "Darker than Black"});
|
||||
AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "1"});
|
||||
AddToParsedInfo(infos, new ParserInfo() {Series = "Cage of Eden", Volumes = "10"});
|
||||
|
||||
var existingSeries = new List<Series>();
|
||||
existingSeries.Add(new Series()
|
||||
{
|
||||
Name = "Cage of Eden",
|
||||
LocalizedName = "Cage of Eden",
|
||||
OriginalName = "Cage of Eden",
|
||||
NormalizedName = Parser.Parser.Normalize("Cage of Eden")
|
||||
});
|
||||
existingSeries.Add(new Series()
|
||||
{
|
||||
Name = "Darker Than Black",
|
||||
LocalizedName = "Darker Than Black",
|
||||
OriginalName = "Darker Than Black",
|
||||
NormalizedName = Parser.Parser.Normalize("Darker Than Black")
|
||||
});
|
||||
var expectedSeries = new List<Series>();
|
||||
|
||||
|
||||
|
||||
Assert.Empty(scannerService.FindSeriesNotOnDisk(existingSeries, infos));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(new [] {"Darker than Black"}, "Darker than Black", "Darker than Black")]
|
||||
[InlineData(new [] {"Darker than Black"}, "Darker Than Black", "Darker than Black")]
|
||||
[InlineData(new [] {"Darker than Black"}, "Darker Than Black!", "Darker Than Black!")]
|
||||
[InlineData(new [] {""}, "Runaway Jack", "Runaway Jack")]
|
||||
public void MergeNameTest(string[] existingSeriesNames, string parsedInfoName, string expected)
|
||||
{
|
||||
var scannerService = new ScannerService(_unitOfWork, _logger, _archiveService, _metadataService);
|
||||
|
||||
var collectedSeries = new ConcurrentDictionary<string, List<ParserInfo>>();
|
||||
foreach (var seriesName in existingSeriesNames)
|
||||
{
|
||||
AddToParsedInfo(collectedSeries, new ParserInfo() {Series = seriesName});
|
||||
}
|
||||
|
||||
var actualName = scannerService.MergeName(collectedSeries, new ParserInfo()
|
||||
{
|
||||
Series = parsedInfoName
|
||||
});
|
||||
|
||||
Assert.Equal(expected, actualName);
|
||||
}
|
||||
|
||||
private void AddToParsedInfo(IDictionary<string, List<ParserInfo>> collectedSeries, ParserInfo info)
|
||||
{
|
||||
if (collectedSeries.GetType() == typeof(ConcurrentDictionary<,>))
|
||||
{
|
||||
((ConcurrentDictionary<string, List<ParserInfo>>) collectedSeries).AddOrUpdate(info.Series, new List<ParserInfo>() {info}, (_, oldValue) =>
|
||||
{
|
||||
oldValue ??= new List<ParserInfo>();
|
||||
if (!oldValue.Contains(info))
|
||||
{
|
||||
oldValue.Add(info);
|
||||
}
|
||||
|
||||
return oldValue;
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!collectedSeries.ContainsKey(info.Series))
|
||||
{
|
||||
collectedSeries.Add(info.Series, new List<ParserInfo>() {info});
|
||||
}
|
||||
else
|
||||
{
|
||||
var list = collectedSeries[info.Series];
|
||||
if (!list.Contains(info))
|
||||
{
|
||||
list.Add(info);
|
||||
}
|
||||
|
||||
collectedSeries[info.Series] = list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// [Fact]
|
||||
// public void ExistingOrDefault_Should_BeFromLibrary()
|
||||
// {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue