SievePropertyMapper for #4

This commit is contained in:
Biarity
2018-02-10 15:37:04 +10:00
parent b52362e2bc
commit 0bd38b8348
10 changed files with 330 additions and 10 deletions

View File

@@ -21,5 +21,11 @@ namespace SieveUnitTests.Entities
[Sieve(CanFilter = true, CanSort = true)]
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;
public string ThisHasNoAttribute { get; set; }
public string ThisHasNoAttributeButIsAccessible { get; set; }
public int OnlySortableViaFluentApi { get; set; }
}
}

84
SieveUnitTests/Mapper.cs Normal file
View File

@@ -0,0 +1,84 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Entities;
using SieveUnitTests.Services;
using System;
using System.Linq;
using System.Collections.Generic;
namespace SieveUnitTests
{
[TestClass]
public class Mapper
{
private ApplicationSieveProcessor _processor;
private IQueryable<Post> _posts;
public Mapper()
{
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_posts = new List<Post>
{
new Post() {
Id = 1,
ThisHasNoAttributeButIsAccessible = "A",
ThisHasNoAttribute = "A",
OnlySortableViaFluentApi = 100
},
new Post() {
Id = 2,
ThisHasNoAttributeButIsAccessible = "B",
ThisHasNoAttribute = "B",
OnlySortableViaFluentApi = 50
},
new Post() {
Id = 3,
ThisHasNoAttributeButIsAccessible = "C",
ThisHasNoAttribute = "C",
OnlySortableViaFluentApi = 0
},
}.AsQueryable();
}
[TestMethod]
public void MapperWorks()
{
var model = new SieveModel()
{
Filters = "shortname@=A",
};
var result = _processor.ApplyAll(model, _posts);
Assert.AreEqual(result.First().ThisHasNoAttributeButIsAccessible, "A");
Assert.IsTrue(result.Count() == 1);
}
[TestMethod]
public void MapperSortOnlyWorks()
{
var model = new SieveModel()
{
Filters = "OnlySortableViaFluentApi@=50",
Sorts = "OnlySortableViaFluentApi"
};
var result = _processor.ApplyAll(model, _posts);
Assert.AreEqual(result.First().Id, 3);
Assert.IsTrue(result.Count() == 3);
}
}
}
//
//Sorts = "LikeCount",
//Page = 1,
//PageSize = 10
//

View File

@@ -0,0 +1,31 @@
using Microsoft.Extensions.Options;
using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace SieveUnitTests.Services
{
public class ApplicationSieveProcessor : SieveProcessor
{
public ApplicationSieveProcessor(IOptions<SieveOptions> options, ISieveCustomSortMethods customSortMethods, ISieveCustomFilterMethods customFilterMethods) : base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.ThisHasNoAttributeButIsAccessible)
.CanSort()
.CanFilter()
.HasName("shortname");
mapper.Property<Post>(p => p.OnlySortableViaFluentApi)
.CanSort();
return mapper;
}
}
}