1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Tests for ParsingService

This commit is contained in:
Mark McDowall 2013-09-15 01:23:54 -07:00
parent 5b5ddf67f8
commit 663eeb53c1
5 changed files with 378 additions and 50 deletions

View File

@ -162,6 +162,8 @@
<Compile Include="NotificationTests\Xbmc\Json\GetSeriesPathFixture.cs" />
<Compile Include="NotificationTests\Xbmc\Json\UpdateFixture.cs" />
<Compile Include="OrganizerTests\BuildFilePathFixture.cs" />
<Compile Include="ParserTests\ParsingServiceTests\GetEpisodesFixture.cs" />
<Compile Include="ParserTests\ParsingServiceTests\MapFixture.cs" />
<Compile Include="Qualities\QualitySizeRepositoryFixture.cs" />
<Compile Include="Qualities\QualityProfileRepositoryFixture.cs" />
<Compile Include="RootFolderTests\FreeSpaceOnDrivesFixture.cs" />

View File

@ -4,11 +4,10 @@
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Contract;
using NzbDrone.Core.Indexers;
using NzbDrone.Common.Expansive;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
using NzbDrone.Common.Expansive;
namespace NzbDrone.Core.Test.ParserTests
{

View File

@ -0,0 +1,173 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.DataAugmentation.Scene;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests
{
[TestFixture]
public class GetEpisodesFixture : TestBase<ParsingService>
{
private Series _series;
private List<Episode> _episodes;
private ParsedEpisodeInfo _parsedEpisodeInfo;
private SingleEpisodeSearchCriteria _singleEpisodeSearchCriteria;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.CleanTitle = "rock")
.Build();
_episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.AirDate = DateTime.Today.ToString(Episode.AIR_DATE_FORMAT))
.Build()
.ToList();
_parsedEpisodeInfo = new ParsedEpisodeInfo
{
SeriesTitle = _series.Title,
SeasonNumber = 1,
EpisodeNumbers = new[] { 1 }
};
_singleEpisodeSearchCriteria = new SingleEpisodeSearchCriteria
{
Series = _series,
EpisodeNumber = _episodes.First().EpisodeNumber,
SeasonNumber = _episodes.First().SeasonNumber,
Episodes = _episodes
};
Mocker.GetMock<ISeriesService>()
.Setup(s => s.FindByTitle(It.IsAny<String>()))
.Returns(_series);
}
private void GivenDailySeries()
{
_series.SeriesType = SeriesTypes.Daily;
}
private void GivenDailyParseResult()
{
_parsedEpisodeInfo.AirDate = DateTime.Today;
}
private void GivenSceneNumberingSeries()
{
_series.UseSceneNumbering = true;
}
[Test]
public void should_get_daily_episode_episode_when_search_criteria_is_null()
{
GivenDailySeries();
GivenDailyParseResult();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<DateTime>()), Times.Once());
}
[Test]
public void should_use_search_criteria_episode_when_it_matches_daily()
{
GivenDailySeries();
GivenDailyParseResult();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<DateTime>()), Times.Never());
}
[Test]
public void should_fallback_to_daily_episode_lookup_when_search_criteria_episode_doesnt_match()
{
GivenDailySeries();
_parsedEpisodeInfo.AirDate = DateTime.Today.AddDays(-5);
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<DateTime>()), Times.Once());
}
[Test]
public void should_use_scene_numbering_when_series_uses_scene_numbering()
{
GivenSceneNumberingSeries();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), true), Times.Once());
}
[Test]
public void should_match_search_criteria_by_scene_numbering()
{
GivenSceneNumberingSeries();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), true), Times.Never());
}
[Test]
public void should_fallback_to_findEpisode_when_search_criteria_match_fails_for_scene_numbering()
{
GivenSceneNumberingSeries();
_episodes.First().SceneEpisodeNumber = 10;
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), true), Times.Once());
}
[Test]
public void should_find_episode()
{
Subject.Map(_parsedEpisodeInfo, _series.TvRageId);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), false), Times.Once());
}
[Test]
public void should_match_episode_with_search_criteria()
{
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), false), Times.Never());
}
[Test]
public void should_fallback_to_findEpisode_when_search_criteria_match_fails()
{
_episodes.First().EpisodeNumber = 10;
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<IEpisodeService>()
.Verify(v => v.FindEpisode(It.IsAny<Int32>(), It.IsAny<Int32>(), It.IsAny<Int32>(), false), Times.Once());
}
}
}

