Missing count will not include unmonitored episodes

pull/4/head
Mark McDowall 12 years ago
parent 6d2090519e
commit 116913107d

@ -1,4 +1,5 @@
using FizzWare.NBuilder; using System;
using FizzWare.NBuilder;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
@ -10,41 +11,71 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
[TestFixture] [TestFixture]
public class EpisodesWithoutFilesFixture : DbTest<EpisodeRepository, Episode> public class EpisodesWithoutFilesFixture : DbTest<EpisodeRepository, Episode>
{ {
private Series _monitoredSeries;
private Series _unmonitoredSeries;
private PagingSpec<Episode> _pagingSpec;
[SetUp] [SetUp]
public void Setup() public void Setup()
{ {
var series = Builder<Series>.CreateNew() _monitoredSeries = Builder<Series>.CreateNew()
.With(s => s.Id = 0) .With(s => s.Id = 0)
.With(s => s.Runtime = 30) .With(s => s.Runtime = 30)
.With(s => s.Monitored = true) .With(s => s.Monitored = true)
.Build(); .Build();
series.Id = Db.Insert(series).Id; _unmonitoredSeries = Builder<Series>.CreateNew()
.With(s => s.Id = 0)
.With(s => s.Runtime = 30)
.With(s => s.Monitored = false)
.Build();
_monitoredSeries.Id = Db.Insert(_monitoredSeries).Id;
_unmonitoredSeries.Id = Db.Insert(_unmonitoredSeries).Id;
var episodes = Builder<Episode>.CreateListOfSize(2) _pagingSpec = new PagingSpec<Episode>
{
Page = 1,
PageSize = 10,
SortKey = "AirDate",
SortDirection = SortDirection.Ascending
};
var monitoredSeriesEpisodes = Builder<Episode>.CreateListOfSize(3)
.All() .All()
.With(e => e.Id = 0) .With(e => e.Id = 0)
.With(e => e.SeriesId = series.Id) .With(e => e.SeriesId = _monitoredSeries.Id)
.With(e => e.EpisodeFileId = 0) .With(e => e.EpisodeFileId = 0)
.With(e => e.AirDate = DateTime.Now.AddDays(-5))
.With(e => e.Monitored = true) .With(e => e.Monitored = true)
.TheFirst(1)
.With(e => e.Monitored = false)
.TheLast(1) .TheLast(1)
.With(e => e.SeasonNumber = 0) .With(e => e.SeasonNumber = 0)
.Build(); .Build();
Db.InsertMany(episodes); var unmonitoredSeriesEpisodes = Builder<Episode>.CreateListOfSize(3)
.All()
.With(e => e.Id = 0)
.With(e => e.SeriesId = _unmonitoredSeries.Id)
.With(e => e.EpisodeFileId = 0)
.With(e => e.AirDate = DateTime.Now.AddDays(-5))
.With(e => e.Monitored = true)
.TheFirst(1)
.With(e => e.Monitored = false)
.TheLast(1)
.With(e => e.SeasonNumber = 0)
.Build();
Db.InsertMany(monitoredSeriesEpisodes);
Db.InsertMany(unmonitoredSeriesEpisodes);
} }
[Test] [Test]
public void should_get_episodes() public void should_get_monitored_episodes()
{ {
var episodes = var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false);
Subject.EpisodesWithoutFiles(new PagingSpec<Episode>
{
Page = 1,
PageSize = 10,
SortKey = "AirDate",
SortDirection = SortDirection.Ascending
}, false);
episodes.Records.Should().HaveCount(1); episodes.Records.Should().HaveCount(1);
} }
@ -52,15 +83,33 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
[Ignore("Specials not implemented")] [Ignore("Specials not implemented")]
public void should_get_episode_including_specials() public void should_get_episode_including_specials()
{ {
var episodes = var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, true);
Subject.EpisodesWithoutFiles(new PagingSpec<Episode>
{
Page = 1,
PageSize = 10,
SortKey = "AirDate",
SortDirection = SortDirection.Ascending
}, true);
episodes.Records.Should().HaveCount(2); episodes.Records.Should().HaveCount(2);
} }
[Test]
public void should_not_include_unmonitored_episodes()
{
var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false);
episodes.Records.Should().NotContain(e => e.Monitored == false);
}
[Test]
public void should_not_contain_unmonitored_series()
{
var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false);
episodes.Records.Should().NotContain(e => e.SeriesId == _unmonitoredSeries.Id);
}
[Test]
public void should_have_count_of_one()
{
var episodes = Subject.EpisodesWithoutFiles(_pagingSpec, false);
episodes.TotalRecords.Should().Be(1);
}
} }
} }

@ -82,20 +82,8 @@ namespace NzbDrone.Core.Tv
startingSeasonNumber = 0; startingSeasonNumber = 0;
} }
var pagingQuery = Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id) pagingSpec.Records = GetEpisodesWithoutFilesQuery(pagingSpec, currentTime, startingSeasonNumber).ToList();
.Where(e => e.EpisodeFileId == 0) pagingSpec.TotalRecords = GetEpisodesWithoutFilesQuery(pagingSpec, currentTime, startingSeasonNumber).GetRowCount();
.AndWhere(e => e.SeasonNumber >= startingSeasonNumber)
.AndWhere(e => e.AirDate <= currentTime)
.AndWhere(e => e.Monitored)
.AndWhere(e => e.Series.Monitored)
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
pagingSpec.Records = pagingQuery.ToList();
//TODO: Use the same query for count and records
pagingSpec.TotalRecords = Query.Count(e => e.EpisodeFileId == 0 && e.SeasonNumber >= startingSeasonNumber && e.AirDate <= currentTime);
return pagingSpec; return pagingSpec;
} }
@ -151,5 +139,18 @@ namespace NzbDrone.Core.Tv
{ {
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId); SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
} }
private SortBuilder<Episode> GetEpisodesWithoutFilesQuery(PagingSpec<Episode> pagingSpec, DateTime currentTime, int startingSeasonNumber)
{
return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id)
.Where(e => e.EpisodeFileId == 0)
.AndWhere(e => e.SeasonNumber >= startingSeasonNumber)
.AndWhere(e => e.AirDate <= currentTime)
.AndWhere(e => e.Monitored)
.AndWhere(e => e.Series.Monitored)
.OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection())
.Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize);
}
} }
} }

Loading…
Cancel
Save