context: using System.Collections.Generic;
  using System.Linq;
  using FluentAssertions;
  using Moq;
  using NUnit.Framework;
  using NzbDrone.Core.Download;
  using NzbDrone.Core.Download.TrackedDownloads;
  using NzbDrone.Core.History;
  using NzbDrone.Core.Indexers;
  using NzbDrone.Core.Movies;
  using NzbDrone.Core.Movies.Events;
  using NzbDrone.Core.Parser;
  using NzbDrone.Core.Parser.Model;
  using NzbDrone.Core.Test.Framework;
  
  namespace NzbDrone.Core.Test.Download.TrackedDownloads
  {
      [TestFixture]
      public class TrackedDownloadServiceFixture : CoreTest<TrackedDownloadService>
      {
          [SetUp]
          public void Setup()
          {
          }
  
          private void GivenDownloadHistory()
          {
              Mocker.GetMock<IHistoryService>()
                  .Setup(s => s.FindByDownloadId(It.Is<string>(sr => sr == "35238")))
                  .Returns(new List<MovieHistory>()
                  {
                      new MovieHistory()
                      {
                          DownloadId = "35238",
                          SourceTitle = "TV Series S01",
                          MovieId = 3,
                      }
                  });
          }
  
          [Test]
          public void should_track_downloads_using_the_source_title_if_it_cannot_be_found_using_the_download_title()
          {
              GivenDownloadHistory();
  
              var remoteEpisode = new RemoteMovie
              {
                  Movie = new Movie() { Id = 3 },
  
                  ParsedMovieInfo = new ParsedMovieInfo()
                  {
                      MovieTitles = new List<string> { "A Movie" },
                      Year = 1998
                  }
              };
  
              Mocker.GetMock<IParsingService>()
                    .Setup(s => s.Map(It.Is<ParsedMovieInfo>(i => i.PrimaryMovieTitle == "A Movie"), It.IsAny<string>(), null))
                    .Returns(new MappingResult { RemoteMovie = remoteEpisode });
  
              ParseMovieTitle();
  
              var client = new DownloadClientDefinition()
              {
                  Id = 1,
                  Protocol = DownloadProtocol.Torrent
              };
  
              var item = new DownloadClientItem()
              {
                  Title = "A Movie 1998",
                  DownloadId = "35238",
                  DownloadClientInfo = new DownloadClientItemClientInfo
                  {
                      Protocol = client.Protocol,
                      Id = client.Id,
                      Name = client.Name
                  }
              };
  
              var trackedDownload = Subject.TrackDownload(client, item);
  
              trackedDownload.Should().NotBeNull();
              trackedDownload.RemoteMovie.Should().NotBeNull();
              trackedDownload.RemoteMovie.Movie.Should().NotBeNull();
              trackedDownload.RemoteMovie.Movie.Id.Should().Be(3);
          }
  
          [Test]
          public void should_unmap_tracked_download_if_movie_deleted()
          {
              GivenDownloadHistory();
  
              var remoteMovie = new RemoteMovie
              {
                  Movie = new Movie() { Id = 3 },
  
                  ParsedMovieInfo = new ParsedMovieInfo()
                  {
                      MovieTitles = { "A Movie" },
                      Year = 1998
                  }
              };
  
              Mocker.GetMock<IParsingService>()
                    .Setup(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), null))
                    .Returns(new MappingResult { RemoteMovie = remoteMovie });
  
              Mocker.GetMock<IHistoryService>()
                    .Setup(s => s.FindByDownloadId(It.IsAny<string>()))
                    .Returns(new List<MovieHistory>());
  
              ParseMovieTitle();
  
              var client = new DownloadClientDefinition()
              {
                  Id = 1,
                  Protocol = DownloadProtocol.Torrent
              };
  
              var item = new DownloadClientItem()
              {
                  Title = "A Movie 1998",
                  DownloadId = "12345",
                  DownloadClientInfo = new DownloadClientItemClientInfo
                  {
                      Id = 1,
                      Type = "Blackhole",
                      Name = "Blackhole Client",
                      Protocol = DownloadProtocol.Torrent
                  }
              };
  
              Subject.TrackDownload(client, item);
              Subject.GetTrackedDownloads().Should().HaveCount(1);
  
              Mocker.GetMock<IParsingService>()
                    .Setup(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), null))
                    .Returns(new MappingResult { MappingResultType = MappingResultType.Unknown });
  
              Subject.Handle(new MoviesDeletedEvent(new List<Movie> { remoteMovie.Movie }, false, false));
  
              var trackedDownloads = Subject.GetTrackedDownloads();
              trackedDownloads.Should().HaveCount(1);
              trackedDownloads.First().RemoteMovie.Should().BeNull();
          }
  
          [Test]
          public void should_not_throw_when_processing_deleted_movie()
          {
              GivenDownloadHistory();
  
              var remoteMovie = new RemoteMovie
              {
                  Movie = new Movie() { Id = 3 },
  
                  ParsedMovieInfo = new ParsedMovieInfo()
                  {
                      MovieTitles = { "A Movie" },
                      Year = 1998
                  }
              };
  
              Mocker.GetMock<IParsingService>()
                    .Setup(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), null))
                    .Returns(new MappingResult { MappingResultType = MappingResultType.Unknown });
  
              Mocker.GetMock<IHistoryService>()
                    .Setup(s => s.FindByDownloadId(It.IsAny<string>()))
                    .Returns(new List<MovieHistory>());
  
              ParseMovieTitle();
  
              var client = new DownloadClientDefinition()
              {
                  Id = 1,
                  Protocol = DownloadProtocol.Torrent
              };
  
              var item = new DownloadClientItem()
              {
                  Title = "A Movie 1998",
                  DownloadId = "12345",
                  DownloadClientInfo = new DownloadClientItemClientInfo
                  {
                      Id = 1,
                      Type = "Blackhole",
                      Name = "Blackhole Client",
                      Protocol = DownloadProtocol.Torrent
                  }
              };
  
              Subject.TrackDownload(client, item);
              Subject.GetTrackedDownloads().Should().HaveCount(1);
  
              Mocker.GetMock<IParsingService>()
                    .Setup(s => s.Map(It.IsAny<ParsedMovieInfo>(), It.IsAny<string>(), null))
                    .Returns(new MappingResult { MappingResultType = MappingResultType.Unknown });
  
              Subject.Handle(new MoviesDeletedEvent(new List<Movie> { remoteMovie.Movie }, false, false));
  
  
gt:             var trackedDownloads = Subject.GetTrackedDownloads();