From b9c7cce6968482145ab15603dcca218e67d1b2b9 Mon Sep 17 00:00:00 2001 From: Keridos <2742845+Keridos@users.noreply.github.com> Date: Wed, 9 Sep 2020 01:57:09 +0200 Subject: [PATCH 1/3] some testing for AudioBook fix PartNumber detection --- .../AudioBook/AudioBookFilePathParser.cs | 17 +--- Emby.Naming/AudioBook/AudioBookResolver.cs | 2 +- .../AudioBook/AudioBookListResolverTests.cs | 90 +++++++++++++++++++ .../AudioBook/AudioBookResolverTests.cs | 58 ++++++++++++ 4 files changed, 151 insertions(+), 16 deletions(-) create mode 100644 tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs create mode 100644 tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs diff --git a/Emby.Naming/AudioBook/AudioBookFilePathParser.cs b/Emby.Naming/AudioBook/AudioBookFilePathParser.cs index 3c874c62ca..eb9393b0bd 100644 --- a/Emby.Naming/AudioBook/AudioBookFilePathParser.cs +++ b/Emby.Naming/AudioBook/AudioBookFilePathParser.cs @@ -50,27 +50,14 @@ namespace Emby.Naming.AudioBook { if (int.TryParse(value.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var intValue)) { - result.ChapterNumber = intValue; + result.PartNumber = intValue; } } } } } - /*var matches = _iRegexProvider.GetRegex("\\d+", RegexOptions.IgnoreCase).Matches(fileName); - if (matches.Count > 0) - { - if (!result.ChapterNumber.HasValue) - { - result.ChapterNumber = int.Parse(matches[0].Groups[0].Value); - } - - if (matches.Count > 1) - { - result.PartNumber = int.Parse(matches[matches.Count - 1].Groups[0].Value); - } - }*/ - result.Success = result.PartNumber.HasValue || result.ChapterNumber.HasValue; + result.Success = result.ChapterNumber.HasValue || result.PartNumber.HasValue; return result; } diff --git a/Emby.Naming/AudioBook/AudioBookResolver.cs b/Emby.Naming/AudioBook/AudioBookResolver.cs index 5466b46379..ed53bd04fa 100644 --- a/Emby.Naming/AudioBook/AudioBookResolver.cs +++ b/Emby.Naming/AudioBook/AudioBookResolver.cs @@ -55,8 +55,8 @@ namespace Emby.Naming.AudioBook { Path = path, Container = container, - PartNumber = parsingResult.PartNumber, ChapterNumber = parsingResult.ChapterNumber, + PartNumber = parsingResult.PartNumber, IsDirectory = isDirectory }; } diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs new file mode 100644 index 0000000000..541bb321cd --- /dev/null +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs @@ -0,0 +1,90 @@ +using System.Linq; +using Emby.Naming.AudioBook; +using Emby.Naming.Common; +using MediaBrowser.Model.IO; +using Xunit; + +namespace Jellyfin.Naming.Tests.AudioBook +{ + public class AudioBookListResolverTests + { + private readonly NamingOptions _namingOptions = new NamingOptions(); + + [Fact] + public void TestStackAndExtras() + { + // No stacking here because there is no part/disc/etc + var files = new[] + { + "Harry Potter and the Deathly Hallows/Part 1.mp3", + "Harry Potter and the Deathly Hallows/Part 2.mp3", + "Harry Potter and the Deathly Hallows/book.nfo", + + "Batman/Chapter 1.mp3", + "Batman/Chapter 2.mp3", + "Batman/Chapter 3.mp3", + }; + + var resolver = GetResolver(); + + var result = resolver.Resolve(files.Select(i => new FileSystemMetadata + { + IsDirectory = false, + FullName = i + }).ToList()).ToList(); + + Assert.Equal(2, result[0].Files.ToList().Count); + // Assert.Empty(result[0].Extras); FIXME: AudioBookListResolver should resolve extra files properly + Assert.Equal("Harry Potter and the Deathly Hallows", result[0].Name); + + Assert.Equal(3, result[1].Files.ToList().Count); + Assert.Empty(result[1].Extras); + Assert.Equal("Batman", result[1].Name); + } + + [Fact] + public void TestWithMetadata() + { + var files = new[] + { + "Harry Potter and the Deathly Hallows/Chapter 1.ogg", + "Harry Potter and the Deathly Hallows/Harry Potter and the Deathly Hallows.nfo" + }; + + var resolver = GetResolver(); + + var result = resolver.Resolve(files.Select(i => new FileSystemMetadata + { + IsDirectory = false, + FullName = i + }).ToList()).ToList(); + + Assert.Single(result); + } + + [Fact] + public void TestWithExtra() + { + var files = new[] + { + "Harry Potter and the Deathly Hallows/Chapter 1.mp3", + "Harry Potter and the Deathly Hallows/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()).ToList(); + + Assert.Single(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 new file mode 100644 index 0000000000..5da2e93d8d --- /dev/null +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using Emby.Naming.AudioBook; +using Emby.Naming.Common; +using MediaBrowser.Model.Entities; +using Xunit; + +namespace Jellyfin.Naming.Tests.AudioBook +{ + public class AudioBookResolverTests + { + private readonly NamingOptions _namingOptions = new NamingOptions(); + + public static IEnumerable GetResolveFileTestData() + { + yield return new object[] + { + new AudioBookFileInfo() + { + Path = @"/server/AudioBooks/Larry Potter/Larry Potter.mp3", + Container = "mp3", + } + }; + yield return new object[] + { + new AudioBookFileInfo() + { + Path = @"/server/AudioBooks/Berry Potter/Chapter 1 .ogg", + Container = "ogg", + ChapterNumber = 1 + } + }; + yield return new object[] + { + new AudioBookFileInfo() + { + Path = @"/server/AudioBooks/Nerry Potter/Part 3 - Chapter 2.mp3", + Container = "mp3", + ChapterNumber = 2, + PartNumber = 3 + } + }; + } + + [Theory] + [MemberData(nameof(GetResolveFileTestData))] + public void ResolveFile_ValidFileName_Success(AudioBookFileInfo expectedResult) + { + var result = new AudioBookResolver(_namingOptions).Resolve(expectedResult.Path); + + Assert.NotNull(result); + Assert.Equal(result?.Path, expectedResult.Path); + Assert.Equal(result?.Container, expectedResult.Container); + Assert.Equal(result?.ChapterNumber, expectedResult.ChapterNumber); + Assert.Equal(result?.PartNumber, expectedResult.PartNumber); + Assert.Equal(result?.IsDirectory, expectedResult.IsDirectory); + } + } +} From 8eb1eedc8d4b1a287f317013c54ed59a6c29b229 Mon Sep 17 00:00:00 2001 From: Keridos <2742845+Keridos@users.noreply.github.com> Date: Wed, 9 Sep 2020 02:33:59 +0200 Subject: [PATCH 2/3] implement suggested changes --- .../AudioBook/AudioBookListResolverTests.cs | 10 +++++----- .../AudioBook/AudioBookResolverTests.cs | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs index 541bb321cd..1084e20bda 100644 --- a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookListResolverTests.cs @@ -31,13 +31,13 @@ namespace Jellyfin.Naming.Tests.AudioBook { IsDirectory = false, FullName = i - }).ToList()).ToList(); + })).ToList(); - Assert.Equal(2, result[0].Files.ToList().Count); + Assert.Equal(2, result[0].Files.Count); // Assert.Empty(result[0].Extras); FIXME: AudioBookListResolver should resolve extra files properly Assert.Equal("Harry Potter and the Deathly Hallows", result[0].Name); - Assert.Equal(3, result[1].Files.ToList().Count); + Assert.Equal(3, result[1].Files.Count); Assert.Empty(result[1].Extras); Assert.Equal("Batman", result[1].Name); } @@ -57,7 +57,7 @@ namespace Jellyfin.Naming.Tests.AudioBook { IsDirectory = false, FullName = i - }).ToList()).ToList(); + })); Assert.Single(result); } @@ -77,7 +77,7 @@ namespace Jellyfin.Naming.Tests.AudioBook { IsDirectory = false, FullName = i - }).ToList()).ToList(); + })).ToList(); Assert.Single(result); } diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs index 5da2e93d8d..0d29f1703c 100644 --- a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using Emby.Naming.AudioBook; using Emby.Naming.Common; -using MediaBrowser.Model.Entities; using Xunit; namespace Jellyfin.Naming.Tests.AudioBook From ba777039ef6635332c8a92c03d558a7ce8e75b7e Mon Sep 17 00:00:00 2001 From: Keridos <2742845+Keridos@users.noreply.github.com> Date: Wed, 9 Sep 2020 02:43:06 +0200 Subject: [PATCH 3/3] remove unnecessary nullchecks --- .../AudioBook/AudioBookResolverTests.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs index 0d29f1703c..83d44721c4 100644 --- a/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs +++ b/tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs @@ -47,11 +47,11 @@ namespace Jellyfin.Naming.Tests.AudioBook var result = new AudioBookResolver(_namingOptions).Resolve(expectedResult.Path); Assert.NotNull(result); - Assert.Equal(result?.Path, expectedResult.Path); - Assert.Equal(result?.Container, expectedResult.Container); - Assert.Equal(result?.ChapterNumber, expectedResult.ChapterNumber); - Assert.Equal(result?.PartNumber, expectedResult.PartNumber); - Assert.Equal(result?.IsDirectory, expectedResult.IsDirectory); + Assert.Equal(result.Path, expectedResult.Path); + Assert.Equal(result.Container, expectedResult.Container); + Assert.Equal(result.ChapterNumber, expectedResult.ChapterNumber); + Assert.Equal(result.PartNumber, expectedResult.PartNumber); + Assert.Equal(result.IsDirectory, expectedResult.IsDirectory); } } }