mirror of
https://github.com/Radarr/Radarr.git
synced 2024-09-11 12:02:35 +02:00
Added some tests for PagingSpecExtensions
Allow specials in missing Dropped ListSortDirection
This commit is contained in:
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 @@ public virtual SortBuilder<T> AndWhere(string whereClause)
|
||||
|
||||
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 @@ public virtual SortBuilder<T> OrderBy(string orderByClause)
|
||||
|
||||
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 @@ public virtual SortBuilder<T> OrderBy(Expression<Func<T, object>> sortExpression
|
||||
|
||||
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 @@ public virtual SortBuilder<T> ThenBy(Expression<Func<T, object>> sortExpression,
|
||||
|
||||
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 @@ public virtual int GetRowCount()
|
||||
|
||||
#endregion
|
||||
|
||||
#region - Count -
|
||||
|
||||
public virtual int Count()
|
||||
{
|
||||
return _baseBuilder.GetRowCount();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region - ToList / ToString / BuildQuery -
|
||||
|
||||
public virtual List<T> ToList()
|
||||
@ -211,7 +219,7 @@ public virtual string BuildQuery(bool useAltName)
|
||||
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,39 +9,22 @@ 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);
|
||||
Direction = 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;
|
||||
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 @@ private MemberExpression GetMemberExpression(Expression exp)
|
||||
|
||||
return me;
|
||||
}
|
||||
|
||||
private ListSortDirection GetSortDirection(SortDirection direction)
|
||||
{
|
||||
if (direction == SortDirection.Desc) return ListSortDirection.Descending;
|
||||
|
||||
return ListSortDirection.Ascending;
|
||||
}
|
||||
}
|
||||
|
||||
public enum SortDirection
|
||||
|
@ -36,15 +36,13 @@ private Response GetHistory()
|
||||
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 @@ private Response GetMissingEpisodes()
|
||||
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 @@ public void should_get_episodes()
|
||||
Page = 1,
|
||||
PageSize = 10,
|
||||
SortKey = "AirDate",
|
||||
SortDirection = ListSortDirection.Ascending
|
||||
SortDirection = SortDirection.Ascending
|
||||
}, false);
|
||||
episodes.Records.Should().HaveCount(1);
|
||||
}
|
||||
@ -61,7 +61,7 @@ public void should_get_episode_including_specials()
|
||||
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 @@ public class PagingSpec<TModel>
|
||||
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 @@ public static int PagingOffset<TModel>(this PagingSpec<TModel> pagingSpec)
|
||||
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 @@ public List<Episode> GetEpisodeByFileId(int fileId)
|
||||
|
||||
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…
Reference in New Issue
Block a user