2018-05-28 03:32:21 +02:00
|
|
|
|
using System;
|
2018-05-15 01:34:37 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Sieve.Exceptions;
|
2018-02-10 01:26:32 +01:00
|
|
|
|
using Sieve.Models;
|
|
|
|
|
using Sieve.Services;
|
|
|
|
|
using SieveUnitTests.Entities;
|
|
|
|
|
using SieveUnitTests.Services;
|
2021-05-13 14:04:18 +02:00
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.Abstractions;
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
|
|
|
|
namespace SieveUnitTests
|
|
|
|
|
{
|
|
|
|
|
public class General
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
private readonly ITestOutputHelper _testOutputHelper;
|
2018-05-15 01:34:37 +02:00
|
|
|
|
private readonly SieveProcessor _processor;
|
2021-08-29 16:30:19 +02:00
|
|
|
|
private readonly SieveProcessor _nullableProcessor;
|
2018-05-15 01:34:37 +02:00
|
|
|
|
private readonly IQueryable<Post> _posts;
|
2018-06-15 10:16:36 +02:00
|
|
|
|
private readonly IQueryable<Comment> _comments;
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
public General(ITestOutputHelper testOutputHelper)
|
2018-02-10 01:26:32 +01:00
|
|
|
|
{
|
2021-08-29 16:30:19 +02:00
|
|
|
|
var nullableAccessor = new SieveOptionsAccessor();
|
|
|
|
|
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
_testOutputHelper = testOutputHelper;
|
2019-01-18 11:45:38 +01:00
|
|
|
|
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
|
2018-02-10 01:26:32 +01:00
|
|
|
|
new SieveCustomSortMethods(),
|
|
|
|
|
new SieveCustomFilterMethods());
|
|
|
|
|
|
2021-08-29 16:30:19 +02:00
|
|
|
|
_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
|
|
|
|
|
new SieveCustomSortMethods(),
|
|
|
|
|
new SieveCustomFilterMethods());
|
|
|
|
|
|
2018-02-10 01:26:32 +01:00
|
|
|
|
_posts = new List<Post>
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2018-02-10 01:26:32 +01:00
|
|
|
|
Id = 0,
|
|
|
|
|
Title = "A",
|
2018-04-08 05:46:16 +02:00
|
|
|
|
LikeCount = 100,
|
2018-05-28 03:32:21 +02:00
|
|
|
|
IsDraft = true,
|
|
|
|
|
CategoryId = null,
|
2019-03-17 23:23:37 +01:00
|
|
|
|
TopComment = new Comment { Id = 0, Text = "A1" },
|
|
|
|
|
FeaturedComment = new Comment { Id = 4, Text = "A2" }
|
2018-02-10 01:26:32 +01:00
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2018-02-10 01:26:32 +01:00
|
|
|
|
Id = 1,
|
|
|
|
|
Title = "B",
|
2018-04-08 05:46:16 +02:00
|
|
|
|
LikeCount = 50,
|
2018-05-28 03:32:21 +02:00
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = 1,
|
2019-03-17 23:23:37 +01:00
|
|
|
|
TopComment = new Comment { Id = 3, Text = "B1" },
|
|
|
|
|
FeaturedComment = new Comment { Id = 5, Text = "B2" }
|
2018-02-10 01:26:32 +01:00
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2018-02-10 01:26:32 +01:00
|
|
|
|
Id = 2,
|
|
|
|
|
Title = "C",
|
2018-05-28 03:32:21 +02:00
|
|
|
|
LikeCount = 0,
|
|
|
|
|
CategoryId = 1,
|
2019-03-17 23:23:37 +01:00
|
|
|
|
TopComment = new Comment { Id = 2, Text = "C1" },
|
|
|
|
|
FeaturedComment = new Comment { Id = 6, Text = "C2" }
|
2018-02-10 01:26:32 +01:00
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2018-04-20 10:46:04 +02:00
|
|
|
|
Id = 3,
|
2018-05-15 01:34:37 +02:00
|
|
|
|
Title = "D",
|
2018-04-20 10:46:04 +02:00
|
|
|
|
LikeCount = 3,
|
2018-05-28 03:32:21 +02:00
|
|
|
|
IsDraft = true,
|
|
|
|
|
CategoryId = 2,
|
2019-03-17 23:23:37 +01:00
|
|
|
|
TopComment = new Comment { Id = 1, Text = "D1" },
|
|
|
|
|
FeaturedComment = new Comment { Id = 7, Text = "D2" }
|
2018-04-20 10:46:04 +02:00
|
|
|
|
},
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}.AsQueryable();
|
2018-06-15 10:16:36 +02:00
|
|
|
|
|
|
|
|
|
_comments = new List<Comment>
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Comment
|
|
|
|
|
{
|
2018-06-15 10:16:36 +02:00
|
|
|
|
Id = 0,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow.AddDays(-20),
|
2019-03-17 23:23:37 +01:00
|
|
|
|
Text = "This is an old comment."
|
2018-06-15 10:16:36 +02:00
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Comment
|
|
|
|
|
{
|
2018-06-15 10:16:36 +02:00
|
|
|
|
Id = 1,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
|
2019-03-17 23:23:37 +01:00
|
|
|
|
Text = "This is a fairly new comment."
|
2018-06-15 10:16:36 +02:00
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Comment
|
|
|
|
|
{
|
2018-06-15 10:16:36 +02:00
|
|
|
|
Id = 2,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow,
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Text = "This is a brand new comment. (Text in braces, comma separated)"
|
2018-06-15 10:16:36 +02:00
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-10 01:26:32 +01:00
|
|
|
|
public void ContainsCanBeCaseInsensitive()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-10 01:26:32 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title@=*a"
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(0, result.First().Id);
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-11-05 15:10:44 +01:00
|
|
|
|
public void NotEqualsCanBeCaseInsensitive()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-11-05 15:10:44 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title!=*a"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(1, result.First().Id);
|
|
|
|
|
Assert.True(result.Count() == 3);
|
2019-11-05 15:10:44 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-10 01:26:32 +01:00
|
|
|
|
public void ContainsIsCaseSensitive()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-10 01:26:32 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title@=a",
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(!result.Any());
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-01-08 17:20:49 +01:00
|
|
|
|
public void NotContainsWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-01-08 17:20:49 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title!@=D",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Count() == 3);
|
2019-01-08 17:20:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-04-08 05:46:16 +02:00
|
|
|
|
public void CanFilterBools()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-04-08 05:46:16 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "IsDraft==false"
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-05-15 01:34:37 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Count() == 2);
|
2018-04-08 05:46:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-04-08 05:46:16 +02:00
|
|
|
|
public void CanSortBools()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-04-08 05:46:16 +02:00
|
|
|
|
{
|
|
|
|
|
Sorts = "-IsDraft"
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-04-08 05:46:16 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(0, result.First().Id);
|
2018-04-08 05:46:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-05-28 03:32:21 +02:00
|
|
|
|
public void CanFilterNullableInts()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-05-28 03:32:21 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "CategoryId==1"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-08-29 16:30:19 +02:00
|
|
|
|
var nullableResult = _nullableProcessor.Apply(model, _posts);
|
2018-05-28 03:32:21 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Count() == 2);
|
2021-08-29 16:30:19 +02:00
|
|
|
|
Assert.True(nullableResult.Count() == 2);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
}
|
2021-08-29 16:30:19 +02:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void CanFilterNullableIntsWithNotEqual()
|
|
|
|
|
{
|
|
|
|
|
var model = new SieveModel()
|
|
|
|
|
{
|
|
|
|
|
Filters = "CategoryId!=1"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
var nullableResult = _nullableProcessor.Apply(model, _posts);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.Count() == 1);
|
|
|
|
|
Assert.True(nullableResult.Count() == 2);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData(@"Text@=*\,")]
|
|
|
|
|
[InlineData(@"Text@=*\, ")]
|
|
|
|
|
[InlineData(@"Text@=*braces\,")]
|
|
|
|
|
[InlineData(@"Text@=*braces\, comma")]
|
|
|
|
|
public void CanFilterWithEscapedComma(string filter)
|
|
|
|
|
{
|
|
|
|
|
var model = new SieveModel
|
|
|
|
|
{
|
|
|
|
|
Filters = filter
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _comments);
|
|
|
|
|
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-05-28 03:32:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-10 01:26:32 +01:00
|
|
|
|
public void EqualsDoesntFailWithNonStringTypes()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-10 01:26:32 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "LikeCount==50",
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
|
|
|
|
|
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
|
|
|
|
|
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(1, result.First().Id);
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}
|
2018-02-11 04:35:33 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-11 04:35:33 +01:00
|
|
|
|
public void CustomFiltersWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-11 04:35:33 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Isnew",
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-02-11 04:35:33 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.False(result.Any(p => p.Id == 0));
|
|
|
|
|
Assert.True(result.Count() == 3);
|
2018-02-11 04:35:33 +01:00
|
|
|
|
}
|
2018-02-13 23:43:33 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-03-24 19:45:23 +01:00
|
|
|
|
public void CustomGenericFiltersWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-03-24 19:45:23 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Latest",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _comments);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.False(result.Any(p => p.Id == 0));
|
|
|
|
|
Assert.True(result.Count() == 2);
|
2019-03-24 19:45:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-11-30 00:08:39 +01:00
|
|
|
|
public void CustomFiltersWithOperatorsWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-11-30 00:08:39 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "HasInTitle==A",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Any(p => p.Id == 0));
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-11-30 00:08:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-06-14 10:50:19 +02:00
|
|
|
|
public void CustomFiltersMixedWithUsualWork1()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-06-14 10:50:19 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "Isnew,CategoryId==2",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Any(p => p.Id == 3));
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-06-14 10:50:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-06-14 10:50:19 +02:00
|
|
|
|
public void CustomFiltersMixedWithUsualWork2()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-06-14 10:50:19 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "CategoryId==2,Isnew",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Any(p => p.Id == 3));
|
|
|
|
|
Assert.True(result.Count() == 1);
|
2018-06-14 10:50:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-06-15 10:16:36 +02:00
|
|
|
|
public void CustomFiltersOnDifferentSourcesCanShareName()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var postModel = new SieveModel
|
2018-06-15 10:16:36 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "CategoryId==2,Isnew",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var postResult = _processor.Apply(postModel, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(postResult.Any(p => p.Id == 3));
|
|
|
|
|
Assert.Equal(1, postResult.Count());
|
2018-06-15 10:16:36 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var commentModel = new SieveModel
|
2018-06-15 10:16:36 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "Isnew",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var commentResult = _processor.Apply(commentModel, _comments);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(commentResult.Any(c => c.Id == 2));
|
|
|
|
|
Assert.Equal(2, commentResult.Count());
|
2018-06-15 10:16:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-07-04 05:06:37 +02:00
|
|
|
|
public void CustomSortsWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-07-04 05:06:37 +02:00
|
|
|
|
{
|
|
|
|
|
Sorts = "Popularity",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.False(result.First().Id == 0);
|
2018-07-04 05:06:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-03-24 19:45:23 +01:00
|
|
|
|
public void CustomGenericSortsWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-03-24 19:45:23 +01:00
|
|
|
|
{
|
|
|
|
|
Sorts = "Oldest",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.True(result.Last().Id == 0);
|
2019-03-24 19:45:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-13 23:43:33 +01:00
|
|
|
|
public void MethodNotFoundExceptionWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-13 23:43:33 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "does not exist",
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Throws<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
|
2018-02-13 23:43:33 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-02-13 23:43:33 +01:00
|
|
|
|
public void IncompatibleMethodExceptionsWork()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-02-13 23:43:33 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "TestComment",
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Throws<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts));
|
2018-02-13 23:43:33 +01:00
|
|
|
|
}
|
2018-04-20 10:46:04 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-11-16 09:08:25 +01:00
|
|
|
|
public void OrNameFilteringWorks()
|
2018-04-20 10:46:04 +02:00
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-04-20 10:46:04 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "(Title|LikeCount)==3",
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-20 11:27:04 +02:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2018-05-15 01:34:37 +02:00
|
|
|
|
var entry = result.FirstOrDefault();
|
|
|
|
|
var resultCount = result.Count();
|
2018-04-20 10:46:04 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.NotNull(entry);
|
|
|
|
|
Assert.Equal(1, resultCount);
|
|
|
|
|
Assert.Equal(3, entry.Id);
|
2018-04-20 10:46:04 +02:00
|
|
|
|
}
|
2019-11-17 00:15:07 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Theory]
|
|
|
|
|
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
|
|
|
|
|
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
|
2020-07-06 21:48:52 +02:00
|
|
|
|
public void CombinedAndOrFilterIndependentOfOrder(string filter)
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-07-06 21:48:52 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = filter,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
var entry = result.FirstOrDefault();
|
|
|
|
|
var resultCount = result.Count();
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.NotNull(entry);
|
|
|
|
|
Assert.Equal(1, resultCount);
|
2020-07-06 21:48:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2020-07-04 21:16:54 +02:00
|
|
|
|
public void CombinedAndOrWithSpaceFilteringWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-07-04 21:16:54 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title==D, (Title|LikeCount)==3",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
|
|
|
|
var entry = result.FirstOrDefault();
|
|
|
|
|
var resultCount = result.Count();
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.NotNull(entry);
|
|
|
|
|
Assert.Equal(1, resultCount);
|
|
|
|
|
Assert.Equal(3, entry.Id);
|
2020-07-04 21:16:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-11-16 09:08:25 +01:00
|
|
|
|
public void OrValueFilteringWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-11-16 09:08:25 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Title==C|D",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(2, result.Count());
|
|
|
|
|
Assert.True(result.Any(p => p.Id == 2));
|
|
|
|
|
Assert.True(result.Any(p => p.Id == 3));
|
2018-11-16 09:08:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2018-11-16 09:08:25 +01:00
|
|
|
|
public void OrValueFilteringWorks2()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2018-11-16 09:08:25 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "Text@=(|)",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _comments);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(1, result.Count());
|
|
|
|
|
Assert.Equal(2, result.FirstOrDefault()?.Id);
|
2018-11-16 09:08:25 +01:00
|
|
|
|
}
|
2019-01-18 11:45:38 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-01-18 11:45:38 +01:00
|
|
|
|
public void NestedFilteringWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-01-18 11:45:38 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "TopComment.Text!@=A",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(3, result.Count());
|
2019-01-18 11:45:38 +01:00
|
|
|
|
var posts = result.ToList();
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Contains("B", posts[0].TopComment.Text);
|
|
|
|
|
Assert.Contains("C", posts[1].TopComment.Text);
|
|
|
|
|
Assert.Contains("D", posts[2].TopComment.Text);
|
2019-01-18 11:45:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-01-18 11:45:38 +01:00
|
|
|
|
public void NestedSortingWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-01-18 11:45:38 +01:00
|
|
|
|
{
|
|
|
|
|
Sorts = "TopComment.Id",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(4, result.Count());
|
2019-01-18 11:45:38 +01:00
|
|
|
|
var posts = result.ToList();
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(0, posts[0].Id);
|
|
|
|
|
Assert.Equal(3, posts[1].Id);
|
|
|
|
|
Assert.Equal(2, posts[2].Id);
|
|
|
|
|
Assert.Equal(1, posts[3].Id);
|
2019-01-18 11:45:38 +01:00
|
|
|
|
}
|
2019-02-03 17:10:34 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2019-02-03 17:10:34 +01:00
|
|
|
|
public void NestedFilteringWithIdenticTypesWorks()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2019-02-03 17:10:34 +01:00
|
|
|
|
{
|
2019-03-17 23:23:37 +01:00
|
|
|
|
Filters = "(topc|featc)@=*2",
|
2019-02-03 17:10:34 +01:00
|
|
|
|
};
|
|
|
|
|
|
2019-03-17 23:23:37 +01:00
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(4, result.Count());
|
2019-02-03 17:10:34 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
model = new SieveModel
|
2019-03-17 23:23:37 +01:00
|
|
|
|
{
|
|
|
|
|
Filters = "(topc|featc)@=*B",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(1, result.Count());
|
2019-02-03 17:10:34 +01:00
|
|
|
|
}
|
2020-10-22 22:21:05 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2020-10-22 22:21:05 +02:00
|
|
|
|
public void FilteringNullsWorks()
|
|
|
|
|
{
|
|
|
|
|
var posts = new List<Post>
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2020-10-22 22:21:05 +02:00
|
|
|
|
Id = 1,
|
|
|
|
|
Title = null,
|
|
|
|
|
LikeCount = 0,
|
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = null,
|
|
|
|
|
TopComment = null,
|
|
|
|
|
FeaturedComment = null
|
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-10-22 22:21:05 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "FeaturedComment.Text!@=Some value",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(0, result.Count());
|
2020-10-22 22:21:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2020-10-22 22:21:05 +02:00
|
|
|
|
public void SortingNullsWorks()
|
|
|
|
|
{
|
|
|
|
|
var posts = new List<Post>
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2020-10-22 22:21:05 +02:00
|
|
|
|
Id = 1,
|
|
|
|
|
Title = null,
|
|
|
|
|
LikeCount = 0,
|
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = null,
|
|
|
|
|
TopComment = new Comment { Id = 1 },
|
|
|
|
|
FeaturedComment = null
|
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2020-10-22 22:21:05 +02:00
|
|
|
|
Id = 2,
|
|
|
|
|
Title = null,
|
|
|
|
|
LikeCount = 0,
|
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = null,
|
|
|
|
|
TopComment = null,
|
|
|
|
|
FeaturedComment = null
|
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-10-22 22:21:05 +02:00
|
|
|
|
{
|
|
|
|
|
Sorts = "TopComment.Id",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(2, result.Count());
|
2020-10-22 22:21:05 +02:00
|
|
|
|
var sortedPosts = result.ToList();
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(2, sortedPosts[0].Id);
|
|
|
|
|
Assert.Equal(1, sortedPosts[1].Id);
|
2020-10-22 22:21:05 +02:00
|
|
|
|
}
|
2020-10-23 18:15:58 +02:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2020-10-23 18:15:58 +02:00
|
|
|
|
public void FilteringOnNullWorks()
|
|
|
|
|
{
|
|
|
|
|
var posts = new List<Post>
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2020-10-23 18:15:58 +02:00
|
|
|
|
Id = 1,
|
|
|
|
|
Title = null,
|
|
|
|
|
LikeCount = 0,
|
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = null,
|
|
|
|
|
TopComment = null,
|
|
|
|
|
FeaturedComment = null
|
|
|
|
|
},
|
2021-05-13 14:04:18 +02:00
|
|
|
|
new Post
|
|
|
|
|
{
|
2020-10-23 18:15:58 +02:00
|
|
|
|
Id = 2,
|
|
|
|
|
Title = null,
|
|
|
|
|
LikeCount = 0,
|
|
|
|
|
IsDraft = false,
|
|
|
|
|
CategoryId = null,
|
|
|
|
|
TopComment = null,
|
|
|
|
|
FeaturedComment = new Comment { Id = 1, Text = null }
|
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-10-23 18:15:58 +02:00
|
|
|
|
{
|
|
|
|
|
Filters = "FeaturedComment.Text==null",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(1, result.Count());
|
2020-10-23 18:15:58 +02:00
|
|
|
|
var filteredPosts = result.ToList();
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(2, filteredPosts[0].Id);
|
2020-10-23 18:15:58 +02:00
|
|
|
|
}
|
2020-11-03 10:42:41 +01:00
|
|
|
|
|
2021-05-13 14:04:18 +02:00
|
|
|
|
[Fact]
|
2020-11-03 10:42:41 +01:00
|
|
|
|
public void BaseDefinedPropertyMappingSortingWorks_WithCustomName()
|
|
|
|
|
{
|
2021-05-13 14:04:18 +02:00
|
|
|
|
var model = new SieveModel
|
2020-11-03 10:42:41 +01:00
|
|
|
|
{
|
|
|
|
|
Sorts = "-CreateDate"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, _posts);
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(4, result.Count());
|
2020-11-03 10:42:41 +01:00
|
|
|
|
|
|
|
|
|
var posts = result.ToList();
|
2021-05-13 14:04:18 +02:00
|
|
|
|
Assert.Equal(3,posts[0].Id);
|
|
|
|
|
Assert.Equal(2,posts[1].Id);
|
|
|
|
|
Assert.Equal(1,posts[2].Id);
|
|
|
|
|
Assert.Equal(0,posts[3].Id);
|
2020-11-03 10:42:41 +01:00
|
|
|
|
}
|
2021-08-29 16:30:19 +02:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void CanFilter_WithEscapeCharacter()
|
|
|
|
|
{
|
|
|
|
|
var comments = new List<Comment>
|
|
|
|
|
{
|
|
|
|
|
new Comment
|
|
|
|
|
{
|
|
|
|
|
Id = 0,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow,
|
|
|
|
|
Text = "Here is, a comment"
|
|
|
|
|
},
|
|
|
|
|
new Comment
|
|
|
|
|
{
|
|
|
|
|
Id = 1,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
|
|
|
|
|
Text = "Here is, another comment"
|
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
|
|
|
|
|
|
|
|
|
var model = new SieveModel
|
|
|
|
|
{
|
|
|
|
|
Filters = "Text==Here is\\, another comment"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, comments);
|
|
|
|
|
Assert.Equal(1, result.Count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void OrEscapedPipeValueFilteringWorks()
|
|
|
|
|
{
|
|
|
|
|
var comments = new List<Comment>
|
|
|
|
|
{
|
|
|
|
|
new Comment
|
|
|
|
|
{
|
|
|
|
|
Id = 0,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow,
|
|
|
|
|
Text = "Here is | a comment"
|
|
|
|
|
},
|
|
|
|
|
new Comment
|
|
|
|
|
{
|
|
|
|
|
Id = 1,
|
|
|
|
|
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
|
|
|
|
|
Text = "Here is | another comment"
|
|
|
|
|
},
|
|
|
|
|
}.AsQueryable();
|
|
|
|
|
|
|
|
|
|
var model = new SieveModel()
|
|
|
|
|
{
|
|
|
|
|
Filters = "Text==Here is \\| a comment|Here is \\| another comment",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = _processor.Apply(model, comments);
|
|
|
|
|
Assert.Equal(2, result.Count());
|
|
|
|
|
}
|
2018-02-10 01:26:32 +01:00
|
|
|
|
}
|
2018-05-28 03:32:21 +02:00
|
|
|
|
}
|