View File

@ -0,0 +1,153 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzWare.NBuilder;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.DataAugmentation.Scene;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests
{
[TestFixture]
public class MapFixture : TestBase<ParsingService>
{
private Series _series;
private List<Episode> _episodes;
private ParsedEpisodeInfo _parsedEpisodeInfo;
private SingleEpisodeSearchCriteria _singleEpisodeSearchCriteria;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew()
.With(s => s.Title = "30 Rock")
.With(s => s.CleanTitle = "rock")
.Build();
_episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.AirDate = DateTime.Today.ToString(Episode.AIR_DATE_FORMAT))
.Build()
.ToList();
_parsedEpisodeInfo = new ParsedEpisodeInfo
{
SeriesTitle = _series.Title,
SeasonNumber = 1,
EpisodeNumbers = new[] { 1 }
};
_singleEpisodeSearchCriteria = new SingleEpisodeSearchCriteria
{
Series = _series,
EpisodeNumber = _episodes.First().EpisodeNumber,
SeasonNumber = _episodes.First().SeasonNumber,
Episodes = _episodes
};
}
private void GivenMatchBySeriesTitle()
{
Mocker.GetMock<ISeriesService>()
.Setup(s => s.FindByTitle(It.IsAny<String>()))
.Returns(_series);
}
private void GivenMatchByTvRageId()
{
Mocker.GetMock<ISeriesService>()
.Setup(s => s.FindByTvRageId(It.IsAny<Int32>()))
.Returns(_series);
}
private void GivenParseResultSeriesDoesntMatchSearchCriteria()
{
_parsedEpisodeInfo.SeriesTitle = "Another Name";
}
[Test]
public void should_lookup_series_by_name()
{
GivenMatchBySeriesTitle();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTitle(It.IsAny<String>()), Times.Once());
}
[Test]
public void should_use_tvrageid_when_series_title_lookup_fails()
{
GivenMatchByTvRageId();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTvRageId(It.IsAny<Int32>()), Times.Once());
}
[Test]
public void should_use_search_criteria_series_title()
{
GivenMatchBySeriesTitle();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTitle(It.IsAny<String>()), Times.Never());
}
[Test]
public void should_FindByTitle_when_search_criteria_matching_fails()
{
GivenParseResultSeriesDoesntMatchSearchCriteria();
Subject.Map(_parsedEpisodeInfo, 10, _singleEpisodeSearchCriteria);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTitle(It.IsAny<String>()), Times.Once());
}
[Test]
public void should_FindByTvRageId_when_search_criteria_and_FIndByTitle_matching_fails()
{
GivenParseResultSeriesDoesntMatchSearchCriteria();
Subject.Map(_parsedEpisodeInfo, 10, _singleEpisodeSearchCriteria);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTvRageId(It.IsAny<Int32>()), Times.Once());
}
[Test]
public void should_use_tvdbid_matching_when_alias_is_found()
{
Mocker.GetMock<ISceneMappingService>()
.Setup(s => s.GetTvDbId(It.IsAny<String>()))
.Returns(_series.TvdbId);
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTitle(It.IsAny<String>()), Times.Never());
}
[Test]
public void should_use_tvrageid_match_from_search_criteria_when_title_match_fails()
{
GivenParseResultSeriesDoesntMatchSearchCriteria();
Subject.Map(_parsedEpisodeInfo, _series.TvRageId, _singleEpisodeSearchCriteria);
Mocker.GetMock<ISeriesService>()
.Verify(v => v.FindByTitle(It.IsAny<String>()), Times.Never());
}
}
}

