/src/NzbDrone.Core.Test/MediaFiles/MediaInfo/UpdateMediaInfoServiceFixture.cs

https://github.com/NzbDrone/NzbDrone · C# · 291 lines · 229 code · 62 blank · 0 comment · 0 complexity · e3bc97bf0c39fcdda8712ca8c7e1daf6 MD5 · raw file

  1. using System.IO;
  2. using FizzWare.NBuilder;
  3. using FluentAssertions;
  4. using Moq;
  5. using NUnit.Framework;
  6. using NzbDrone.Common.Disk;
  7. using NzbDrone.Core.MediaFiles;
  8. using NzbDrone.Core.MediaFiles.Events;
  9. using NzbDrone.Core.MediaFiles.MediaInfo;
  10. using NzbDrone.Core.Test.Framework;
  11. using NzbDrone.Core.Tv;
  12. using NzbDrone.Test.Common;
  13. using NzbDrone.Core.Configuration;
  14. namespace NzbDrone.Core.Test.MediaFiles.MediaInfo
  15. {
  16. [TestFixture]
  17. public class UpdateMediaInfoServiceFixture : CoreTest<UpdateMediaInfoService>
  18. {
  19. private Series _series;
  20. [SetUp]
  21. public void Setup()
  22. {
  23. _series = new Series
  24. {
  25. Id = 1,
  26. Path = @"C:\series".AsOsAgnostic()
  27. };
  28. Mocker.GetMock<IConfigService>()
  29. .SetupGet(s => s.EnableMediaInfo)
  30. .Returns(true);
  31. }
  32. private void GivenFileExists()
  33. {
  34. Mocker.GetMock<IDiskProvider>()
  35. .Setup(v => v.FileExists(It.IsAny<string>()))
  36. .Returns(true);
  37. }
  38. private void GivenSuccessfulScan()
  39. {
  40. Mocker.GetMock<IVideoFileInfoReader>()
  41. .Setup(v => v.GetMediaInfo(It.IsAny<string>()))
  42. .Returns(new MediaInfoModel());
  43. }
  44. private void GivenFailedScan(string path)
  45. {
  46. Mocker.GetMock<IVideoFileInfoReader>()
  47. .Setup(v => v.GetMediaInfo(path))
  48. .Returns((MediaInfoModel)null);
  49. }
  50. [Test]
  51. public void should_skip_up_to_date_media_info()
  52. {
  53. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(3)
  54. .All()
  55. .With(v => v.RelativePath = "media.mkv")
  56. .TheFirst(1)
  57. .With(v => v.MediaInfo = new MediaInfoModel { SchemaRevision = VideoFileInfoReader.CURRENT_MEDIA_INFO_SCHEMA_REVISION })
  58. .BuildList();
  59. Mocker.GetMock<IMediaFileService>()
  60. .Setup(v => v.GetFilesBySeries(1))
  61. .Returns(episodeFiles);
  62. GivenFileExists();
  63. GivenSuccessfulScan();
  64. Subject.Handle(new SeriesScannedEvent(_series));
  65. Mocker.GetMock<IVideoFileInfoReader>()
  66. .Verify(v => v.GetMediaInfo(Path.Combine(_series.Path, "media.mkv")), Times.Exactly(2));
  67. Mocker.GetMock<IMediaFileService>()
  68. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Exactly(2));
  69. }
  70. [Test]
  71. public void should_skip_not_yet_date_media_info()
  72. {
  73. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(3)
  74. .All()
  75. .With(v => v.RelativePath = "media.mkv")
  76. .TheFirst(1)
  77. .With(v => v.MediaInfo = new MediaInfoModel { SchemaRevision = VideoFileInfoReader.MINIMUM_MEDIA_INFO_SCHEMA_REVISION })
  78. .BuildList();
  79. Mocker.GetMock<IMediaFileService>()
  80. .Setup(v => v.GetFilesBySeries(1))
  81. .Returns(episodeFiles);
  82. GivenFileExists();
  83. GivenSuccessfulScan();
  84. Subject.Handle(new SeriesScannedEvent(_series));
  85. Mocker.GetMock<IVideoFileInfoReader>()
  86. .Verify(v => v.GetMediaInfo(Path.Combine(_series.Path, "media.mkv")), Times.Exactly(2));
  87. Mocker.GetMock<IMediaFileService>()
  88. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Exactly(2));
  89. }
  90. [Test]
  91. public void should_update_outdated_media_info()
  92. {
  93. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(3)
  94. .All()
  95. .With(v => v.RelativePath = "media.mkv")
  96. .TheFirst(1)
  97. .With(v => v.MediaInfo = new MediaInfoModel())
  98. .BuildList();
  99. Mocker.GetMock<IMediaFileService>()
  100. .Setup(v => v.GetFilesBySeries(1))
  101. .Returns(episodeFiles);
  102. GivenFileExists();
  103. GivenSuccessfulScan();
  104. Subject.Handle(new SeriesScannedEvent(_series));
  105. Mocker.GetMock<IVideoFileInfoReader>()
  106. .Verify(v => v.GetMediaInfo(Path.Combine(_series.Path, "media.mkv")), Times.Exactly(3));
  107. Mocker.GetMock<IMediaFileService>()
  108. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Exactly(3));
  109. }
  110. [Test]
  111. public void should_ignore_missing_files()
  112. {
  113. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(2)
  114. .All()
  115. .With(v => v.RelativePath = "media.mkv")
  116. .BuildList();
  117. Mocker.GetMock<IMediaFileService>()
  118. .Setup(v => v.GetFilesBySeries(1))
  119. .Returns(episodeFiles);
  120. GivenSuccessfulScan();
  121. Subject.Handle(new SeriesScannedEvent(_series));
  122. Mocker.GetMock<IVideoFileInfoReader>()
  123. .Verify(v => v.GetMediaInfo("media.mkv"), Times.Never());
  124. Mocker.GetMock<IMediaFileService>()
  125. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Never());
  126. }
  127. [Test]
  128. public void should_continue_after_failure()
  129. {
  130. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(2)
  131. .All()
  132. .With(v => v.RelativePath = "media.mkv")
  133. .TheFirst(1)
  134. .With(v => v.RelativePath = "media2.mkv")
  135. .BuildList();
  136. Mocker.GetMock<IMediaFileService>()
  137. .Setup(v => v.GetFilesBySeries(1))
  138. .Returns(episodeFiles);
  139. GivenFileExists();
  140. GivenSuccessfulScan();
  141. GivenFailedScan(Path.Combine(_series.Path, "media2.mkv"));
  142. Subject.Handle(new SeriesScannedEvent(_series));
  143. Mocker.GetMock<IVideoFileInfoReader>()
  144. .Verify(v => v.GetMediaInfo(Path.Combine(_series.Path, "media.mkv")), Times.Exactly(1));
  145. Mocker.GetMock<IMediaFileService>()
  146. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Exactly(1));
  147. }
  148. [Test]
  149. public void should_not_update_files_if_media_info_disabled()
  150. {
  151. var episodeFiles = Builder<EpisodeFile>.CreateListOfSize(2)
  152. .All()
  153. .With(v => v.RelativePath = "media.mkv")
  154. .TheFirst(1)
  155. .With(v => v.RelativePath = "media2.mkv")
  156. .BuildList();
  157. Mocker.GetMock<IMediaFileService>()
  158. .Setup(v => v.GetFilesBySeries(1))
  159. .Returns(episodeFiles);
  160. Mocker.GetMock<IConfigService>()
  161. .SetupGet(s => s.EnableMediaInfo)
  162. .Returns(false);
  163. GivenFileExists();
  164. GivenSuccessfulScan();
  165. Subject.Handle(new SeriesScannedEvent(_series));
  166. Mocker.GetMock<IVideoFileInfoReader>()
  167. .Verify(v => v.GetMediaInfo(It.IsAny<string>()), Times.Never());
  168. Mocker.GetMock<IMediaFileService>()
  169. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Never());
  170. }
  171. [Test]
  172. public void should_not_update_if_media_info_disabled()
  173. {
  174. var episodeFile = Builder<EpisodeFile>.CreateNew()
  175. .With(v => v.RelativePath = "media.mkv")
  176. .Build();
  177. Mocker.GetMock<IConfigService>()
  178. .SetupGet(s => s.EnableMediaInfo)
  179. .Returns(false);
  180. GivenFileExists();
  181. GivenSuccessfulScan();
  182. Subject.Update(episodeFile, _series);
  183. Mocker.GetMock<IVideoFileInfoReader>()
  184. .Verify(v => v.GetMediaInfo(It.IsAny<string>()), Times.Never());
  185. Mocker.GetMock<IMediaFileService>()
  186. .Verify(v => v.Update(It.IsAny<EpisodeFile>()), Times.Never());
  187. }
  188. [Test]
  189. public void should_update_media_info()
  190. {
  191. var episodeFile = Builder<EpisodeFile>.CreateNew()
  192. .With(v => v.RelativePath = "media.mkv")
  193. .With(e => e.MediaInfo = new MediaInfoModel{SchemaRevision = 3})
  194. .Build();
  195. GivenFileExists();
  196. GivenSuccessfulScan();
  197. Subject.Update(episodeFile, _series);
  198. Mocker.GetMock<IVideoFileInfoReader>()
  199. .Verify(v => v.GetMediaInfo(Path.Combine(_series.Path, "media.mkv")), Times.Once());
  200. Mocker.GetMock<IMediaFileService>()
  201. .Verify(v => v.Update(episodeFile), Times.Once());
  202. }
  203. [Test]
  204. public void should_not_update_media_info_if_new_info_is_null()
  205. {
  206. var episodeFile = Builder<EpisodeFile>.CreateNew()
  207. .With(v => v.RelativePath = "media.mkv")
  208. .With(e => e.MediaInfo = new MediaInfoModel{SchemaRevision = 3})
  209. .Build();
  210. GivenFileExists();
  211. GivenFailedScan(Path.Combine(_series.Path, "media.mkv"));
  212. Subject.Update(episodeFile, _series);
  213. episodeFile.MediaInfo.Should().NotBeNull();
  214. }
  215. [Test]
  216. public void should_not_save_episode_file_if_new_info_is_null()
  217. {
  218. var episodeFile = Builder<EpisodeFile>.CreateNew()
  219. .With(v => v.RelativePath = "media.mkv")
  220. .With(e => e.MediaInfo = new MediaInfoModel{SchemaRevision = 3})
  221. .Build();
  222. GivenFileExists();
  223. GivenFailedScan(Path.Combine(_series.Path, "media.mkv"));
  224. Subject.Update(episodeFile, _series);
  225. Mocker.GetMock<IMediaFileService>()
  226. .Verify(v => v.Update(episodeFile), Times.Never());
  227. }
  228. }
  229. }