using System; using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Core.Books; using NzbDrone.Core.DecisionEngine.Specifications; using NzbDrone.Core.DecisionEngine.Specifications.RssSync; using NzbDrone.Core.Download.Pending; using NzbDrone.Core.Indexers; using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Profiles.Delay; using NzbDrone.Core.Profiles.Qualities; using NzbDrone.Core.Qualities; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.DecisionEngineTests.RssSync { [TestFixture] public class DelaySpecificationFixture : CoreTest { private QualityProfile _profile; private DelayProfile _delayProfile; private RemoteBook _remoteBook; [SetUp] public void Setup() { _profile = Builder.CreateNew() .Build(); _delayProfile = Builder.CreateNew() .With(d => d.PreferredProtocol = DownloadProtocol.Usenet) .Build(); var author = Builder.CreateNew() .With(s => s.QualityProfile = _profile) .Build(); _remoteBook = Builder.CreateNew() .With(r => r.Author = author) .Build(); _profile.Items = new List(); _profile.Items.Add(new QualityProfileQualityItem { Allowed = true, Quality = Quality.PDF }); _profile.Items.Add(new QualityProfileQualityItem { Allowed = true, Quality = Quality.AZW3 }); _profile.Items.Add(new QualityProfileQualityItem { Allowed = true, Quality = Quality.MP3_320 }); _profile.Cutoff = Quality.AZW3.Id; _remoteBook.ParsedBookInfo = new ParsedBookInfo(); _remoteBook.Release = new ReleaseInfo(); _remoteBook.Release.DownloadProtocol = DownloadProtocol.Usenet; _remoteBook.Books = Builder.CreateListOfSize(1).Build().ToList(); Mocker.GetMock() .Setup(s => s.GetFilesByBook(It.IsAny())) .Returns(new List { }); Mocker.GetMock() .Setup(s => s.BestForTags(It.IsAny>())) .Returns(_delayProfile); Mocker.GetMock() .Setup(s => s.GetPendingRemoteBooks(It.IsAny())) .Returns(new List()); } private void GivenExistingFile(QualityModel quality) { Mocker.GetMock() .Setup(s => s.GetFilesByBook(It.IsAny())) .Returns(new List { new BookFile { Quality = quality } }); } private void GivenUpgradeForExistingFile() { Mocker.GetMock() .Setup(s => s.IsUpgradable(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(true); } [Test] public void should_be_true_when_user_invoked_search() { Subject.IsSatisfiedBy(new RemoteBook(), new BookSearchCriteria { UserInvokedSearch = true }).Accepted.Should().BeTrue(); } [Test] public void should_be_false_when_system_invoked_search_and_release_is_younger_than_delay() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MOBI); _remoteBook.Release.PublishDate = DateTime.UtcNow; _delayProfile.UsenetDelay = 720; Subject.IsSatisfiedBy(_remoteBook, new BookSearchCriteria()).Accepted.Should().BeFalse(); } [Test] public void should_be_true_when_profile_does_not_have_a_delay() { _delayProfile.UsenetDelay = 0; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeTrue(); } [Test] public void should_be_true_when_quality_is_last_allowed_in_profile() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MP3_320); Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeTrue(); } [Test] public void should_be_true_when_release_is_older_than_delay() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MOBI); _remoteBook.Release.PublishDate = DateTime.UtcNow.AddHours(-10); _delayProfile.UsenetDelay = 60; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeTrue(); } [Test] public void should_be_false_when_release_is_younger_than_delay() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MOBI); _remoteBook.Release.PublishDate = DateTime.UtcNow; _delayProfile.UsenetDelay = 720; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeFalse(); } [Test] public void should_be_true_when_release_is_a_proper_for_existing_book() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MP3_320, new Revision(version: 2)); _remoteBook.Release.PublishDate = DateTime.UtcNow; GivenExistingFile(new QualityModel(Quality.MP3_320)); GivenUpgradeForExistingFile(); Mocker.GetMock() .Setup(s => s.IsRevisionUpgrade(It.IsAny(), It.IsAny())) .Returns(true); _delayProfile.UsenetDelay = 720; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeTrue(); } [Test] public void should_be_true_when_release_is_a_real_for_existing_book() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.MP3_320, new Revision(real: 1)); _remoteBook.Release.PublishDate = DateTime.UtcNow; GivenExistingFile(new QualityModel(Quality.MP3_320)); GivenUpgradeForExistingFile(); Mocker.GetMock() .Setup(s => s.IsRevisionUpgrade(It.IsAny(), It.IsAny())) .Returns(true); _delayProfile.UsenetDelay = 720; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeTrue(); } [Test] public void should_be_false_when_release_is_proper_for_existing_book_of_different_quality() { _remoteBook.ParsedBookInfo.Quality = new QualityModel(Quality.AZW3, new Revision(version: 2)); _remoteBook.Release.PublishDate = DateTime.UtcNow; GivenExistingFile(new QualityModel(Quality.PDF)); _delayProfile.UsenetDelay = 720; Subject.IsSatisfiedBy(_remoteBook, null).Accepted.Should().BeFalse(); } } }