2019-11-17 00:15:07 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Sieve.Exceptions;
|
2018-02-10 06:37:04 +01:00
|
|
|
using Sieve.Models;
|
|
|
|
using SieveUnitTests.Entities;
|
|
|
|
using SieveUnitTests.Services;
|
2021-05-13 14:04:18 +02:00
|
|
|
using Xunit;
|
2018-02-10 06:37:04 +01:00
|
|
|
|
|
|
|
namespace SieveUnitTests
|
|
|
|
{
|
|
|
|
public class Mapper
|
|
|
|
{
|
2018-05-15 01:34:37 +02:00
|
|
|
private readonly ApplicationSieveProcessor _processor;
|
|
|
|
private readonly IQueryable<Post> _posts;
|
2018-02-10 06:37:04 +01:00
|
|
|
|
|
|
|
public Mapper()
|
|
|
|
{
|
|
|
|
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
|
|
|
|
new SieveCustomSortMethods(),
|
|
|
|
new SieveCustomFilterMethods());
|
|
|
|
|
|
|
|
_posts = new List<Post>
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
new Post
|
|
|
|
{
|
2018-02-10 06:37:04 +01:00
|
|
|
Id = 1,
|
|
|
|
ThisHasNoAttributeButIsAccessible = "A",
|
|
|
|
ThisHasNoAttribute = "A",
|
|
|
|
OnlySortableViaFluentApi = 100
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
new Post
|
|
|
|
{
|
2018-02-10 06:37:04 +01:00
|
|
|
Id = 2,
|
|
|
|
ThisHasNoAttributeButIsAccessible = "B",
|
|
|
|
ThisHasNoAttribute = "B",
|
|
|
|
OnlySortableViaFluentApi = 50
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
new Post
|
|
|
|
{
|
2018-02-10 06:37:04 +01:00
|
|
|
Id = 3,
|
|
|
|
ThisHasNoAttributeButIsAccessible = "C",
|
|
|
|
ThisHasNoAttribute = "C",
|
|
|
|
OnlySortableViaFluentApi = 0
|
|
|
|
},
|
|
|
|
}.AsQueryable();
|
|
|
|
}
|
2018-05-15 01:34:37 +02:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
[Fact]
|
2018-02-10 06:37:04 +01:00
|
|
|
public void MapperWorks()
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
var model = new SieveModel
|
2018-02-10 06:37:04 +01:00
|
|
|
{
|
|
|
|
Filters = "shortname@=A",
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 06:37:04 +01:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
Assert.Equal("A", result.First().ThisHasNoAttributeButIsAccessible);
|
2018-02-10 06:37:04 +01:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
Assert.True(result.Count() == 1);
|
2018-02-10 06:37:04 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
[Fact]
|
2018-02-10 06:37:04 +01:00
|
|
|
public void MapperSortOnlyWorks()
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
var model = new SieveModel
|
2018-02-10 06:37:04 +01:00
|
|
|
{
|
|
|
|
Filters = "OnlySortableViaFluentApi@=50",
|
|
|
|
Sorts = "OnlySortableViaFluentApi"
|
|
|
|
};
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
var result = _processor.Apply(model, _posts, applyFiltering: false, applyPagination: false);
|
2018-02-13 23:43:33 +01:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
Assert.Throws<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
|
2018-02-10 06:37:04 +01:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
Assert.Equal(3, result.First().Id);
|
2018-02-10 06:37:04 +01:00
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
Assert.True(result.Count() == 3);
|
2018-02-10 06:37:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|