diff --git a/src/NzbDrone.Core.Test/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecificationFixture.cs new file mode 100644 index 000000000..6e9033078 --- /dev/null +++ b/src/NzbDrone.Core.Test/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecificationFixture.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FizzWare.NBuilder; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Download; +using NzbDrone.Core.History; +using NzbDrone.Core.MediaFiles.MovieImport.Specifications; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; + +namespace NzbDrone.Core.Test.MediaFiles.MovieImport.Specifications +{ + [TestFixture] + public class AlreadyImportedSpecificationFixture : CoreTest + { + private Movie _movie; + private LocalMovie _localMovie; + private DownloadClientItem _downloadClientItem; + + [SetUp] + public void Setup() + { + _movie = Builder.CreateNew() + .With(s => s.Path = @"C:\Test\Movies\Casablanca".AsOsAgnostic()) + .Build(); + + _localMovie = new LocalMovie + { + Path = @"C:\Test\Unsorted\Casablanca\Casablanca.1942.avi".AsOsAgnostic(), + Movie = _movie + }; + + _downloadClientItem = Builder.CreateNew() + .Build(); + } + + private void GivenHistory(List history) + { + Mocker.GetMock() + .Setup(s => s.GetByMovieId(It.IsAny(), null)) + .Returns(history); + } + + [Test] + public void should_accepted_if_download_client_item_is_null() + { + Subject.IsSatisfiedBy(_localMovie, null).Accepted.Should().BeTrue(); + } + + [Test] + public void should_accept_if_episode_does_not_have_file() + { + _movie.MovieFileId = 0; + + Subject.IsSatisfiedBy(_localMovie, _downloadClientItem).Accepted.Should().BeTrue(); + } + + [Test] + public void should_accept_if_episode_has_not_been_imported() + { + var history = Builder.CreateListOfSize(1) + .All() + .With(h => h.MovieId = _movie.Id) + .With(h => h.EventType = MovieHistoryEventType.Grabbed) + .Build() + .ToList(); + + GivenHistory(history); + + Subject.IsSatisfiedBy(_localMovie, _downloadClientItem).Accepted.Should().BeTrue(); + } + + [Test] + public void should_accept_if_episode_was_grabbed_after_being_imported() + { + var history = Builder.CreateListOfSize(3) + .All() + .With(h => h.MovieId = _movie.Id) + .TheFirst(1) + .With(h => h.EventType = MovieHistoryEventType.Grabbed) + .With(h => h.Date = DateTime.UtcNow) + .TheNext(1) + .With(h => h.EventType = MovieHistoryEventType.DownloadFolderImported) + .With(h => h.Date = DateTime.UtcNow.AddDays(-1)) + .TheNext(1) + .With(h => h.EventType = MovieHistoryEventType.Grabbed) + .With(h => h.Date = DateTime.UtcNow.AddDays(-2)) + .Build() + .ToList(); + + GivenHistory(history); + + Subject.IsSatisfiedBy(_localMovie, _downloadClientItem).Accepted.Should().BeTrue(); + } + + [Test] + public void should_reject_if_episode_imported_after_being_grabbed() + { + var history = Builder.CreateListOfSize(2) + .All() + .With(h => h.MovieId = _movie.Id) + .TheFirst(1) + .With(h => h.EventType = MovieHistoryEventType.DownloadFolderImported) + .With(h => h.Date = DateTime.UtcNow.AddDays(-1)) + .TheNext(1) + .With(h => h.EventType = MovieHistoryEventType.Grabbed) + .With(h => h.Date = DateTime.UtcNow.AddDays(-2)) + .Build() + .ToList(); + + GivenHistory(history); + + Subject.IsSatisfiedBy(_localMovie, _downloadClientItem).Accepted.Should().BeTrue(); + } + } +} diff --git a/src/NzbDrone.Core/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecification.cs b/src/NzbDrone.Core/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecification.cs index 0c37af222..a466c018a 100644 --- a/src/NzbDrone.Core/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/MovieImport/Specifications/AlreadyImportedSpecification.cs @@ -39,8 +39,11 @@ namespace NzbDrone.Core.MediaFiles.MovieImport.Specifications } var movieImportedHistory = _historyService.GetByMovieId(movie.Id, null); - var lastImported = movieImportedHistory.FirstOrDefault(h => h.EventType == MovieHistoryEventType.DownloadFolderImported); - var lastGrabbed = movieImportedHistory.FirstOrDefault(h => h.EventType == MovieHistoryEventType.Grabbed); + var lastImported = movieImportedHistory.FirstOrDefault(h => + h.DownloadId == downloadClientItem.DownloadId && + h.EventType == MovieHistoryEventType.DownloadFolderImported); + var lastGrabbed = movieImportedHistory.FirstOrDefault(h => + h.DownloadId == downloadClientItem.DownloadId && h.EventType == MovieHistoryEventType.Grabbed); if (lastImported == null) { @@ -48,17 +51,21 @@ namespace NzbDrone.Core.MediaFiles.MovieImport.Specifications return Decision.Accept(); } - // If the release was grabbed again after importing don't reject it - if (lastGrabbed != null && lastGrabbed.Date.After(lastImported.Date)) + if (lastGrabbed != null) { - _logger.Trace("Movie file was grabbed again after importing"); - return Decision.Accept(); - } + // If the release was grabbed again after importing don't reject it + if (lastGrabbed.Date.After(lastImported.Date)) + { + _logger.Trace("Movie file was grabbed again after importing"); + return Decision.Accept(); + } - if (lastImported.DownloadId == downloadClientItem.DownloadId) - { - _logger.Debug("Movie file previously imported at {0}", lastImported.Date); - return Decision.Reject("Movie file already imported at {0}", lastImported.Date.ToLocalTime()); + // If the release was imported after the last grab reject it + if (lastImported.Date.After(lastGrabbed.Date)) + { + _logger.Debug("Movie file previously imported at {0}", lastImported.Date); + return Decision.Reject("Movie file already imported at {0}", lastImported.Date.ToLocalTime()); + } } return Decision.Accept();