Allow Filters on different sources to share the same name

Allows Posts and Comments to both use the IsNew filter with their own implementations.
This commit is contained in:
Matt Furden
2018-06-15 01:16:36 -07:00
parent abb7029c70
commit d792813cd5
5 changed files with 225 additions and 12 deletions

View File

@@ -1,9 +1,15 @@
namespace SieveUnitTests.Entities
using System;
using Sieve.Attributes;
namespace SieveUnitTests.Entities
{
public class Comment
{
public int Id { get; set; }
[Sieve(CanFilter = true, CanSort = true)]
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;
public string Text { get; set; }
}
}

View File

@@ -15,6 +15,7 @@ namespace SieveUnitTests
{
private readonly SieveProcessor _processor;
private readonly IQueryable<Post> _posts;
private readonly IQueryable<Comment> _comments;
public General()
{
@@ -52,6 +53,25 @@ namespace SieveUnitTests
CategoryId = 2,
},
}.AsQueryable();
_comments = new List<Comment>
{
new Comment() {
Id = 0,
DateCreated = DateTimeOffset.UtcNow.AddDays(-20),
Text = "This is an old comment."
},
new Comment() {
Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "This is a fairly new comment."
},
new Comment() {
Id = 2,
DateCreated = DateTimeOffset.UtcNow,
Text = "This is a brand new comment."
},
}.AsQueryable();
}
[TestMethod]
@@ -180,6 +200,30 @@ namespace SieveUnitTests
Assert.IsTrue(result.Count() == 1);
}
[TestMethod]
public void CustomFiltersOnDifferentSourcesCanShareName()
{
var postModel = new SieveModel()
{
Filters = "CategoryId==2,Isnew",
};
var postResult = _processor.Apply(postModel, _posts);
Assert.IsTrue(postResult.Any(p => p.Id == 3));
Assert.AreEqual(1, postResult.Count());
var commentModel = new SieveModel()
{
Filters = "Isnew",
};
var commentResult = _processor.Apply(commentModel, _comments);
Assert.IsTrue(commentResult.Any(c => c.Id == 2));
Assert.AreEqual(2, commentResult.Count());
}
[TestMethod]
public void MethodNotFoundExceptionWork()
{

View File

@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using Sieve.Services;
using SieveUnitTests.Entities;
@@ -13,6 +14,13 @@ namespace SieveUnitTests.Services
return result;
}
public IQueryable<Comment> IsNew(IQueryable<Comment> source, string op, string value)
{
var result = source.Where(c => c.DateCreated > DateTimeOffset.UtcNow.AddDays(-2));
return result;
}
public IQueryable<Comment> TestComment(IQueryable<Comment> source, string op, string value)
{
return source;