public void ExtractReleaseIdentity_SubtitleWithAllProperties_ReleaseIdentityMatches(
            Subtitle subtitle,
            EpisodeParser sut)
        {
            var expected = new TvReleaseIdentity
            {
                SeriesName = subtitle.SeriesName,
                Season = subtitle.Season.Value,
                Episode = subtitle.Episode.Value,
                EndEpisode = subtitle.EndEpisode.Value,
                ReleaseGroup = subtitle.ReleaseGroup
            };
            var result = sut.ExtractReleaseIdentity(subtitle);

            Assert.That(result, Is.EqualTo(expected));
        }
        public void DownloadSubtitle_MatchingSubtitlesDownloaderFails_ShouldReturnFalse(
            string file,
            Language[] languages,
            TvReleaseIdentity identity,
            Subtitle[] matches,
            Language language,
            ISubtitleRanker subtitleRanker,
            IEnhancementProvider enhancementProvider,
            [Frozen]IEpisodeSubtitleDownloader fakeDownloader)
        {
            A.CallTo(() => fakeDownloader.SearchSubtitle(A<TvReleaseIdentity>._, A<Language[]>._)).Returns(matches);
            A.CallTo(() => fakeDownloader.TryDownloadSubtitle(A<Subtitle>._, A<string>._)).Returns(false);
            A.CallTo(() => fakeDownloader.CanHandleAtLeastOneOf(A<Language[]>._)).Returns(true);
            var downloaders = new[] { fakeDownloader };
            var service = new SubtitleDownloadService(downloaders, enhancementProvider, subtitleRanker);

            bool success = service.DownloadSubtitle(file, identity, languages);

            Assert.That(success, Is.False);
        }
        public void DownloadSubtitle_MatchingSubtitlesDownloaderSucceeds_DownloaderShouldDownloadFirstLanguage(
            string file,
            Subtitle[] matches,
            TvReleaseIdentity identity,
            ISubtitleRanker subtitleRanker,
            IEpisodeSubtitleDownloader fakeDownloader,
            IEnhancementProvider enhancementProvider
            )
        {
            var languages = matches.Skip(1).Select(m => m.Language);
            var expectedLanguage = languages.First();
            A.CallTo(() => fakeDownloader.SearchSubtitle(A<TvReleaseIdentity>._, A<Language[]>._)).Returns(matches);
            A.CallTo(() => fakeDownloader.TryDownloadSubtitle(A<Subtitle>._, A<string>._)).Returns(true);
            A.CallTo(() => fakeDownloader.CanHandleAtLeastOneOf(A<Language[]>._)).Returns(true);
            var downloaders = new[] { fakeDownloader };
            var service = new SubtitleDownloadService(downloaders, enhancementProvider, subtitleRanker);

            service.DownloadSubtitle(file, identity, languages);

            A.CallTo(() => fakeDownloader.TryDownloadSubtitle(A<Subtitle>.That.Matches(s => s.Language == expectedLanguage), A<string>._))
                .MustHaveHappened(Repeated.Exactly.Once);
        }
        public void ExtractReleaseIdentity_SubtitleWithAllNullValues_ReleaseIdentityMatches(
            Subtitle subtitle,
            EpisodeParser sut)
        {
            subtitle.SeriesName = null;
            subtitle.ReleaseGroup = null;
            subtitle.Season = null;
            subtitle.Episode = null;
            subtitle.EndEpisode = null;

            var expected = new TvReleaseIdentity
            {
                SeriesName = null,
                Season = 0,
                Episode = 0,
                EndEpisode = 0,
                ReleaseGroup = null
            };
            var result = sut.ExtractReleaseIdentity(subtitle);

            Assert.That(result, Is.EqualTo(expected));
        }
        public void DownloadSubtitle_DownloaderHasEnhancementRequests_ProvidesEnhancements(
            SubDbFileHashEnhancement enhancement,
            string file,
            TvReleaseIdentity identity,
            Subtitle[] matches,
            [Frozen]IEnumerable<IEpisodeSubtitleDownloader> downloaders,
            [Frozen]IEnhancementProvider enhancementProvider,
            SubtitleDownloadService sut
            )
        {
            var languages = matches.Select(m => m.Language);
            var downloaderWithRequest = downloaders.First();
            A.CallTo(() => downloaderWithRequest.CanHandleAtLeastOneOf(A<Language[]>._)).Returns(true);
            A.CallTo(() => downloaderWithRequest.EnhancementRequests)
                .Returns(new[] {new EnhancementRequest<SubDbFileHashEnhancement>()});
            A.CallTo(() => enhancementProvider.GetEnhancement(typeof(SubDbFileHashEnhancement), file, identity))
                .Returns(enhancement);

            sut.DownloadSubtitle(file, identity, languages);

            A.CallTo(() => downloaderWithRequest.SearchSubtitle(
                A<TvReleaseIdentity>.That.Matches(r => r.Enhancements.Contains(enhancement)),
                A<IEnumerable<Language>>._)).MustHaveHappened();
        }
        public void DownloadSubtitle_SomeDowloadersCantHandleLanguages_OnlyCallsDownloadersThatHandlesLanguages(
            string file,
            TvReleaseIdentity identity,
            Subtitle[] matches,
            [Frozen]IEpisodeSubtitleDownloader[] downloaders,
            SubtitleDownloadService sut
            )
        {
            var expectedDownloader = downloaders.First();
            A.CallTo(() => expectedDownloader.SearchSubtitle(A<TvReleaseIdentity>._, A<Language[]>._)).Returns(matches);
            A.CallTo(() => expectedDownloader.TryDownloadSubtitle(A<Subtitle>._, A<string>._)).Returns(true);
            A.CallTo(() => expectedDownloader.CanHandleAtLeastOneOf(A<Language[]>._)).Returns(true);

            var incapableDownloaders = downloaders.Skip(1).ToArray();
            incapableDownloaders.ForEach(incapableDownloader =>
                A.CallTo(() => incapableDownloader.CanHandleAtLeastOneOf(A<Language[]>._)).Returns(false)
            );

            var languages = matches.Select(m => m.Language);

            sut.DownloadSubtitle(file, identity, languages);

            incapableDownloaders.ForEach(incapableDownloader =>
                A.CallTo(() => incapableDownloader.SearchSubtitle(A<TvReleaseIdentity>._, A<Language[]>._))
                    .MustNotHaveHappened()
            );
        }
        public void TryDownloadSubtitle_DownloadSuccessful_ReturnsTrue(
            Subtitle subtitle,
            string resultFile,
            string fileName,
            [Frozen]ISubtitleDownloader downloader,
            SubtitleDownloaderWrapper sut)
        {
            A.CallTo(() => downloader.SaveSubtitle(A<Subtitle>._)).Returns(new [] { new FileInfo(resultFile) });

            var result = sut.TryDownloadSubtitle(subtitle, fileName);

            Assert.That(result, Is.True);
        }
        public void TryDownloadSubtitle_DownloadSuccessful_RenamesFile(
            Subtitle subtitle,
            string resultFile,
            string fileName,
            [Frozen]ISubtitleDownloader downloader,
            [Frozen]IFileOperations fileOperations,
            SubtitleDownloaderWrapper sut)
        {
            var fileInfo = new FileInfo(resultFile);
            A.CallTo(() => downloader.SaveSubtitle(A<Subtitle>._)).Returns(new List<FileInfo> { fileInfo });

            sut.TryDownloadSubtitle(subtitle, fileName);

            A.CallTo(() => fileOperations.RenameSubtitleFile(fileInfo.FullName, fileName + "." + subtitle.Language.TwoLetterIsoName + ".srt")).MustHaveHappened();
        }
        public void TryDownloadSubtitle_DownloaderThrowsException_ReturnsFalse(
            Subtitle subtitle,
            string fileName,
            [Frozen]ISubtitleDownloader downloader,
            SubtitleDownloaderWrapper sut)
        {
            A.CallTo(() => downloader.SaveSubtitle(A<Subtitle>._)).Throws<Exception>();

            bool result = sut.TryDownloadSubtitle(subtitle, fileName);

            Assert.That(result, Is.False);
        }