From f39775dc3a813609454655c31dd5c6a1413cc890 Mon Sep 17 00:00:00 2001 From: Stepan Date: Sun, 1 Nov 2020 17:10:48 +0100 Subject: [PATCH] Written test to finish coverage for AudioBookListResolver & AudioBookResolver and corrected some logical erros / unhandled exception --- .../AudioBook/AudioBookListResolver.cs | 20 ++++++---- Emby.Naming/AudioBook/AudioBookResolver.cs | 3 +- .../AudioBook/AudioBookListResolverTests.cs | 38 ++++++++++++++++++- .../AudioBook/AudioBookResolverTests.cs | 21 ++++++++-- 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/Emby.Naming/AudioBook/AudioBookListResolver.cs b/Emby.Naming/AudioBook/AudioBookListResolver.cs index 0d190c172e..795065a6c9 100644 --- a/Emby.Naming/AudioBook/AudioBookListResolver.cs +++ b/Emby.Naming/AudioBook/AudioBookListResolver.cs @@ -1,6 +1,8 @@ #pragma warning disable CS1591 +using System; using System.Collections.Generic; +using System.IO; using System.Linq; using Emby.Naming.Common; using Emby.Naming.Video; @@ -21,25 +23,27 @@ namespace Emby.Naming.AudioBook { var audioBookResolver = new AudioBookResolver(_options); + // File with empty fullname will be sorted out here var audiobookFileInfos = files .Select(i => audioBookResolver.Resolve(i.FullName)) .OfType() .ToList(); - // Filter out all extras, otherwise they could cause stacks to not be resolved - // See the unit test TestStackedWithTrailer - var metadata = audiobookFileInfos - .Select(i => new FileSystemMetadata { FullName = i.Path, IsDirectory = false }); - var stackResult = new StackResolver(_options) .ResolveAudioBooks(audiobookFileInfos); foreach (var stack in stackResult) { - var stackFiles = stack.Files.Select(i => audioBookResolver.Resolve(i)).OfType().ToList(); + var stackFiles = stack.Files + .Select(i => audioBookResolver.Resolve(i)) + .OfType() + .ToList(); + stackFiles.Sort(); - // TODO nullable discover if name can be empty - var info = new AudioBookInfo(stack.Name ?? string.Empty) { Files = stackFiles }; + + // stack.Name can be empty when we have file without folder, but always have some files + var name = string.IsNullOrEmpty(stack.Name) ? stack.Files[0] : stack.Name; + var info = new AudioBookInfo(name) { Files = stackFiles }; yield return info; } diff --git a/Emby.Naming/AudioBook/AudioBookResolver.cs b/Emby.Naming/AudioBook/AudioBookResolver.cs index c9e8c76cbc..542d6fee51 100644 --- a/Emby.Naming/AudioBook/AudioBookResolver.cs +++ b/Emby.Naming/AudioBook/AudioBookResolver.cs @@ -21,7 +21,8 @@ namespace Emby.Naming.AudioBook { if (path.Length == 0) { - throw new ArgumentException("String can't be empty.", nameof(path)); + // Return null to indicate this path will not be used, instead of stopping whole process with exception + return null; } var extension = Path.GetExtension(path); diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs index 1084e20bda..d912b0e907 100644 --- a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using Emby.Naming.AudioBook; using Emby.Naming.Common; using MediaBrowser.Model.IO; @@ -82,6 +83,41 @@ namespace Jellyfin.Naming.Tests.AudioBook Assert.Single(result); } + [Fact] + public void TestWithoutFolder() + { + var files = new[] + { + "Harry Potter and the Deathly Hallows trailer.mp3" + }; + + var resolver = GetResolver(); + + var result = resolver.Resolve(files.Select(i => new FileSystemMetadata + { + IsDirectory = false, + FullName = i + })).ToList(); + + Assert.Single(result); + } + + [Fact] + public void TestEmpty() + { + var files = Array.Empty(); + + var resolver = GetResolver(); + + var result = resolver.Resolve(files.Select(i => new FileSystemMetadata + { + IsDirectory = false, + FullName = i + })).ToList(); + + Assert.Empty(result); + } + private AudioBookListResolver GetResolver() { return new AudioBookListResolver(_namingOptions); diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs index 2708d80bb6..282da2b93a 100644 --- a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs @@ -35,6 +35,11 @@ namespace Jellyfin.Naming.Tests.AudioBook }; } + public static IEnumerable GetPathsWithInvalidExtensions() + { + yield return new object[] { @"/server/AudioBooks/Larry Potter/Larry Potter.mp9" }; + } + [Theory] [MemberData(nameof(GetResolveFileTestData))] public void Resolve_ValidFileName_Success(AudioBookFileInfo expectedResult) @@ -46,13 +51,23 @@ namespace Jellyfin.Naming.Tests.AudioBook Assert.Equal(result!.Container, expectedResult.Container); Assert.Equal(result!.ChapterNumber, expectedResult.ChapterNumber); Assert.Equal(result!.PartNumber, expectedResult.PartNumber); - Assert.Equal(result!.IsDirectory, expectedResult.IsDirectory); + } + + [Theory] + [MemberData(nameof(GetPathsWithInvalidExtensions))] + public void Resolve_InvalidExtension(string path) + { + var result = new AudioBookResolver(_namingOptions).Resolve(path); + + Assert.Null(result); } [Fact] - public void Resolve_EmptyFileName_ArgumentException() + public void Resolve_EmptyFileName() { - Assert.Throws(() => new AudioBookResolver(_namingOptions).Resolve(string.Empty)); + var result = new AudioBookResolver(_namingOptions).Resolve(string.Empty); + + Assert.Null(result); } } }