using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FizzWare.NBuilder; using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Core.Books; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Indexers; using NzbDrone.Core.IndexerSearch; using NzbDrone.Core.IndexerSearch.Definitions; using NzbDrone.Core.Test.Framework; namespace NzbDrone.Core.Test.IndexerSearchTests { public class ReleaseSearchServiceFixture : CoreTest { private Mock _mockIndexer; private Author _author; private Book _firstBook; [SetUp] public void SetUp() { _mockIndexer = Mocker.GetMock(); _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1 }); _mockIndexer.SetupGet(s => s.SupportsSearch).Returns(true); Mocker.GetMock() .Setup(s => s.AutomaticSearchEnabled(true)) .Returns(new List { _mockIndexer.Object }); Mocker.GetMock() .Setup(s => s.GetSearchDecision(It.IsAny>(), It.IsAny())) .Returns(new List()); _author = Builder.CreateNew() .With(v => v.Monitored = true) .Build(); _firstBook = Builder.CreateNew() .With(e => e.Author = _author) .Build(); var edition = Builder.CreateNew() .With(e => e.Book = _firstBook) .With(e => e.Monitored = true) .Build(); _firstBook.Editions = new List { edition }; Mocker.GetMock() .Setup(v => v.GetAuthor(_author.Id)) .Returns(_author); } private List WatchForSearchCriteria() { var result = new List(); _mockIndexer.Setup(v => v.Fetch(It.IsAny())) .Callback(s => result.Add(s)) .Returns(Task.FromResult>(new List())); return result; } [Test] public async Task Tags_IndexerTags_AuthorNoTags_IndexerNotIncluded() { _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1, Tags = new HashSet { 3 } }); var allCriteria = WatchForSearchCriteria(); await Subject.BookSearch(_firstBook, false, true, false); var criteria = allCriteria.OfType().ToList(); criteria.Count.Should().Be(0); } [Test] public async Task Tags_IndexerNoTags_AuthorTags_IndexerIncluded() { _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1 }); _author = Builder.CreateNew() .With(v => v.Monitored = true) .With(v => v.Tags = new HashSet { 3 }) .Build(); Mocker.GetMock() .Setup(v => v.GetAuthor(_author.Id)) .Returns(_author); var allCriteria = WatchForSearchCriteria(); await Subject.BookSearch(_firstBook, false, true, false); var criteria = allCriteria.OfType().ToList(); criteria.Count.Should().Be(1); } [Test] public async Task Tags_IndexerAndAuthorTagsMatch_IndexerIncluded() { _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1, Tags = new HashSet { 1, 2, 3 } }); _author = Builder.CreateNew() .With(v => v.Monitored = true) .With(v => v.Tags = new HashSet { 3, 4, 5 }) .Build(); Mocker.GetMock() .Setup(v => v.GetAuthor(_author.Id)) .Returns(_author); var allCriteria = WatchForSearchCriteria(); await Subject.BookSearch(_firstBook, false, true, false); var criteria = allCriteria.OfType().ToList(); criteria.Count.Should().Be(1); } [Test] public async Task Tags_IndexerAndAuthorTagsMismatch_IndexerNotIncluded() { _mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1, Tags = new HashSet { 1, 2, 3 } }); _author = Builder.CreateNew() .With(v => v.Monitored = true) .With(v => v.Tags = new HashSet { 4, 5, 6 }) .Build(); Mocker.GetMock() .Setup(v => v.GetAuthor(_author.Id)) .Returns(_author); var allCriteria = WatchForSearchCriteria(); await Subject.BookSearch(_firstBook, false, true, false); var criteria = allCriteria.OfType().ToList(); criteria.Count.Should().Be(0); } } }