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

Merge branch 'markus101'

This commit is contained in:
kay.one 2011-07-27 16:05:08 -07:00
commit 28b840766e
3 changed files with 249 additions and 17 deletions

View File

@ -47,7 +47,6 @@ public void GetEpisodes_exists()
episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries); episode.Series.ShouldHave().AllProperties().EqualTo(fakeSeries);
} }
[Test] [Test]
public void GetEpisodes_by_season_episode_exists() public void GetEpisodes_by_season_episode_exists()
{ {
@ -336,8 +335,9 @@ public void new_episodes_only_calls_Insert()
var currentEpisodes = new List<Episode>(); var currentEpisodes = new List<Episode>();
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
var db = MockLib.GetEmptyDatabase();
mocker.SetConstant(db);
mocker.GetMock<TvDbProvider>(MockBehavior.Strict) mocker.GetMock<TvDbProvider>(MockBehavior.Strict)
.Setup(c => c.GetSeries(seriesId, true)) .Setup(c => c.GetSeries(seriesId, true))
@ -586,19 +586,125 @@ public void IsSeasonIgnored_should_return_false_if_some_of_episodes_are_ignored(
} }
[Test] [Test]
public void IsSeasonIgnored_should_return_true_if_invalid_series() public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 3)
.Have(c => c.Ignored = true)
.Build();
episodes.ToList().ForEach(c => db.Insert(c));
//Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 2);
//Assert
result.Should().BeFalse();
}
[Test]
public void IsSeasonIgnored_should_return_true_if_zero_episodes_in_db_for_season_and_previous_is_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 3)
.Have(c => c.Ignored = true)
.Build();
episodes.ToList().ForEach(c => db.Insert(c));
//Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 4);
//Assert
result.Should().BeTrue();
}
[Test]
public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season_and_previous_is_not_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 3)
.Have(c => c.Ignored = false)
.Build();
episodes.ToList().ForEach(c => db.Insert(c));
//Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 4);
//Assert
result.Should().BeFalse();
}
[Test]
public void IsSeasonIgnored_should_return_false_if_zero_episodes_in_db_for_season_one()
{ {
var db = MockLib.GetEmptyDatabase(); var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict); var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db); mocker.SetConstant(db);
//Act //Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 2); var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 1);
//Assert
result.Should().BeFalse();
}
[Test]
public void IsSeasonIgnored_should_return_true_if_zero_episodes_in_db_for_season_zero()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db);
//Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 0);
//Assert //Assert
result.Should().BeTrue(); result.Should().BeTrue();
} }
[Test]
public void IsSeasonIgnored_should_return_false_if_season_zero_is_not_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer(MockBehavior.Strict);
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 0)
.Have(c => c.Ignored = false)
.Build();
episodes.ToList().ForEach(c => db.Insert(c));
//Act
var result = mocker.Resolve<EpisodeProvider>().IsIgnored(10, 0);
//Assert
result.Should().BeFalse();
}
[Test] [Test]
[Explicit] [Explicit]
public void Add_daily_show_episodes() public void Add_daily_show_episodes()
@ -643,7 +749,6 @@ public void GetEpisode_by_Season_Episode_none_existing()
episode.Should().BeNull(); episode.Should().BeNull();
} }
[Test] [Test]
public void GetEpisode_by_Season_Episode_with_EpisodeFile() public void GetEpisode_by_Season_Episode_with_EpisodeFile()
{ {
@ -672,8 +777,6 @@ public void GetEpisode_by_Season_Episode_with_EpisodeFile()
episode.EpisodeFile.Should().NotBeNull(); episode.EpisodeFile.Should().NotBeNull();
} }
[Test] [Test]
public void GetEpisode_by_Season_Episode_without_EpisodeFile() public void GetEpisode_by_Season_Episode_without_EpisodeFile()
{ {
@ -700,9 +803,6 @@ public void GetEpisode_by_Season_Episode_without_EpisodeFile()
episode.EpisodeFile.Should().BeNull(); episode.EpisodeFile.Should().BeNull();
} }
[Test] [Test]
public void GetEpisode_by_AirDate_with_EpisodeFile() public void GetEpisode_by_AirDate_with_EpisodeFile()
{ {
@ -781,8 +881,115 @@ public void MarkEpisodeAsFetched()
episodes.Where(e => e.GrabDate == null).Should().HaveCount(4); episodes.Where(e => e.GrabDate == null).Should().HaveCount(4);
} }
[Test]
public void AddEpisode_episode_is_ignored_when_full_season_is_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer();
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 1)
.Have(c => c.Ignored = true)
.Build().ToList();
episodes.ForEach(c => db.Insert(c));
var newEpisode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = 10)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 8)
.With(e => e.SeasonNumber = 1)
.With(e => e.Ignored = false)
.Build();
//Act
mocker.Resolve<EpisodeProvider>().AddEpisode(newEpisode);
//Assert
var episodesInDb = db.Fetch<Episode>(@"SELECT * FROM Episodes");
episodesInDb.Should().HaveCount(5);
episodesInDb.Should().OnlyContain(e => e.Ignored);
mocker.VerifyAllMocks();
}
[Test]
public void AddEpisode_episode_is_not_ignored_when_full_season_is_not_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer();
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 1)
.Have(c => c.Ignored = false)
.Build().ToList();
episodes.ForEach(c => db.Insert(c));
var newEpisode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = 10)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 8)
.With(e => e.SeasonNumber = 1)
.With(e => e.Ignored = false)
.Build();
//Act
mocker.Resolve<EpisodeProvider>().AddEpisode(newEpisode);
//Assert
var episodesInDb = db.Fetch<Episode>(@"SELECT * FROM Episodes");
episodesInDb.Should().HaveCount(5);
episodesInDb.Should().OnlyContain(e => e.Ignored == false);
mocker.VerifyAllMocks();
}
[Test]
public void AddEpisode_episode_is_not_ignored_when_not_full_season_is_not_ignored()
{
var db = MockLib.GetEmptyDatabase();
var mocker = new AutoMoqer();
mocker.SetConstant(db);
var episodes = Builder<Episode>.CreateListOfSize(4)
.WhereAll()
.Have(c => c.SeriesId = 10)
.Have(c => c.SeasonNumber = 1)
.WhereTheFirst(2)
.Have(c => c.Ignored = false)
.AndTheRemaining()
.Have(c => c.Ignored = true)
.Build().ToList();
episodes.ForEach(c => db.Insert(c));
var newEpisode = Builder<Episode>.CreateNew()
.With(e => e.SeriesId = 10)
.With(e => e.SeasonNumber = 1)
.With(e => e.EpisodeNumber = 8)
.With(e => e.SeasonNumber = 1)
.With(e => e.Ignored = false)
.Build();
//Act
mocker.Resolve<EpisodeProvider>().AddEpisode(newEpisode);
//Assert
var episodesInDb = db.Fetch<Episode>(@"SELECT * FROM Episodes");
episodesInDb.Should().HaveCount(5);
episodesInDb.Where(e => e.EpisodeNumber == 8 && !e.Ignored).Should().HaveCount(1);
mocker.VerifyAllMocks();
}
} }
} }

