|
| 1 | +using DevExtreme.AspNet.Data.Helpers; |
| 2 | +using Newtonsoft.Json; |
| 3 | +using System; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Linq.Expressions; |
| 8 | +using Xunit; |
| 9 | + |
| 10 | +namespace DevExtreme.AspNet.Data.Tests { |
| 11 | + |
| 12 | + public class CustomFilterCompilersTests { |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void OneToManyContains() { |
| 16 | + // https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/277#issuecomment-497249871 |
| 17 | + |
| 18 | + try { |
| 19 | + CustomFilterCompilers.RegisterBinaryExpressionCompiler(info => { |
| 20 | + if(info.DataItemExpression.Type == typeof(Category) && info.AccessorText == "Products" && info.Operation == "contains") { |
| 21 | + var text = Convert.ToString(info.Value); |
| 22 | + |
| 23 | + Expression<Func<Product, bool>> innerLambda = (p) => p.Name.ToLower().Contains(text); |
| 24 | + |
| 25 | + return Expression.Call( |
| 26 | + typeof(Enumerable), nameof(Enumerable.Any), new[] { typeof(Product) }, |
| 27 | + Expression.Property(info.DataItemExpression, "Products"), innerLambda |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | + }); |
| 33 | + |
| 34 | + var source = new[] { new Category(), new Category() }; |
| 35 | + source[0].Products.Add(new Product { Name = "Chai" }); |
| 36 | + |
| 37 | + var filter = JsonConvert.DeserializeObject<IList>(@"[ ""Products"", ""Contains"", ""ch"" ]"); |
| 38 | + |
| 39 | + var loadOptions = new SampleLoadOptions { |
| 40 | + Filter = filter, |
| 41 | + RequireTotalCount = true |
| 42 | + }; |
| 43 | + |
| 44 | + var loadResult = DataSourceLoader.Load(source, loadOptions); |
| 45 | + Assert.Equal(1, loadResult.totalCount); |
| 46 | + Assert.Contains(loadOptions.ExpressionLog, line => line.Contains(".Products.Any(p => p.Name.ToLower().Contains(")); |
| 47 | + } finally { |
| 48 | + CustomFilterCompilers.Binary.CompilerFuncs.Clear(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void ArrayContainsWithNullGuard() { |
| 54 | + try { |
| 55 | + CustomFilterCompilers.RegisterBinaryExpressionCompiler(info => { |
| 56 | + if(info.DataItemExpression.Type == typeof(Post) && info.AccessorText == "Tags" && info.Operation == "contains") { |
| 57 | + var text = Convert.ToString(info.Value); |
| 58 | + |
| 59 | + var tagsAccessor = Expression.Property(info.DataItemExpression, "Tags"); |
| 60 | + |
| 61 | + var containsCall = Expression.Call( |
| 62 | + typeof(Enumerable), nameof(Enumerable.Contains), new[] { typeof(string) }, |
| 63 | + tagsAccessor, Expression.Constant(text) /*, Expression.Constant(StringComparer.InvariantCultureIgnoreCase)*/ |
| 64 | + ); |
| 65 | + |
| 66 | + return Expression.Condition( |
| 67 | + Expression.MakeBinary(ExpressionType.NotEqual, tagsAccessor, Expression.Constant(null, typeof(string[]))), |
| 68 | + containsCall, |
| 69 | + Expression.Constant(false) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return null; |
| 74 | + }); |
| 75 | + |
| 76 | + var source = new[] { |
| 77 | + new Post { Tags = new[] { "news", "article" } }, |
| 78 | + new Post { Tags = new[] { "announcement" } } |
| 79 | + }; |
| 80 | + |
| 81 | + var loadOptions = new SampleLoadOptions { |
| 82 | + Filter = new[] { "Tags", "contains", "news" }, |
| 83 | + RequireTotalCount = true |
| 84 | + }; |
| 85 | + |
| 86 | + var loadResult = DataSourceLoader.Load(source, loadOptions); |
| 87 | + Assert.Equal(1, loadResult.totalCount); |
| 88 | + Assert.Contains(loadOptions.ExpressionLog, line => line.Contains(@".Where(obj => IIF((obj.Tags != null), obj.Tags.Contains(""news""), False))")); |
| 89 | + } finally { |
| 90 | + CustomFilterCompilers.Binary.CompilerFuncs.Clear(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + class Product { |
| 96 | + public string Name { get; set; } |
| 97 | + } |
| 98 | + |
| 99 | + class Category { |
| 100 | + public ICollection<Product> Products { get; } = new List<Product>(); |
| 101 | + } |
| 102 | + |
| 103 | + class Post { |
| 104 | + public string[] Tags { get; set; } |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments