/src/NzbDrone.Core.Test/MediaFiles/UpgradeMediaFileServiceFixture.cs

https://github.com/NzbDrone/NzbDrone · C# · 213 lines · 173 code · 40 blank · 0 comment · 0 complexity · cd8ac160da10881365e879ca215de813 MD5 · raw file

  1. using System.IO;
  2. using System.Linq;
  3. using FizzWare.NBuilder;
  4. using FluentAssertions;
  5. using Marr.Data;
  6. using Moq;
  7. using NUnit.Framework;
  8. using NzbDrone.Common.Disk;
  9. using NzbDrone.Core.MediaFiles;
  10. using NzbDrone.Core.MediaFiles.EpisodeImport;
  11. using NzbDrone.Core.Parser.Model;
  12. using NzbDrone.Core.Test.Framework;
  13. using NzbDrone.Core.Tv;
  14. using NzbDrone.Test.Common;
  15. namespace NzbDrone.Core.Test.MediaFiles
  16. {
  17. public class UpgradeMediaFileServiceFixture : CoreTest<UpgradeMediaFileService>
  18. {
  19. private EpisodeFile _episodeFile;
  20. private LocalEpisode _localEpisode;
  21. [SetUp]
  22. public void Setup()
  23. {
  24. _localEpisode = new LocalEpisode();
  25. _localEpisode.Series = new Series
  26. {
  27. Path = @"C:\Test\TV\Series".AsOsAgnostic()
  28. };
  29. _episodeFile = Builder<EpisodeFile>
  30. .CreateNew()
  31. .Build();
  32. Mocker.GetMock<IDiskProvider>()
  33. .Setup(c => c.FolderExists(Directory.GetParent(_localEpisode.Series.Path).FullName))
  34. .Returns(true);
  35. Mocker.GetMock<IDiskProvider>()
  36. .Setup(c => c.FileExists(It.IsAny<string>()))
  37. .Returns(true);
  38. Mocker.GetMock<IDiskProvider>()
  39. .Setup(c => c.GetParentFolder(It.IsAny<string>()))
  40. .Returns<string>(c => Path.GetDirectoryName(c));
  41. }
  42. private void GivenSingleEpisodeWithSingleEpisodeFile()
  43. {
  44. _localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
  45. .All()
  46. .With(e => e.EpisodeFileId = 1)
  47. .With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
  48. new EpisodeFile
  49. {
  50. Id = 1,
  51. RelativePath = @"Season 01\30.rock.s01e01.avi",
  52. }))
  53. .Build()
  54. .ToList();
  55. }
  56. private void GivenMultipleEpisodesWithSingleEpisodeFile()
  57. {
  58. _localEpisode.Episodes = Builder<Episode>.CreateListOfSize(2)
  59. .All()
  60. .With(e => e.EpisodeFileId = 1)
  61. .With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
  62. new EpisodeFile
  63. {
  64. Id = 1,
  65. RelativePath = @"Season 01\30.rock.s01e01.avi",
  66. }))
  67. .Build()
  68. .ToList();
  69. }
  70. private void GivenMultipleEpisodesWithMultipleEpisodeFiles()
  71. {
  72. _localEpisode.Episodes = Builder<Episode>.CreateListOfSize(2)
  73. .TheFirst(1)
  74. .With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
  75. new EpisodeFile
  76. {
  77. Id = 1,
  78. RelativePath = @"Season 01\30.rock.s01e01.avi",
  79. }))
  80. .TheNext(1)
  81. .With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(
  82. new EpisodeFile
  83. {
  84. Id = 2,
  85. RelativePath = @"Season 01\30.rock.s01e02.avi",
  86. }))
  87. .Build()
  88. .ToList();
  89. }
  90. [Test]
  91. public void should_delete_single_episode_file_once()
  92. {
  93. GivenSingleEpisodeWithSingleEpisodeFile();
  94. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  95. Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
  96. }
  97. [Test]
  98. public void should_delete_the_same_episode_file_only_once()
  99. {
  100. GivenMultipleEpisodesWithSingleEpisodeFile();
  101. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  102. Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>(), It.IsAny<string>()), Times.Once());
  103. }
  104. [Test]
  105. public void should_delete_multiple_different_episode_files()
  106. {
  107. GivenMultipleEpisodesWithMultipleEpisodeFiles();
  108. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  109. Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>(), It.IsAny<string>()), Times.Exactly(2));
  110. }
  111. [Test]
  112. public void should_delete_episode_file_from_database()
  113. {
  114. GivenSingleEpisodeWithSingleEpisodeFile();
  115. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  116. Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(It.IsAny<EpisodeFile>(), DeleteMediaFileReason.Upgrade), Times.Once());
  117. }
  118. [Test]
  119. public void should_delete_existing_file_fromdb_if_file_doesnt_exist()
  120. {
  121. GivenSingleEpisodeWithSingleEpisodeFile();
  122. Mocker.GetMock<IDiskProvider>()
  123. .Setup(c => c.FileExists(It.IsAny<string>()))
  124. .Returns(false);
  125. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  126. Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, DeleteMediaFileReason.Upgrade), Times.Once());
  127. }
  128. [Test]
  129. public void should_not_try_to_recyclebin_existing_file_if_file_doesnt_exist()
  130. {
  131. GivenSingleEpisodeWithSingleEpisodeFile();
  132. Mocker.GetMock<IDiskProvider>()
  133. .Setup(c => c.FileExists(It.IsAny<string>()))
  134. .Returns(false);
  135. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  136. Mocker.GetMock<IRecycleBinProvider>().Verify(v => v.DeleteFile(It.IsAny<string>(), It.IsAny<string>()), Times.Never());
  137. }
  138. [Test]
  139. public void should_return_old_episode_file_in_oldFiles()
  140. {
  141. GivenSingleEpisodeWithSingleEpisodeFile();
  142. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode).OldFiles.Count.Should().Be(1);
  143. }
  144. [Test]
  145. public void should_return_old_episode_files_in_oldFiles()
  146. {
  147. GivenMultipleEpisodesWithMultipleEpisodeFiles();
  148. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode).OldFiles.Count.Should().Be(2);
  149. }
  150. [Test]
  151. public void should_throw_if_there_are_existing_episode_files_and_the_root_folder_is_missing()
  152. {
  153. GivenSingleEpisodeWithSingleEpisodeFile();
  154. Mocker.GetMock<IDiskProvider>()
  155. .Setup(c => c.FolderExists(Directory.GetParent(_localEpisode.Series.Path).FullName))
  156. .Returns(false);
  157. Assert.Throws<RootFolderNotFoundException>(() => Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode));
  158. Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, DeleteMediaFileReason.Upgrade), Times.Never());
  159. }
  160. [Test]
  161. public void should_import_if_existing_file_doesnt_exist_in_db()
  162. {
  163. _localEpisode.Episodes = Builder<Episode>.CreateListOfSize(1)
  164. .All()
  165. .With(e => e.EpisodeFileId = 1)
  166. .With(e => e.EpisodeFile = new LazyLoaded<EpisodeFile>(null))
  167. .Build()
  168. .ToList();
  169. Subject.UpgradeEpisodeFile(_episodeFile, _localEpisode);
  170. Mocker.GetMock<IMediaFileService>().Verify(v => v.Delete(_localEpisode.Episodes.Single().EpisodeFile.Value, It.IsAny<DeleteMediaFileReason>()), Times.Never());
  171. }
  172. }
  173. }