View File

@ -36,6 +36,10 @@ public override Func<object, object> GetFromDbConverter(Type destinationType, Ty
public override Func<object, object> GetFromDbConverter(PropertyInfo propertyInfo, Type sourceType) public override Func<object, object> GetFromDbConverter(PropertyInfo propertyInfo, Type sourceType)
{ {
//Only needed if using dynamic as the return type from DB, not implemented currently as it has no use right now
//if (propertyInfo == null)
// return null;
return GetFromDbConverter(propertyInfo.PropertyType, sourceType); return GetFromDbConverter(propertyInfo.PropertyType, sourceType);
} }
} }

View File

@ -30,6 +30,10 @@ public EpisodeProvider()
public virtual void AddEpisode(Episode episode) public virtual void AddEpisode(Episode episode)
{ {
//If Season is ignored ignore this episode
if (IsIgnored(episode.SeriesId, episode.SeasonNumber))
episode.Ignored = true;
_database.Insert(episode); _database.Insert(episode);
} }
@ -105,14 +109,12 @@ public virtual IList<Episode> GetEpisodesBySeason(long seriesId, int seasonNumbe
return episodes; return episodes;
} }
public virtual void MarkEpisodeAsFetched(int episodeId) public virtual void MarkEpisodeAsFetched(int episodeId)
{ {
Logger.Trace("Marking episode {0} as fetched.", episodeId); Logger.Trace("Marking episode {0} as fetched.", episodeId);
_database.Execute("UPDATE Episodes SET GrabDate=@0 WHERE EpisodeId=@1", DateTime.Now, episodeId); _database.Execute("UPDATE Episodes SET GrabDate=@0 WHERE EpisodeId=@1", DateTime.Now, episodeId);
} }
public virtual IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult, Boolean autoAddNew = false) public virtual IList<Episode> GetEpisodesByParseResult(EpisodeParseResult parseResult, Boolean autoAddNew = false)
{ {
var result = new List<Episode>(); var result = new List<Episode>();
@ -238,7 +240,7 @@ public virtual void RefreshEpisodeInfo(Series series)
using (var tran = _database.GetTransaction()) using (var tran = _database.GetTransaction())
{ {
newList.ForEach(episode => _database.Insert(episode)); newList.ForEach(AddEpisode);
updateList.ForEach(episode => _database.Update(episode)); updateList.ForEach(episode => _database.Update(episode));
//Shouldn't run if Database is a mock since transaction will be null //Shouldn't run if Database is a mock since transaction will be null
@ -246,7 +248,6 @@ public virtual void RefreshEpisodeInfo(Series series)
{ {
tran.Complete(); tran.Complete();
} }
} }
Logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ", Logger.Info("Finished episode refresh for series: {0}. Successful: {1} - Failed: {2} ",
@ -260,11 +261,31 @@ public virtual void UpdateEpisode(Episode episode)
public virtual bool IsIgnored(int seriesId, int seasonNumber) public virtual bool IsIgnored(int seriesId, int seasonNumber)
{ {
var episodes =_database.Fetch<Episode>(@"SELECT * FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber);
var unIgnoredCount = _database.ExecuteScalar<int>( if (episodes == null || episodes.Count == 0)
"SELECT COUNT (*) FROM Episodes WHERE SeriesId=@0 AND SeasonNumber=@1 AND Ignored='0'", seriesId, seasonNumber); {
if (seasonNumber == 0)
return true;
return unIgnoredCount == 0; //Don't check for a previous season if season is 1
if (seasonNumber == 1)
return false;
//else
var lastSeasonsEpisodes =_database.Fetch<Episode>(@"SELECT * FROM Episodes
WHERE SeriesId=@0 AND SeasonNumber=@1", seriesId, seasonNumber - 1);
if (lastSeasonsEpisodes !=null && lastSeasonsEpisodes.Count > 0 && lastSeasonsEpisodes.Count == lastSeasonsEpisodes.Where(e => e.Ignored).Count())
return true;
return false;
}
if (episodes.Count == episodes.Where(e => e.Ignored).Count())
return true;
return false;
} }
public virtual IList<int> GetSeasons(int seriesId) public virtual IList<int> GetSeasons(int seriesId)