mirror of
				https://github.com/Biarity/Sieve.git
				synced 2025-10-31 00:39:12 +01:00 
			
		
		
		
	* Migrate tests to xunit
* Update sample project to dotnetcore3.1 * Use Sqlite in sample project to run it everywhere * Fix: Filter with escaped comma * Fix: Filter "null" does not work with Contains or StartsWith * Code cleanup: Adjust namespaces, adjust usings
This commit is contained in:
		| @@ -1,33 +1,35 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using System.Text; | ||||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||
| using Sieve.Exceptions; | ||||
| using Sieve.Models; | ||||
| using Sieve.Services; | ||||
| using SieveUnitTests.Abstractions.Entity; | ||||
| using SieveUnitTests.Entities; | ||||
| using SieveUnitTests.Services; | ||||
| using Xunit; | ||||
| using Xunit.Abstractions; | ||||
|  | ||||
| namespace SieveUnitTests | ||||
| { | ||||
|     [TestClass] | ||||
|     public class GeneralWithInterfaces | ||||
|     { | ||||
|         private readonly ITestOutputHelper _testOutputHelper; | ||||
|         private readonly SieveProcessor _processor; | ||||
|         private readonly IQueryable<IPost> _posts; | ||||
|         private readonly IQueryable<Comment> _comments; | ||||
|  | ||||
|         public GeneralWithInterfaces() | ||||
|         public GeneralWithInterfaces(ITestOutputHelper testOutputHelper) | ||||
|         { | ||||
|             _testOutputHelper = testOutputHelper; | ||||
|             _processor = new ApplicationSieveProcessor(new SieveOptionsAccessor(), | ||||
|                 new SieveCustomSortMethods(), | ||||
|                 new SieveCustomFilterMethods()); | ||||
|  | ||||
|             _posts = new List<IPost> | ||||
|             { | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 0, | ||||
|                     Title = "A", | ||||
|                     LikeCount = 100, | ||||
| @@ -36,7 +38,8 @@ namespace SieveUnitTests | ||||
|                     TopComment = new Comment { Id = 0, Text = "A1" }, | ||||
|                     FeaturedComment = new Comment { Id = 4, Text = "A2" } | ||||
|                 }, | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 1, | ||||
|                     Title = "B", | ||||
|                     LikeCount = 50, | ||||
| @@ -45,7 +48,8 @@ namespace SieveUnitTests | ||||
|                     TopComment = new Comment { Id = 3, Text = "B1" }, | ||||
|                     FeaturedComment = new Comment { Id = 5, Text = "B2" } | ||||
|                 }, | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 2, | ||||
|                     Title = "C", | ||||
|                     LikeCount = 0, | ||||
| @@ -53,7 +57,8 @@ namespace SieveUnitTests | ||||
|                     TopComment = new Comment { Id = 2, Text = "C1" }, | ||||
|                     FeaturedComment = new Comment { Id = 6, Text = "C2" } | ||||
|                 }, | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 3, | ||||
|                     Title = "D", | ||||
|                     LikeCount = 3, | ||||
| @@ -66,17 +71,20 @@ namespace SieveUnitTests | ||||
|  | ||||
|             _comments = new List<Comment> | ||||
|             { | ||||
|                 new Comment() { | ||||
|                 new Comment | ||||
|                 { | ||||
|                     Id = 0, | ||||
|                     DateCreated = DateTimeOffset.UtcNow.AddDays(-20), | ||||
|                     Text = "This is an old comment." | ||||
|                 }, | ||||
|                 new Comment() { | ||||
|                 new Comment | ||||
|                 { | ||||
|                     Id = 1, | ||||
|                     DateCreated = DateTimeOffset.UtcNow.AddDays(-1), | ||||
|                     Text = "This is a fairly new comment." | ||||
|                 }, | ||||
|                 new Comment() { | ||||
|                 new Comment | ||||
|                 { | ||||
|                     Id = 2, | ||||
|                     DateCreated = DateTimeOffset.UtcNow, | ||||
|                     Text = "This is a brand new comment. (Text in braces)" | ||||
| @@ -84,263 +92,263 @@ namespace SieveUnitTests | ||||
|             }.AsQueryable(); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void ContainsCanBeCaseInsensitive() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title@=*a" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.AreEqual(result.First().Id, 0); | ||||
|             Assert.IsTrue(result.Count() == 1); | ||||
|             Assert.Equal(0, result.First().Id); | ||||
|             Assert.True(result.Count() == 1); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void NotEqualsCanBeCaseInsensitive() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title!=*a" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.AreEqual(result.First().Id, 1); | ||||
|             Assert.IsTrue(result.Count() == 3); | ||||
|             Assert.Equal(1, result.First().Id); | ||||
|             Assert.True(result.Count() == 3); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void ContainsIsCaseSensitive() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title@=a", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Count() == 0); | ||||
|             Assert.True(!result.Any()); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void NotContainsWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title!@=D", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Count() == 3); | ||||
|             Assert.True(result.Count() == 3); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CanFilterBools() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "IsDraft==false" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Count() == 2); | ||||
|             Assert.True(result.Count() == 2); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CanSortBools() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "-IsDraft" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.AreEqual(result.First().Id, 0); | ||||
|             Assert.Equal(0, result.First().Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CanFilterNullableInts() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "CategoryId==1" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Count() == 2); | ||||
|             Assert.True(result.Count() == 2); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void EqualsDoesntFailWithNonStringTypes() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "LikeCount==50", | ||||
|             }; | ||||
|  | ||||
|             Console.WriteLine(model.GetFiltersParsed()[0].Values); | ||||
|             Console.WriteLine(model.GetFiltersParsed()[0].Operator); | ||||
|             Console.WriteLine(model.GetFiltersParsed()[0].OperatorParsed); | ||||
|             _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.AreEqual(result.First().Id, 1); | ||||
|             Assert.IsTrue(result.Count() == 1); | ||||
|             Assert.Equal(1, result.First().Id); | ||||
|             Assert.True(result.Count() == 1); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomFiltersWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Isnew", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsFalse(result.Any(p => p.Id == 0)); | ||||
|             Assert.IsTrue(result.Count() == 3); | ||||
|             Assert.False(result.Any(p => p.Id == 0)); | ||||
|             Assert.True(result.Count() == 3); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomGenericFiltersWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Latest", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _comments); | ||||
|  | ||||
|             Assert.IsFalse(result.Any(p => p.Id == 0)); | ||||
|             Assert.IsTrue(result.Count() == 2); | ||||
|             Assert.False(result.Any(p => p.Id == 0)); | ||||
|             Assert.True(result.Count() == 2); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomFiltersWithOperatorsWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "HasInTitle==A", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Any(p => p.Id == 0)); | ||||
|             Assert.IsTrue(result.Count() == 1); | ||||
|             Assert.True(result.Any(p => p.Id == 0)); | ||||
|             Assert.True(result.Count() == 1); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomFiltersMixedWithUsualWork1() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Isnew,CategoryId==2", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Any(p => p.Id == 3)); | ||||
|             Assert.IsTrue(result.Count() == 1); | ||||
|             Assert.True(result.Any(p => p.Id == 3)); | ||||
|             Assert.True(result.Count() == 1); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomFiltersMixedWithUsualWork2() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "CategoryId==2,Isnew", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Any(p => p.Id == 3)); | ||||
|             Assert.IsTrue(result.Count() == 1); | ||||
|             Assert.True(result.Any(p => p.Id == 3)); | ||||
|             Assert.True(result.Count() == 1); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomFiltersOnDifferentSourcesCanShareName() | ||||
|         { | ||||
|             var postModel = new SieveModel() | ||||
|             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()); | ||||
|             Assert.True(postResult.Any(p => p.Id == 3)); | ||||
|             Assert.Equal(1, postResult.Count()); | ||||
|  | ||||
|             var commentModel = new SieveModel() | ||||
|             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()); | ||||
|             Assert.True(commentResult.Any(c => c.Id == 2)); | ||||
|             Assert.Equal(2, commentResult.Count()); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomSortsWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "Popularity", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsFalse(result.First().Id == 0); | ||||
|             Assert.False(result.First().Id == 0); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CustomGenericSortsWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "Oldest", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|  | ||||
|             Assert.IsTrue(result.Last().Id == 0); | ||||
|             Assert.True(result.Last().Id == 0); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void MethodNotFoundExceptionWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "does not exist", | ||||
|             }; | ||||
|  | ||||
|             Assert.ThrowsException<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts)); | ||||
|             Assert.Throws<SieveMethodNotFoundException>(() => _processor.Apply(model, _posts)); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void IncompatibleMethodExceptionsWork() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "TestComment", | ||||
|             }; | ||||
|  | ||||
|             Assert.ThrowsException<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts)); | ||||
|             Assert.Throws<SieveIncompatibleMethodException>(() => _processor.Apply(model, _posts)); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void OrNameFilteringWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "(Title|LikeCount)==3", | ||||
|             }; | ||||
| @@ -349,17 +357,17 @@ namespace SieveUnitTests | ||||
|             var entry = result.FirstOrDefault(); | ||||
|             var resultCount = result.Count(); | ||||
|  | ||||
|             Assert.IsNotNull(entry); | ||||
|             Assert.AreEqual(1, resultCount); | ||||
|             Assert.AreEqual(3, entry.Id); | ||||
|             Assert.NotNull(entry); | ||||
|             Assert.Equal(1, resultCount); | ||||
|             Assert.Equal(3, entry.Id); | ||||
|         } | ||||
|  | ||||
|         [DataTestMethod] | ||||
|         [DataRow("CategoryId==1,(CategoryId|LikeCount)==50")] | ||||
|         [DataRow("(CategoryId|LikeCount)==50,CategoryId==1")] | ||||
|         [Theory] | ||||
|         [InlineData("CategoryId==1,(CategoryId|LikeCount)==50")] | ||||
|         [InlineData("(CategoryId|LikeCount)==50,CategoryId==1")] | ||||
|         public void CombinedAndOrFilterIndependentOfOrder(string filter) | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = filter, | ||||
|             }; | ||||
| @@ -368,14 +376,14 @@ namespace SieveUnitTests | ||||
|             var entry = result.FirstOrDefault(); | ||||
|             var resultCount = result.Count(); | ||||
|  | ||||
|             Assert.IsNotNull(entry); | ||||
|             Assert.AreEqual(1, resultCount); | ||||
|             Assert.NotNull(entry); | ||||
|             Assert.Equal(1, resultCount); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void CombinedAndOrWithSpaceFilteringWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title==D, (Title|LikeCount)==3", | ||||
|             }; | ||||
| @@ -384,97 +392,98 @@ namespace SieveUnitTests | ||||
|             var entry = result.FirstOrDefault(); | ||||
|             var resultCount = result.Count(); | ||||
|  | ||||
|             Assert.IsNotNull(entry); | ||||
|             Assert.AreEqual(1, resultCount); | ||||
|             Assert.AreEqual(3, entry.Id); | ||||
|             Assert.NotNull(entry); | ||||
|             Assert.Equal(1, resultCount); | ||||
|             Assert.Equal(3, entry.Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void OrValueFilteringWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Title==C|D", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(2, result.Count()); | ||||
|             Assert.IsTrue(result.Any(p => p.Id == 2)); | ||||
|             Assert.IsTrue(result.Any(p => p.Id == 3)); | ||||
|             Assert.Equal(2, result.Count()); | ||||
|             Assert.True(result.Any(p => p.Id == 2)); | ||||
|             Assert.True(result.Any(p => p.Id == 3)); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void OrValueFilteringWorks2() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "Text@=(|)", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _comments); | ||||
|             Assert.AreEqual(1, result.Count()); | ||||
|             Assert.AreEqual(2, result.FirstOrDefault().Id); | ||||
|             Assert.Equal(1, result.Count()); | ||||
|             Assert.Equal(2, result.FirstOrDefault()?.Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void NestedFilteringWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "TopComment.Text!@=A", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(3, result.Count()); | ||||
|             Assert.Equal(3, result.Count()); | ||||
|             var posts = result.ToList(); | ||||
|             Assert.IsTrue(posts[0].TopComment.Text.Contains("B")); | ||||
|             Assert.IsTrue(posts[1].TopComment.Text.Contains("C")); | ||||
|             Assert.IsTrue(posts[2].TopComment.Text.Contains("D")); | ||||
|             Assert.Contains("B", posts[0].TopComment.Text); | ||||
|             Assert.Contains("C", posts[1].TopComment.Text); | ||||
|             Assert.Contains("D", posts[2].TopComment.Text); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void NestedSortingWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "TopComment.Id", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(4, result.Count()); | ||||
|             Assert.Equal(4, result.Count()); | ||||
|             var posts = result.ToList(); | ||||
|             Assert.AreEqual(posts[0].Id, 0); | ||||
|             Assert.AreEqual(posts[1].Id, 3); | ||||
|             Assert.AreEqual(posts[2].Id, 2); | ||||
|             Assert.AreEqual(posts[3].Id, 1); | ||||
|             Assert.Equal(0, posts[0].Id); | ||||
|             Assert.Equal(3, posts[1].Id); | ||||
|             Assert.Equal(2, posts[2].Id); | ||||
|             Assert.Equal(1, posts[3].Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void NestedFilteringWithIdenticTypesWorks() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "(topc|featc)@=*2", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(4, result.Count()); | ||||
|             Assert.Equal(4, result.Count()); | ||||
|  | ||||
|             model = new SieveModel() | ||||
|             model = new SieveModel | ||||
|             { | ||||
|                 Filters = "(topc|featc)@=*B", | ||||
|             }; | ||||
|  | ||||
|             result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(1, result.Count()); | ||||
|             Assert.Equal(1, result.Count()); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void FilteringNullsWorks() | ||||
|         { | ||||
|             var posts = new List<Post> | ||||
|             { | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 1, | ||||
|                     Title = null, | ||||
|                     LikeCount = 0, | ||||
| @@ -485,21 +494,22 @@ namespace SieveUnitTests | ||||
|                 }, | ||||
|             }.AsQueryable(); | ||||
|  | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "FeaturedComment.Text!@=Some value", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, posts); | ||||
|             Assert.AreEqual(0, result.Count()); | ||||
|             Assert.Equal(0, result.Count()); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void SortingNullsWorks() | ||||
|         { | ||||
|             var posts = new List<Post> | ||||
|             { | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 1, | ||||
|                     Title = null, | ||||
|                     LikeCount = 0, | ||||
| @@ -508,7 +518,8 @@ namespace SieveUnitTests | ||||
|                     TopComment =  new Comment { Id = 1 }, | ||||
|                     FeaturedComment = null | ||||
|                 }, | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 2, | ||||
|                     Title = null, | ||||
|                     LikeCount = 0, | ||||
| @@ -519,24 +530,25 @@ namespace SieveUnitTests | ||||
|                 }, | ||||
|             }.AsQueryable(); | ||||
|  | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "TopComment.Id", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, posts); | ||||
|             Assert.AreEqual(2, result.Count()); | ||||
|             Assert.Equal(2, result.Count()); | ||||
|             var sortedPosts = result.ToList(); | ||||
|             Assert.AreEqual(sortedPosts[0].Id, 2); | ||||
|             Assert.AreEqual(sortedPosts[1].Id, 1); | ||||
|             Assert.Equal(2, sortedPosts[0].Id); | ||||
|             Assert.Equal(1, sortedPosts[1].Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void FilteringOnNullWorks() | ||||
|         { | ||||
|             var posts = new List<Post> | ||||
|             { | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 1, | ||||
|                     Title = null, | ||||
|                     LikeCount = 0, | ||||
| @@ -545,7 +557,8 @@ namespace SieveUnitTests | ||||
|                     TopComment = null, | ||||
|                     FeaturedComment = null | ||||
|                 }, | ||||
|                 new Post() { | ||||
|                 new Post | ||||
|                 { | ||||
|                     Id = 2, | ||||
|                     Title = null, | ||||
|                     LikeCount = 0, | ||||
| @@ -556,33 +569,33 @@ namespace SieveUnitTests | ||||
|                 }, | ||||
|             }.AsQueryable(); | ||||
|  | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Filters = "FeaturedComment.Text==null", | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, posts); | ||||
|             Assert.AreEqual(1, result.Count()); | ||||
|             Assert.Equal(1, result.Count()); | ||||
|             var filteredPosts = result.ToList(); | ||||
|             Assert.AreEqual(filteredPosts[0].Id, 2); | ||||
|             Assert.Equal(2, filteredPosts[0].Id); | ||||
|         } | ||||
|  | ||||
|         [TestMethod] | ||||
|         [Fact] | ||||
|         public void BaseDefinedPropertyMappingSortingWorks_WithCustomName() | ||||
|         { | ||||
|             var model = new SieveModel() | ||||
|             var model = new SieveModel | ||||
|             { | ||||
|                 Sorts = "-CreateDate" | ||||
|             }; | ||||
|  | ||||
|             var result = _processor.Apply(model, _posts); | ||||
|             Assert.AreEqual(4, result.Count()); | ||||
|             Assert.Equal(4, result.Count()); | ||||
|  | ||||
|             var posts = result.ToList(); | ||||
|             Assert.AreEqual(posts[0].Id, 3); | ||||
|             Assert.AreEqual(posts[1].Id, 2); | ||||
|             Assert.AreEqual(posts[2].Id, 1); | ||||
|             Assert.AreEqual(posts[3].Id, 0); | ||||
|             Assert.Equal(3,posts[0].Id); | ||||
|             Assert.Equal(2,posts[1].Id); | ||||
|             Assert.Equal(1,posts[2].Id); | ||||
|             Assert.Equal(0,posts[3].Id); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user