Sieve/SieveUnitTests/General.cs

877 lines
25 KiB
C#
Raw Normal View History

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;
using Sieve.Models;
using Sieve.Services;
using SieveUnitTests.Entities;
using SieveUnitTests.Services;
using Xunit;
using Xunit.Abstractions;
namespace SieveUnitTests
{
public class General
{
private readonly ITestOutputHelper _testOutputHelper;
2018-05-15 01:34:37 +02:00
private readonly SieveProcessor _processor;
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
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;
private readonly IQueryable<Comment> _comments;
public General(ITestOutputHelper testOutputHelper)
{
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
var nullableAccessor = new SieveOptionsAccessor();
nullableAccessor.Value.IgnoreNullsOnNotEqual = false;
_testOutputHelper = testOutputHelper;
2019-01-18 11:45:38 +01:00
_processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(),
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
_nullableProcessor = new ApplicationSieveProcessor(nullableAccessor,
new SieveCustomSortMethods(),
new SieveCustomFilterMethods());
_posts = new List<Post>
{
new Post
{
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,
TopComment = new Comment { Id = 0, Text = "A1" },
FeaturedComment = new Comment { Id = 4, Text = "A2" }
},
new Post
{
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,
TopComment = new Comment { Id = 3, Text = "B1" },
FeaturedComment = new Comment { Id = 5, Text = "B2" }
},
new Post
{
Id = 2,
Title = "C",
2018-05-28 03:32:21 +02:00
LikeCount = 0,
CategoryId = 1,
TopComment = new Comment { Id = 2, Text = "C1" },
FeaturedComment = new Comment { Id = 6, Text = "C2" }
},
new Post
{
Id = 3,
2018-05-15 01:34:37 +02:00
Title = "D",
LikeCount = 3,
2018-05-28 03:32:21 +02:00
IsDraft = true,
CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" }
},
new Post
{
Id = 4,
Title = "Yen",
LikeCount = 5,
IsDraft = true,
CategoryId = 5,
TopComment = new Comment { Id = 4, Text = "Yen3" },
FeaturedComment = new Comment { Id = 8, Text = "Yen4" }
}
}.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. (Text in braces, comma separated)"
},
}.AsQueryable();
}
[Fact]
public void ContainsCanBeCaseInsensitive()
{
var model = new SieveModel
{
Filters = "Title@=*a"
};
2018-04-20 11:27:04 +02:00
var result = _processor.Apply(model, _posts);
Assert.Equal(0, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
public void NotEqualsCanBeCaseInsensitive()
{
var model = new SieveModel
{
Filters = "Title!=*a"
};
var result = _processor.Apply(model, _posts);
Assert.Equal(1, result.First().Id);
Assert.True(result.Count() == 4);
}
[Fact]
public void EndsWithWorks()
{
var model = new SieveModel
{
Filters = "Title_-=n"
};
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
public void EndsWithCanBeCaseInsensitive()
{
var model = new SieveModel
{
Filters = "Title_-=*N"
};
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
public void ContainsIsCaseSensitive()
{
var model = new SieveModel
{
Filters = "Title@=a",
};
2018-04-20 11:27:04 +02:00
var result = _processor.Apply(model, _posts);
Assert.True(!result.Any());
}
[Fact]
2019-01-08 17:20:49 +01:00
public void NotContainsWorks()
{
var model = new SieveModel
2019-01-08 17:20:49 +01:00
{
Filters = "Title!@=D",
};
var result = _processor.Apply(model, _posts);
Assert.True(result.Count() == 4);
2019-01-08 17:20:49 +01:00
}
[Fact]
2018-04-08 05:46:16 +02:00
public void CanFilterBools()
{
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
Assert.True(result.Count() == 2);
2018-04-08 05:46:16 +02:00
}
[Fact]
2018-04-08 05:46:16 +02:00
public void CanSortBools()
{
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
Assert.Equal(0, result.First().Id);
2018-04-08 05:46:16 +02:00
}
[Fact]
2018-05-28 03:32:21 +02:00
public void CanFilterNullableInts()
{
var model = new SieveModel
2018-05-28 03:32:21 +02:00
{
Filters = "CategoryId==1"
};
var result = _processor.Apply(model, _posts);
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
var nullableResult = _nullableProcessor.Apply(model, _posts);
2018-05-28 03:32:21 +02:00
Assert.True(result.Count() == 2);
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
Assert.True(nullableResult.Count() == 2);
}
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
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() == 2);
Assert.True(nullableResult.Count() == 3);
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +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
}
[Fact]
public void EqualsDoesntFailWithNonStringTypes()
{
var model = new SieveModel
{
Filters = "LikeCount==50",
};
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Values.ToString());
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].Operator);
_testOutputHelper.WriteLine(model.GetFiltersParsed()[0].OperatorParsed.ToString());
2018-04-20 11:27:04 +02:00
var result = _processor.Apply(model, _posts);
Assert.Equal(1, result.First().Id);
Assert.True(result.Count() == 1);
}
[Fact]
public void CustomFiltersWork()
{
var model = new SieveModel
{
Filters = "Isnew",
};
2018-04-20 11:27:04 +02:00
var result = _processor.Apply(model, _posts);
Assert.False(result.Any(p => p.Id == 0));
Assert.True(result.Count() == 4);
}
2018-02-13 23:43:33 +01:00
[Fact]
public void CustomGenericFiltersWork()
{
var model = new SieveModel
{
Filters = "Latest",
};
var result = _processor.Apply(model, _comments);
Assert.False(result.Any(p => p.Id == 0));
Assert.True(result.Count() == 2);
}
[Fact]
public void CustomFiltersWithOperatorsWork()
{
var model = new SieveModel
{
Filters = "HasInTitle==A",
};
var result = _processor.Apply(model, _posts);
Assert.True(result.Any(p => p.Id == 0));
Assert.True(result.Count() == 1);
}
[Fact]
2018-06-14 10:50:19 +02:00
public void CustomFiltersMixedWithUsualWork1()
{
var model = new SieveModel
2018-06-14 10:50:19 +02:00
{
Filters = "Isnew,CategoryId==2",
};
var result = _processor.Apply(model, _posts);
Assert.True(result.Any(p => p.Id == 3));
Assert.True(result.Count() == 1);
2018-06-14 10:50:19 +02:00
}
[Fact]
2018-06-14 10:50:19 +02:00
public void CustomFiltersMixedWithUsualWork2()
{
var model = new SieveModel
2018-06-14 10:50:19 +02:00
{
Filters = "CategoryId==2,Isnew",
};
var result = _processor.Apply(model, _posts);
Assert.True(result.Any(p => p.Id == 3));
Assert.True(result.Count() == 1);
2018-06-14 10:50:19 +02:00
}
[Fact]
public void CustomFiltersOnDifferentSourcesCanShareName()
{
var postModel = new SieveModel
{
Filters = "CategoryId==2,Isnew",
};
var postResult = _processor.Apply(postModel, _posts);
Assert.True(postResult.Any(p => p.Id == 3));
Assert.Equal(1, postResult.Count());
var commentModel = new SieveModel
{
Filters = "Isnew",
};
var commentResult = _processor.Apply(commentModel, _comments);
Assert.True(commentResult.Any(c => c.Id == 2));
Assert.Equal(2, commentResult.Count());
}
[Fact]
2018-07-04 05:06:37 +02:00
public void CustomSortsWork()
{
var model = new SieveModel
2018-07-04 05:06:37 +02:00
{
Sorts = "Popularity",
};
var result = _processor.Apply(model, _posts);
Assert.False(result.First().Id == 0);
2018-07-04 05:06:37 +02:00
}
[Fact]
public void CustomGenericSortsWork()
{
var model = new SieveModel
{
Sorts = "Oldest",
};
var result = _processor.Apply(model, _posts);
Assert.True(result.Last().Id == 0);
}
[Fact]
2018-02-13 23:43:33 +01:00
public void MethodNotFoundExceptionWork()
{
var model = new SieveModel
2018-02-13 23:43:33 +01:00
{
Filters = "does not exist",
};
Assert.Throws<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts));
2018-02-13 23:43:33 +01:00
}
[Fact]
2018-02-13 23:43:33 +01:00
public void IncompatibleMethodExceptionsWork()
{
var model = new SieveModel
2018-02-13 23:43:33 +01:00
{
Filters = "TestComment",
};
Assert.Throws<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts));
2018-02-13 23:43:33 +01:00
}
[Fact]
public void OrNameFilteringWorks()
{
var model = new SieveModel
{
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();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
Assert.Equal(3, entry.Id);
}
2019-11-17 00:15:07 +01:00
[Theory]
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
public void CombinedAndOrFilterIndependentOfOrder(string filter)
{
var model = new SieveModel
{
Filters = filter,
};
var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Fact]
public void CombinedAndOrWithSpaceFilteringWorks()
{
var model = new SieveModel
{
Filters = "Title==D, (Title|LikeCount)==3",
};
var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
Assert.Equal(3, entry.Id);
}
[Fact]
public void OrValueFilteringWorks()
{
var model = new SieveModel
{
Filters = "Title==C|D",
};
var result = _processor.Apply(model, _posts);
Assert.Equal(2, result.Count());
Assert.True(result.Any(p => p.Id == 2));
Assert.True(result.Any(p => p.Id == 3));
}
[Fact]
public void OrValueFilteringWorks2()
{
var model = new SieveModel
{
Filters = "Text@=(|)",
};
var result = _processor.Apply(model, _comments);
Assert.Equal(1, result.Count());
Assert.Equal(2, result.FirstOrDefault()?.Id);
}
2019-01-18 11:45:38 +01:00
[Fact]
2019-01-18 11:45:38 +01:00
public void NestedFilteringWorks()
{
var model = new SieveModel
2019-01-18 11:45:38 +01:00
{
Filters = "TopComment.Text!@=A",
};
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.Count());
2019-01-18 11:45:38 +01:00
var posts = result.ToList();
Assert.Contains("B", posts[0].TopComment.Text);
Assert.Contains("C", posts[1].TopComment.Text);
Assert.Contains("D", posts[2].TopComment.Text);
Assert.Contains("Yen", posts[3].TopComment.Text);
2019-01-18 11:45:38 +01:00
}
[Fact]
2019-01-18 11:45:38 +01:00
public void NestedSortingWorks()
{
var model = new SieveModel
2019-01-18 11:45:38 +01:00
{
Sorts = "TopComment.Id",
};
var result = _processor.Apply(model, _posts);
Assert.Equal(5, result.Count());
2019-01-18 11:45:38 +01:00
var posts = result.ToList();
Assert.Equal(0, posts[0].Id);
Assert.Equal(3, posts[1].Id);
Assert.Equal(2, posts[2].Id);
Assert.Equal(1, posts[3].Id);
Assert.Equal(4, posts[4].Id);
2019-01-18 11:45:38 +01:00
}
[Fact]
public void NestedFilteringWithIdenticTypesWorks()
{
var model = new SieveModel
{
Filters = "(topc|featc)@=*2",
};
var result = _processor.Apply(model, _posts);
Assert.Equal(4, result.Count());
model = new SieveModel
{
Filters = "(topc|featc)@=*B",
};
result = _processor.Apply(model, _posts);
Assert.Equal(1, result.Count());
}
[Fact]
public void FilteringNullsWorks()
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = null,
LikeCount = 0,
IsDraft = false,
CategoryId = null,
TopComment = null,
FeaturedComment = null
},
}.AsQueryable();
var model = new SieveModel
{
Filters = "FeaturedComment.Text!@=Some value",
};
var result = _processor.Apply(model, posts);
Assert.Equal(0, result.Count());
}
[Fact]
public void SortingNullsWorks()
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = null,
LikeCount = 0,
IsDraft = false,
CategoryId = null,
TopComment = new Comment { Id = 1 },
FeaturedComment = null
},
new Post
{
Id = 2,
Title = null,
LikeCount = 0,
IsDraft = false,
CategoryId = null,
TopComment = null,
FeaturedComment = null
},
}.AsQueryable();
var model = new SieveModel
{
Sorts = "TopComment.Id",
};
var result = _processor.Apply(model, posts);
Assert.Equal(2, result.Count());
var sortedPosts = result.ToList();
Assert.Equal(2, sortedPosts[0].Id);
Assert.Equal(1, sortedPosts[1].Id);
}
2020-10-23 18:15:58 +02:00
[Fact]
2020-10-23 18:15:58 +02:00
public void FilteringOnNullWorks()
{
var posts = new List<Post>
{
new Post
{
2020-10-23 18:15:58 +02:00
Id = 1,
Title = null,
LikeCount = 0,
IsDraft = false,
CategoryId = null,
TopComment = null,
FeaturedComment = null
},
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();
var model = new SieveModel
2020-10-23 18:15:58 +02:00
{
Filters = "FeaturedComment.Text==null",
};
var result = _processor.Apply(model, posts);
Assert.Equal(1, result.Count());
2020-10-23 18:15:58 +02:00
var filteredPosts = result.ToList();
Assert.Equal(2, filteredPosts[0].Id);
2020-10-23 18:15:58 +02:00
}
[Fact]
public void BaseDefinedPropertyMappingSortingWorks_WithCustomName()
{
var model = new SieveModel
{
Sorts = "-CreateDate"
};
var result = _processor.Apply(model, _posts);
Assert.Equal(5, result.Count());
var posts = result.ToList();
Assert.Equal(4, posts[0].Id);
Assert.Equal(3,posts[1].Id);
Assert.Equal(2,posts[2].Id);
Assert.Equal(1,posts[3].Id);
Assert.Equal(0,posts[4].Id);
}
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
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"
},
new Comment
{
Id = 2,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = @"Here is \| another comment(1)"
}
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
}.AsQueryable();
var model = new SieveModel
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
{
Filters = @"Text==Here is \| a comment|Here is \| another comment|Here is \\\| another comment(1)",
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
};
var result = _processor.Apply(model, comments);
Assert.Equal(3, result.Count());
}
[Theory]
[InlineData("CategoryId==1,(CategoryId|LikeCount)==50")]
[InlineData("(CategoryId|LikeCount)==50,CategoryId==1")]
public void CanFilterWithEscape(string filter)
{
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, _posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[InlineData(@"Title@=\\")]
public void CanFilterWithEscapedBackSlash(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = "E\\",
LikeCount = 4,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "E1" },
FeaturedComment = new Comment { Id = 7, Text = "E2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
}
[Theory]
[InlineData(@"Title@=\== ")]
[InlineData(@"Title@=\!= ")]
[InlineData(@"Title@=\> ")]
[InlineData(@"Title@=\< ")]
[InlineData(@"Title@=\<= ")]
[InlineData(@"Title@=\>= ")]
[InlineData(@"Title@=\@= ")]
[InlineData(@"Title@=\_= ")]
[InlineData(@"Title@=\_-= ")]
[InlineData(@"Title@=!\@= ")]
[InlineData(@"Title@=!\_= ")]
[InlineData(@"Title@=!\_-= ")]
[InlineData(@"Title@=\@=* ")]
[InlineData(@"Title@=\_=* ")]
[InlineData(@"Title@=\_-=* ")]
[InlineData(@"Title@=\==* ")]
[InlineData(@"Title@=\!=* ")]
[InlineData(@"Title@=!\@=* ")]
public void CanFilterWithEscapedOperators(string filter)
{
var posts = new List<Post>
{
new Post
{
Id = 1,
Title = @"Operators: == != > < >= <= @= _= _-= !@= !_= !_-= @=* _=* ==* !=* !@=* !_=* !_-=* ",
LikeCount = 1,
IsDraft = true,
CategoryId = 1,
TopComment = new Comment { Id = 1, Text = "F1" },
FeaturedComment = new Comment { Id = 7, Text = "F2" }
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter,
};
var result = _processor.Apply(model, posts);
var entry = result.FirstOrDefault();
var resultCount = result.Count();
Assert.NotNull(entry);
Assert.Equal(1, resultCount);
Release Sieve 2.5.0 (#151) * Setup release 2.5.0 with automated build and pre-releases * #80 added support for escaping pipe control characters (#113) * #80 added support for escaping comma and pipe control characters * Update SieveModel.cs Fix build. Accidentally broken by resolving conflicts. * Migrate UnitTests to xUnit Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer13 <kevin@ksommer.eu> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> * SieveProcessor.Options made protected property (#134) Mapper assignment in constructor is moved to a null-coalescing member pair (a field and a property) "IncludeScopes" switch is removed from appSettings.{env}.json files * Revert to _mapper assignment in constructor. (#140) * reverting fix (#142) * Revert to _mapper assignment in constructor. * reverting fix * pass filter values as parameters (#112) make GetClosureOverConstant really work * Make ApplyFiltering, ApplySorting and ApplyPagination protected virtual #139 (#144) * stop excluding null values when filtering using notEqual (#114) * stop excluding null values when filtering using notEqual * add IgnoreNullsOnNotEqual config field, to enable/disable the new feature Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com> Co-authored-by: Clayton Andersen <tunaman65@gmail.com> Co-authored-by: Clayton Andersen <candersen@restaurant365.com> Co-authored-by: ITDancer139 <kevinitdancersommer@gmail.com> Co-authored-by: Hasan Manzak <hasan.manzak@gmail.com> Co-authored-by: alicak <alicak@users.noreply.github.com> Co-authored-by: AnasZakarneh <Zakarnehanas1@gmail.com> Co-authored-by: AnasZakarneh <a.zakarneh@foothillsolutions.com>
2021-08-29 16:30:19 +02:00
}
2024-05-27 22:11:10 +02:00
[Theory]
[InlineData(@"HasInTitleStatic@=Tale", 1)]
[InlineData(@"HasInTitleStatic@=Tail", 0)]
public void CanFilterWithStaticFilters(string filter, int expectedMatches)
{
var posts = new List<Post>
{
new()
{
Id = 1,
Title = "A Tale of Two Cities",
}
}.AsQueryable();
var model = new SieveModel
{
Filters = filter
};
var result = _processor.Apply(model, posts);
var resultCount = result.Count();
Assert.Equal(expectedMatches, resultCount);
}
}
2018-05-28 03:32:21 +02:00
}