Added some tests for PagingSpecExtensions

Allow specials in missing
Dropped ListSortDirection
pull/3113/head
Mark McDowall 12 years ago
parent d37c8c26c2
commit f4dd6adc6a

@ -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<T> Order(Type declaringType, string propertyName)
{
_sortExpressions.Add(new SortColumn<T>(declaringType, propertyName, ListSortDirection.Ascending));
_sortExpressions.Add(new SortColumn<T>(declaringType, propertyName, SortDirection.Asc));
return this;
}
internal SortBuilder<T> OrderByDescending(Type declaringType, string propertyName)
{
_sortExpressions.Add(new SortColumn<T>(declaringType, propertyName, ListSortDirection.Descending));
_sortExpressions.Add(new SortColumn<T>(declaringType, propertyName, SortDirection.Desc));
return this;
}
@ -104,11 +103,11 @@ namespace Marr.Data.QGen
public virtual SortBuilder<T> OrderBy(Expression<Func<T, object>> sortExpression)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, ListSortDirection.Ascending));
_sortExpressions.Add(new SortColumn<T>(sortExpression, SortDirection.Asc));
return this;
}
public virtual SortBuilder<T> OrderBy(Expression<Func<T, object>> sortExpression, ListSortDirection sortDirection)
public virtual SortBuilder<T> OrderBy(Expression<Func<T, object>> sortExpression, SortDirection sortDirection)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, sortDirection));
return this;
@ -116,17 +115,17 @@ namespace Marr.Data.QGen
public virtual SortBuilder<T> OrderByDescending(Expression<Func<T, object>> sortExpression)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, ListSortDirection.Descending));
_sortExpressions.Add(new SortColumn<T>(sortExpression, SortDirection.Desc));
return this;
}
public virtual SortBuilder<T> ThenBy(Expression<Func<T, object>> sortExpression)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, ListSortDirection.Ascending));
_sortExpressions.Add(new SortColumn<T>(sortExpression, SortDirection.Asc));
return this;
}
public virtual SortBuilder<T> ThenBy(Expression<Func<T, object>> sortExpression, ListSortDirection sortDirection)
public virtual SortBuilder<T> ThenBy(Expression<Func<T, object>> sortExpression, SortDirection sortDirection)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, sortDirection));
return this;
@ -134,7 +133,7 @@ namespace Marr.Data.QGen
public virtual SortBuilder<T> ThenByDescending(Expression<Func<T, object>> sortExpression)
{
_sortExpressions.Add(new SortColumn<T>(sortExpression, ListSortDirection.Descending));
_sortExpressions.Add(new SortColumn<T>(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<T> 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");
}

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
@ -10,24 +9,7 @@ namespace Marr.Data.QGen
{
public class SortColumn<T>
{
[Obsolete("Use ListSortDirection instead")]
public SortColumn(Expression<Func<T, object>> sortExpression, SortDirection direction)
{
MemberExpression me = GetMemberExpression(sortExpression.Body);
DeclaringType = me.Expression.Type;
PropertyName = me.Member.Name;
Direction = GetSortDirection(direction);
}
[Obsolete("Use ListSortDirection instead")]
public SortColumn(Type declaringType, string propertyName, SortDirection direction)
{
DeclaringType = declaringType;
PropertyName = propertyName;
Direction = GetSortDirection(direction);
}
public SortColumn(Expression<Func<T, object>> sortExpression, ListSortDirection direction)
{
MemberExpression me = GetMemberExpression(sortExpression.Body);
DeclaringType = me.Expression.Type;
@ -35,14 +17,14 @@ namespace Marr.Data.QGen
Direction = direction;
}
public SortColumn(Type declaringType, string propertyName, ListSortDirection direction)
public SortColumn(Type declaringType, string propertyName, SortDirection 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

@ -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<Episode>
{

@ -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<Episode>
{

@ -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<Episode>
{
Page = 1,
PageSize = 10,
SortDirection = ListSortDirection.Ascending,
SortKey = "AirDate"
};
pagingSpec.OrderByClause().Should().NotBeNullOrEmpty();
}
}
}

@ -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<Episode>
{
Page = page,
PageSize = pageSize,
SortDirection = SortDirection.Ascending,
SortKey = "AirDate"
};
pagingSpec.PagingOffset().Should().Be(expected);
}
}
}

@ -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<Episode>
{
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<Episode>
{
Page = 1,
PageSize = 10,
SortDirection = SortDirection.Descending,
SortKey = "AirDate"
};
pagingSpec.ToSortDirection().Should().Be(Marr.Data.QGen.SortDirection.Desc);
}
}
}

@ -120,7 +120,8 @@
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />
<Compile Include="Datastore\PagingSpecExtenstionsTests\OrderByClauseFixture.cs" />
<Compile Include="Datastore\PagingSpecExtenstionsTests\ToSortDirectionFixture.cs" />
<Compile Include="Datastore\PagingSpecExtenstionsTests\PagingOffsetFixture.cs" />
<Compile Include="Download\DownloadClientTests\BlackholeProviderFixture.cs" />
<Compile Include="Download\DownloadClientTests\NzbgetProviderTests\DownloadNzbFixture.cs" />
<Compile Include="Download\DownloadClientTests\NzbgetProviderTests\QueueFixture.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);
}

@ -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<TModel> Records { get; set; }
public SortDirection SortDirection { get; set; }
public List<TModel> Records { get; set; }
}
public enum SortDirection
{
Ascending,
Descending
}
}

@ -20,6 +20,13 @@ namespace NzbDrone.Core.Datastore
return (pagingSpec.Page - 1)*pagingSpec.PageSize;
}
public static Marr.Data.QGen.SortDirection ToSortDirection<TModel>(this PagingSpec<TModel> pagingSpec)
{
if (pagingSpec.SortDirection == SortDirection.Descending) return Marr.Data.QGen.SortDirection.Desc;
return Marr.Data.QGen.SortDirection.Asc;
}
private static Expression<Func<TModel, object>> CreateExpression<TModel>(string propertyName)
{
Type type = typeof(TModel);

@ -67,25 +67,25 @@ namespace NzbDrone.Core.Tv
public PagingSpec<Episode> EpisodesWithoutFiles(PagingSpec<Episode> 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<Episode, Series>(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;
}

Loading…
Cancel
Save