View File

@ -15,6 +15,7 @@ public interface IParsingService
LocalEpisode GetEpisodes(string filename, Series series, bool sceneSource);
Series GetSeries(string title);
RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria = null);
List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series, bool sceneSource, SearchCriteriaBase searchCriteria = null);
}
public class ParsingService : IParsingService
@ -99,52 +100,7 @@ public RemoteEpisode Map(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, Sear
return remoteEpisode;
}
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria)
{
var tvdbId = _sceneMappingService.GetTvDbId(parsedEpisodeInfo.SeriesTitle);
if (tvdbId.HasValue)
{
if (searchCriteria.Series.TvdbId == tvdbId)
{
return searchCriteria.Series;
}
}
if (parsedEpisodeInfo.SeriesTitle.CleanSeriesTitle() == searchCriteria.Series.CleanTitle)
{
return searchCriteria.Series;
}
if (tvRageId == searchCriteria.Series.TvRageId)
{
//TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import
return searchCriteria.Series;
}
return GetSeries(parsedEpisodeInfo, tvRageId);
}
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId)
{
var series = _seriesService.FindByTitle(parsedEpisodeInfo.SeriesTitle);
if (series == null && tvRageId > 0)
{
//TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import
series = _seriesService.FindByTvRageId(tvRageId);
}
if (series == null)
{
_logger.Trace("No matching series {0}", parsedEpisodeInfo.SeriesTitle);
return null;
}
return series;
}
private List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series, bool sceneSource, SearchCriteriaBase searchCriteria = null)
public List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series series, bool sceneSource, SearchCriteriaBase searchCriteria = null)
{
var result = new List<Episode>();
@ -205,7 +161,7 @@ private List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series se
if (episodeInfo == null)
{
episodeInfo = _episodeService.GetEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber);
episodeInfo = _episodeService.FindEpisode(series.Id, parsedEpisodeInfo.SeasonNumber, episodeNumber);
}
if (episodeInfo != null)
@ -222,6 +178,51 @@ private List<Episode> GetEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, Series se
return result;
}
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId, SearchCriteriaBase searchCriteria)
{
var tvdbId = _sceneMappingService.GetTvDbId(parsedEpisodeInfo.SeriesTitle);
if (tvdbId.HasValue)
{
if (searchCriteria.Series.TvdbId == tvdbId)
{
return searchCriteria.Series;
}
}
if (parsedEpisodeInfo.SeriesTitle.CleanSeriesTitle() == searchCriteria.Series.CleanTitle)
{
return searchCriteria.Series;
}
if (tvRageId == searchCriteria.Series.TvRageId)
{
//TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import
return searchCriteria.Series;
}
return GetSeries(parsedEpisodeInfo, tvRageId);
}
private Series GetSeries(ParsedEpisodeInfo parsedEpisodeInfo, int tvRageId)
{
var series = _seriesService.FindByTitle(parsedEpisodeInfo.SeriesTitle);
if (series == null && tvRageId > 0)
{
//TODO: If series is found by TvRageId, we should report it as a scene naming exception, since it will fail to import
series = _seriesService.FindByTvRageId(tvRageId);
}
if (series == null)
{
_logger.Trace("No matching series {0}", parsedEpisodeInfo.SeriesTitle);
return null;
}
return series;
}
private Episode GetDailyEpisode(Series series, DateTime airDate, SearchCriteriaBase searchCriteria)
{
Episode episodeInfo = null;
@ -234,7 +235,7 @@ private Episode GetDailyEpisode(Series series, DateTime airDate, SearchCriteriaB
if (episodeInfo == null)
{
episodeInfo = _episodeService.GetEpisode(series.Id, airDate);
episodeInfo = _episodeService.FindEpisode(series.Id, airDate);
}
return episodeInfo;