From f4dd6adc6a0af50c982fc74b168f3875dc2ed154 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 7 May 2013 00:22:19 -0700 Subject: [PATCH] Added some tests for PagingSpecExtensions Allow specials in missing Dropped ListSortDirection --- Marr.Data/QGen/SortBuilder.cs | 28 ++++++++----- Marr.Data/QGen/SortColumn.cs | 29 +------------ NzbDrone.Api/History/HistoryModule.cs | 10 ++--- NzbDrone.Api/Missing/MissingModule.cs | 10 ++--- .../OrderByClauseFixture.cs | 25 ----------- .../PagingOffsetFixture.cs | 31 ++++++++++++++ .../ToSortDirectionFixture.cs | 41 +++++++++++++++++++ NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 3 +- .../EpisodesWithoutFilesFixture.cs | 4 +- NzbDrone.Core/Datastore/PagingSpec.cs | 11 +++-- .../Datastore/PagingSpecExtensions.cs | 7 ++++ NzbDrone.Core/Tv/EpisodeRepository.cs | 24 +++++------ 12 files changed, 131 insertions(+), 92 deletions(-) delete mode 100644 NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/OrderByClauseFixture.cs create mode 100644 NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/PagingOffsetFixture.cs create mode 100644 NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/ToSortDirectionFixture.cs diff --git a/Marr.Data/QGen/SortBuilder.cs b/Marr.Data/QGen/SortBuilder.cs index a0bfb2492..d16267cf7 100644 --- a/Marr.Data/QGen/SortBuilder.cs +++ b/Marr.Data/QGen/SortBuilder.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Linq.Expressions; @@ -78,13 +77,13 @@ namespace Marr.Data.QGen internal SortBuilder Order(Type declaringType, string propertyName) { - _sortExpressions.Add(new SortColumn(declaringType, propertyName, ListSortDirection.Ascending)); + _sortExpressions.Add(new SortColumn(declaringType, propertyName, SortDirection.Asc)); return this; } internal SortBuilder OrderByDescending(Type declaringType, string propertyName) { - _sortExpressions.Add(new SortColumn(declaringType, propertyName, ListSortDirection.Descending)); + _sortExpressions.Add(new SortColumn(declaringType, propertyName, SortDirection.Desc)); return this; } @@ -104,11 +103,11 @@ namespace Marr.Data.QGen public virtual SortBuilder OrderBy(Expression> sortExpression) { - _sortExpressions.Add(new SortColumn(sortExpression, ListSortDirection.Ascending)); + _sortExpressions.Add(new SortColumn(sortExpression, SortDirection.Asc)); return this; } - public virtual SortBuilder OrderBy(Expression> sortExpression, ListSortDirection sortDirection) + public virtual SortBuilder OrderBy(Expression> sortExpression, SortDirection sortDirection) { _sortExpressions.Add(new SortColumn(sortExpression, sortDirection)); return this; @@ -116,17 +115,17 @@ namespace Marr.Data.QGen public virtual SortBuilder OrderByDescending(Expression> sortExpression) { - _sortExpressions.Add(new SortColumn(sortExpression, ListSortDirection.Descending)); + _sortExpressions.Add(new SortColumn(sortExpression, SortDirection.Desc)); return this; } public virtual SortBuilder ThenBy(Expression> sortExpression) { - _sortExpressions.Add(new SortColumn(sortExpression, ListSortDirection.Ascending)); + _sortExpressions.Add(new SortColumn(sortExpression, SortDirection.Asc)); return this; } - public virtual SortBuilder ThenBy(Expression> sortExpression, ListSortDirection sortDirection) + public virtual SortBuilder ThenBy(Expression> sortExpression, SortDirection sortDirection) { _sortExpressions.Add(new SortColumn(sortExpression, sortDirection)); return this; @@ -134,7 +133,7 @@ namespace Marr.Data.QGen public virtual SortBuilder ThenByDescending(Expression> sortExpression) { - _sortExpressions.Add(new SortColumn(sortExpression, ListSortDirection.Descending)); + _sortExpressions.Add(new SortColumn(sortExpression, SortDirection.Desc)); return this; } @@ -171,6 +170,15 @@ namespace Marr.Data.QGen #endregion + #region - Count - + + public virtual int Count() + { + return _baseBuilder.GetRowCount(); + } + + #endregion + #region - ToList / ToString / BuildQuery - public virtual List ToList() @@ -211,7 +219,7 @@ namespace Marr.Data.QGen string columnName = DataHelper.GetColumnName(sort.DeclaringType, sort.PropertyName, useAltName); sb.Append(_dialect.CreateToken(string.Format("{0}.{1}", table.Alias, columnName))); - if (sort.Direction == ListSortDirection.Descending) + if (sort.Direction == SortDirection.Desc) sb.Append(" DESC"); } diff --git a/Marr.Data/QGen/SortColumn.cs b/Marr.Data/QGen/SortColumn.cs index bbd9cbd6e..1a38c3c82 100644 --- a/Marr.Data/QGen/SortColumn.cs +++ b/Marr.Data/QGen/SortColumn.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; using System.Linq.Expressions; @@ -10,39 +9,22 @@ namespace Marr.Data.QGen { public class SortColumn { - [Obsolete("Use ListSortDirection instead")] public SortColumn(Expression> sortExpression, SortDirection direction) { MemberExpression me = GetMemberExpression(sortExpression.Body); DeclaringType = me.Expression.Type; PropertyName = me.Member.Name; - Direction = GetSortDirection(direction); + Direction = direction; } - [Obsolete("Use ListSortDirection instead")] public SortColumn(Type declaringType, string propertyName, SortDirection direction) { DeclaringType = declaringType; PropertyName = propertyName; - Direction = GetSortDirection(direction); - } - - public SortColumn(Expression> sortExpression, ListSortDirection direction) - { - MemberExpression me = GetMemberExpression(sortExpression.Body); - DeclaringType = me.Expression.Type; - PropertyName = me.Member.Name; Direction = direction; } - public SortColumn(Type declaringType, string propertyName, ListSortDirection direction) - { - DeclaringType = declaringType; - PropertyName = propertyName; - Direction = direction; - } - - public ListSortDirection Direction { get; private set; } + public SortDirection Direction { get; private set; } public Type DeclaringType { get; private set; } public string PropertyName { get; private set; } @@ -58,13 +40,6 @@ namespace Marr.Data.QGen return me; } - - private ListSortDirection GetSortDirection(SortDirection direction) - { - if (direction == SortDirection.Desc) return ListSortDirection.Descending; - - return ListSortDirection.Ascending; - } } public enum SortDirection diff --git a/NzbDrone.Api/History/HistoryModule.cs b/NzbDrone.Api/History/HistoryModule.cs index dd572cc39..1800f6b3a 100644 --- a/NzbDrone.Api/History/HistoryModule.cs +++ b/NzbDrone.Api/History/HistoryModule.cs @@ -36,15 +36,13 @@ namespace NzbDrone.Api.History Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page); if (page == 0) page = 1; - var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey) - .Equals("SeriesTitle", StringComparison.InvariantCultureIgnoreCase) - ? "SeriesTitle" - : "AirDate"; + var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey); + if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate"; var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir) .Equals("Asc", StringComparison.InvariantCultureIgnoreCase) - ? ListSortDirection.Ascending - : ListSortDirection.Descending; + ? SortDirection.Ascending + : SortDirection.Descending; var pagingSpec = new PagingSpec { diff --git a/NzbDrone.Api/Missing/MissingModule.cs b/NzbDrone.Api/Missing/MissingModule.cs index 5c751f733..a316601a8 100644 --- a/NzbDrone.Api/Missing/MissingModule.cs +++ b/NzbDrone.Api/Missing/MissingModule.cs @@ -35,15 +35,13 @@ namespace NzbDrone.Api.Missing Int32.TryParse(PrimitiveExtensions.ToNullSafeString(Request.Query.Page), out page); if (page == 0) page = 1; - var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey) - .Equals("Series.Title", StringComparison.InvariantCultureIgnoreCase) - ? "Series.Title" - : "AirDate"; + var sortKey = PrimitiveExtensions.ToNullSafeString(Request.Query.SortKey); + if (String.IsNullOrEmpty(sortKey)) sortKey = "AirDate"; var sortDirection = PrimitiveExtensions.ToNullSafeString(Request.Query.SortDir) .Equals("Asc", StringComparison.InvariantCultureIgnoreCase) - ? ListSortDirection.Ascending - : ListSortDirection.Descending; + ? SortDirection.Ascending + : SortDirection.Descending; var pagingSpec = new PagingSpec { diff --git a/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/OrderByClauseFixture.cs b/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/OrderByClauseFixture.cs deleted file mode 100644 index d8d42c127..000000000 --- a/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/OrderByClauseFixture.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.ComponentModel; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Core.Datastore; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.Test.Datastore.PagingSpecExtenstionsTests -{ - public class OrderByClauseFixture - { - [Test] - public void Test() - { - var pagingSpec = new PagingSpec - { - Page = 1, - PageSize = 10, - SortDirection = ListSortDirection.Ascending, - SortKey = "AirDate" - }; - - pagingSpec.OrderByClause().Should().NotBeNullOrEmpty(); - } - } -} diff --git a/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/PagingOffsetFixture.cs b/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/PagingOffsetFixture.cs new file mode 100644 index 000000000..27982c244 --- /dev/null +++ b/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/PagingOffsetFixture.cs @@ -0,0 +1,31 @@ +using System; +using System.ComponentModel; +using System.Linq.Expressions; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.Datastore.PagingSpecExtenstionsTests +{ + public class PagingOffsetFixture + { + [TestCase(1, 10, 0)] + [TestCase(2, 10, 10)] + [TestCase(3, 20, 40)] + [TestCase(1, 100, 0)] + public void should_calcuate_expected_offset(int page, int pageSize, int expected) + { + var pagingSpec = new PagingSpec + { + Page = page, + PageSize = pageSize, + SortDirection = SortDirection.Ascending, + SortKey = "AirDate" + }; + + pagingSpec.PagingOffset().Should().Be(expected); + } + + } +} diff --git a/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/ToSortDirectionFixture.cs b/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/ToSortDirectionFixture.cs new file mode 100644 index 000000000..9485fb93c --- /dev/null +++ b/NzbDrone.Core.Test/Datastore/PagingSpecExtenstionsTests/ToSortDirectionFixture.cs @@ -0,0 +1,41 @@ +using System; +using System.ComponentModel; +using System.Linq.Expressions; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore; +using NzbDrone.Core.Tv; + +namespace NzbDrone.Core.Test.Datastore.PagingSpecExtenstionsTests +{ + public class ToSortDirectionFixture + { + [Test] + public void should_convert_ascending_to_asc() + { + var pagingSpec = new PagingSpec + { + Page = 1, + PageSize = 10, + SortDirection = SortDirection.Ascending, + SortKey = "AirDate" + }; + + pagingSpec.ToSortDirection().Should().Be(Marr.Data.QGen.SortDirection.Asc); + } + + [Test] + public void should_convert_descending_to_desc() + { + var pagingSpec = new PagingSpec + { + Page = 1, + PageSize = 10, + SortDirection = SortDirection.Descending, + SortKey = "AirDate" + }; + + pagingSpec.ToSortDirection().Should().Be(Marr.Data.QGen.SortDirection.Desc); + } + } +} diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 978d5950b..c6d206185 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -120,7 +120,8 @@ - + + diff --git a/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs b/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs index 405c51f27..082b9c4ff 100644 --- a/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs +++ b/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/EpisodesWithoutFilesFixture.cs @@ -46,7 +46,7 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests Page = 1, PageSize = 10, SortKey = "AirDate", - SortDirection = ListSortDirection.Ascending + SortDirection = SortDirection.Ascending }, false); episodes.Records.Should().HaveCount(1); } @@ -61,7 +61,7 @@ namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests Page = 1, PageSize = 10, SortKey = "AirDate", - SortDirection = ListSortDirection.Ascending + SortDirection = SortDirection.Ascending }, true); episodes.Records.Should().HaveCount(2); } diff --git a/NzbDrone.Core/Datastore/PagingSpec.cs b/NzbDrone.Core/Datastore/PagingSpec.cs index 08c498e4d..b763c02e2 100644 --- a/NzbDrone.Core/Datastore/PagingSpec.cs +++ b/NzbDrone.Core/Datastore/PagingSpec.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Text; @@ -12,7 +11,13 @@ namespace NzbDrone.Core.Datastore public int PageSize { get; set; } public int TotalRecords { get; set; } public string SortKey { get; set; } - public ListSortDirection SortDirection { get; set; } - public List Records { get; set; } + public SortDirection SortDirection { get; set; } + public List Records { get; set; } + } + + public enum SortDirection + { + Ascending, + Descending } } diff --git a/NzbDrone.Core/Datastore/PagingSpecExtensions.cs b/NzbDrone.Core/Datastore/PagingSpecExtensions.cs index ae8a7038e..ed7ba739f 100644 --- a/NzbDrone.Core/Datastore/PagingSpecExtensions.cs +++ b/NzbDrone.Core/Datastore/PagingSpecExtensions.cs @@ -20,6 +20,13 @@ namespace NzbDrone.Core.Datastore return (pagingSpec.Page - 1)*pagingSpec.PageSize; } + public static Marr.Data.QGen.SortDirection ToSortDirection(this PagingSpec pagingSpec) + { + if (pagingSpec.SortDirection == SortDirection.Descending) return Marr.Data.QGen.SortDirection.Desc; + + return Marr.Data.QGen.SortDirection.Asc; + } + private static Expression> CreateExpression(string propertyName) { Type type = typeof(TModel); diff --git a/NzbDrone.Core/Tv/EpisodeRepository.cs b/NzbDrone.Core/Tv/EpisodeRepository.cs index 18f721b01..13b639ffa 100644 --- a/NzbDrone.Core/Tv/EpisodeRepository.cs +++ b/NzbDrone.Core/Tv/EpisodeRepository.cs @@ -67,25 +67,25 @@ namespace NzbDrone.Core.Tv public PagingSpec EpisodesWithoutFiles(PagingSpec pagingSpec, bool includeSpecials) { + var currentTime = DateTime.UtcNow; + var startingSeasonNumber = 1; + if (includeSpecials) { - throw new NotImplementedException("Including specials is not available"); + startingSeasonNumber = 0; } - //This causes an issue if done within the LINQ Query - var currentTime = DateTime.UtcNow; - pagingSpec.Records = Query.Join(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id) - .Where(e => e.EpisodeFileId == 0) - .AndWhere(e => e.SeasonNumber > 0) - .AndWhere(e => e.AirDate <= currentTime) - .OrderBy(pagingSpec.OrderByClause(), pagingSpec.SortDirection) - .Skip(pagingSpec.PagingOffset()) - .Take(pagingSpec.PageSize) - .ToList(); + .Where(e => e.EpisodeFileId == 0) + .AndWhere(e => e.SeasonNumber >= startingSeasonNumber) + .AndWhere(e => e.AirDate <= currentTime) + .OrderBy(pagingSpec.OrderByClause(), pagingSpec.ToSortDirection()) + .Skip(pagingSpec.PagingOffset()) + .Take(pagingSpec.PageSize) + .ToList(); //TODO: Use the same query for count and records - pagingSpec.TotalRecords = Query.Where(e => e.EpisodeFileId == 0 && e.SeasonNumber > 0 && e.AirDate <= currentTime).GetRowCount(); + pagingSpec.TotalRecords = Query.Where(e => e.EpisodeFileId == 0 && e.SeasonNumber >= startingSeasonNumber && e.AirDate <= currentTime).Count(); return pagingSpec; }