/src/NzbDrone.Core.Test/MediaFiles/TrackImport/Aggregation/AggregateFilenameInfoFixture.cs

https://github.com/lidarr/Lidarr · C# · 206 lines · 179 code · 26 blank · 1 comment · 8 complexity · c797d070335e4d787a5effc2533ac9a8 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using FluentAssertions;
  7. using NUnit.Framework;
  8. using NzbDrone.Core.MediaFiles.TrackImport.Aggregation.Aggregators;
  9. using NzbDrone.Core.Parser.Model;
  10. using NzbDrone.Core.Test.Framework;
  11. using NzbDrone.Test.Common;
  12. namespace NzbDrone.Core.Test.MediaFiles.TrackImport.Aggregation.Aggregators
  13. {
  14. [TestFixture]
  15. public class AggregateFilenameInfoFixture : CoreTest<AggregateFilenameInfo>
  16. {
  17. private LocalAlbumRelease GivenTracks(List<string> files, string root)
  18. {
  19. var tracks = files.Select(x => new LocalTrack
  20. {
  21. Path = Path.Combine(root, x),
  22. FileTrackInfo = new ParsedTrackInfo
  23. {
  24. TrackNumbers = new[] { 0 },
  25. }
  26. }).ToList();
  27. return new LocalAlbumRelease(tracks);
  28. }
  29. private void VerifyData(LocalTrack track, string artist, string title, int trackNum, int disc)
  30. {
  31. track.FileTrackInfo.ArtistTitle.Should().Be(artist);
  32. track.FileTrackInfo.Title.Should().Be(title);
  33. track.FileTrackInfo.TrackNumbers[0].Should().Be(trackNum);
  34. track.FileTrackInfo.DiscNumber.Should().Be(disc);
  35. }
  36. [Test]
  37. public void should_aggregate_filenames_example()
  38. {
  39. var release = GivenTracks(new List<string>
  40. {
  41. "Adele - 19 - 101 - Daydreamer.mp3",
  42. "Adele - 19 - 102 - Best for Last.mp3",
  43. "Adele - 19 - 103 - Chasing Pavements.mp3",
  44. "Adele - 19 - 203 - That's It, I Quit, I'm Moving On.mp3"
  45. }, @"C:\incoming".AsOsAgnostic());
  46. Subject.Aggregate(release, true);
  47. VerifyData(release.LocalTracks[0], "Adele", "Daydreamer", 1, 1);
  48. VerifyData(release.LocalTracks[1], "Adele", "Best for Last", 2, 1);
  49. VerifyData(release.LocalTracks[2], "Adele", "Chasing Pavements", 3, 1);
  50. VerifyData(release.LocalTracks[3], "Adele", "That's It, I Quit, I'm Moving On", 3, 2);
  51. }
  52. public static class TestCaseFactory
  53. {
  54. private static List<string[]> tokenList = new List<string[]>
  55. {
  56. new[] { "trackNum2", "artist", "title", "tag" },
  57. new[] { "trackNum3", "artist", "title", "tag" },
  58. new[] { "trackNum2", "artist", "tag", "title" },
  59. new[] { "trackNum3", "artist", "tag", "title" },
  60. new[] { "trackNum2", "artist", "title" },
  61. new[] { "trackNum3", "artist", "title" },
  62. new[] { "artist", "tag", "trackNum2", "title" },
  63. new[] { "artist", "tag", "trackNum3", "title" },
  64. new[] { "artist", "trackNum2", "title", "tag" },
  65. new[] { "artist", "trackNum3", "title", "tag" },
  66. new[] { "artist", "trackNum2", "title" },
  67. new[] { "artist", "trackNum3", "title" },
  68. new[] { "artist", "title", "tag" },
  69. new[] { "artist", "tag", "title" },
  70. new[] { "artist", "title" },
  71. new[] { "trackNum2", "title" },
  72. new[] { "trackNum3", "title" },
  73. new[] { "title" },
  74. };
  75. private static List<Tuple<string, string>> separators = new List<Tuple<string, string>>
  76. {
  77. Tuple.Create(" - ", " "),
  78. Tuple.Create("_", " "),
  79. Tuple.Create("-", "_")
  80. };
  81. private static List<Tuple<string[], string, string>> otherCases = new List<Tuple<string[], string, string>>
  82. {
  83. Tuple.Create(new[] { "track2", "title" }, " ", " "),
  84. Tuple.Create(new[] { "track3", "title" }, " ", " ")
  85. };
  86. public static IEnumerable TestCases
  87. {
  88. get
  89. {
  90. int i = 0;
  91. foreach (var tokens in tokenList)
  92. {
  93. foreach (var separator in separators)
  94. {
  95. i++;
  96. yield return new TestCaseData(Tuple.Create(tokens, separator.Item1, separator.Item2))
  97. .SetName($"should_aggregate_filenames_auto_{i}")
  98. .SetDescription($"tokens: {string.Join(", ", tokens)}, separator: '{separator.Item1}', whitespace: '{separator.Item2}'");
  99. }
  100. }
  101. // and a few other cases where all the permutations don't make sense
  102. foreach (var item in otherCases)
  103. {
  104. i++;
  105. yield return new TestCaseData(item)
  106. .SetName($"should_aggregate_filenames_auto_{i}")
  107. .SetDescription($"tokens: {string.Join(", ", item.Item1)}, separator: '{item.Item2}', whitespace: '{item.Item3}'");
  108. }
  109. }
  110. }
  111. }
  112. private List<string> GivenFilenames(string[] fields, string fieldSeparator, string whitespace)
  113. {
  114. var outp = new List<string>();
  115. for (int i = 1; i <= 3; i++)
  116. {
  117. var components = new List<string>();
  118. foreach (var field in fields)
  119. {
  120. switch (field)
  121. {
  122. case "artist":
  123. components.Add("artist name".Replace(" ", whitespace));
  124. break;
  125. case "tag":
  126. components.Add("tag string ignore".Replace(" ", whitespace));
  127. break;
  128. case "title":
  129. components.Add($"{(char)(96 + i)} track title {i}".Replace(" ", whitespace));
  130. break;
  131. case "trackNum2":
  132. components.Add(i.ToString("00"));
  133. break;
  134. case "trackNum3":
  135. components.Add((100 + i).ToString("000"));
  136. break;
  137. }
  138. }
  139. outp.Add(string.Join(fieldSeparator, components) + ".mp3");
  140. }
  141. return outp;
  142. }
  143. private void VerifyDataAuto(List<LocalTrack> tracks, string[] tokens, string whitespace)
  144. {
  145. for (int i = 1; i <= tracks.Count; i++)
  146. {
  147. var info = tracks[i - 1].FileTrackInfo;
  148. if (tokens.Contains("artist"))
  149. {
  150. info.ArtistTitle.Should().Be("artist name".Replace(" ", whitespace));
  151. }
  152. if (tokens.Contains("title"))
  153. {
  154. info.Title.Should().Be($"{(char)(96 + i)} track title {i}".Replace(" ", whitespace));
  155. }
  156. if (tokens.Contains("trackNum2") || tokens.Contains("trackNum3"))
  157. {
  158. info.TrackNumbers[0].Should().Be(i);
  159. }
  160. if (tokens.Contains("trackNum3"))
  161. {
  162. info.DiscNumber.Should().Be(1);
  163. }
  164. else
  165. {
  166. info.DiscNumber.Should().Be(0);
  167. }
  168. }
  169. }
  170. [Test]
  171. [TestCaseSource(typeof(TestCaseFactory), "TestCases")]
  172. public void should_aggregate_filenames_auto(Tuple<string[], string, string> testcase)
  173. {
  174. var files = GivenFilenames(testcase.Item1, testcase.Item2, testcase.Item3);
  175. var release = GivenTracks(files, @"C:\incoming".AsOsAgnostic());
  176. Subject.Aggregate(release, true);
  177. VerifyDataAuto(release.LocalTracks, testcase.Item1, testcase.Item3);
  178. }
  179. }
  180. }