瀏覽代碼

Written test to finish coverage for AudioBookListResolver & AudioBookResolver and corrected some logical erros / unhandled exception

Stepan 4 年之前
父節點
當前提交
f39775dc3a

+ 12 - 8
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<AudioBookFileInfo>()
                 .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<AudioBookFileInfo>().ToList();
+                var stackFiles = stack.Files
+                    .Select(i => audioBookResolver.Resolve(i))
+                    .OfType<AudioBookFileInfo>()
+                    .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;
             }

+ 2 - 1
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);

+ 37 - 1
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<string>();
+
+            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);

+ 18 - 3
tests/Jellyfin.Naming.Tests/AudioBook/AudioBookResolverTests.cs

@@ -35,6 +35,11 @@ namespace Jellyfin.Naming.Tests.AudioBook
             };
         }
 
+        public static IEnumerable<object[]> 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<ArgumentException>(() => new AudioBookResolver(_namingOptions).Resolve(string.Empty));
+            var result = new AudioBookResolver(_namingOptions).Resolve(string.Empty);
+
+            Assert.Null(result);
         }
     }
 }