From 40510190dad9e3a09e22a8d26d9fc5936028bc8f Mon Sep 17 00:00:00 2001 From: AliReZa Sabouri Date: Thu, 20 Jun 2024 01:00:30 +0200 Subject: [PATCH] test: add more tests --- .../Gridify.Tests/IssueTests/Issue165Tests.cs | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/test/Gridify.Tests/IssueTests/Issue165Tests.cs b/test/Gridify.Tests/IssueTests/Issue165Tests.cs index 5adae7ad..b0fe722a 100644 --- a/test/Gridify.Tests/IssueTests/Issue165Tests.cs +++ b/test/Gridify.Tests/IssueTests/Issue165Tests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using Xunit; namespace Gridify.Tests.IssueTests; @@ -65,12 +66,91 @@ private void Filtering_WithDictionaryGuidSubKey_ShouldReturnTheCorrectResult() Assert.Equal(expected.ToString(), actual.ToString()); } + [Fact] + private void Filtering_WithDictionaryNullableSubKeyValue_ShouldReturnTheCorrectResult() + { + // arrange + var ds = new List() { new() { Prop5 = new Dictionary { { "subkey", true } } } }.AsQueryable(); + static Expression> BuildPredicate(bool? value) // generates: (field => field.Prop5["subkey"] == true) without convert() + { + var parameter = Expression.Parameter(typeof(TestModel), "field"); + var property = Expression.Property(parameter, nameof(TestModel.Prop5)); + var method = typeof(IDictionary).GetProperty("Item")?.GetGetMethod()!; + var indexer = Expression.Call(property, method, Expression.Constant("subkey")); + var condition = Expression.Equal(indexer, Expression.Constant(value, typeof(bool?))); + return Expression.Lambda>(condition, parameter); + } + var expected = ds.Where(BuildPredicate(true)); + + // act + var queryBuilder = new QueryBuilder() + .AddMap("prop5", (field, key) => field.Prop5[key]) + .AddCondition("prop5{subkey} = true"); + var actual = queryBuilder.Build(ds); + + // assert + Assert.NotEmpty(expected.ToList()); + Assert.Equal(expected.ToList().Count, actual.ToList().Count); + Assert.Equal(expected.ToString(), actual.ToString()); + } + + [Fact] + private void FilteringWithGenericAddMap_WithDictionaryGuidSubKey_ShouldReturnTheCorrectResult() + { + // arrange + var id = Guid.Parse("0f0ee7914ba7496aa13ae5ce4ff058b1"); + var ds = new List() { new() { Prop3 = new Dictionary { { id, "John" } } } }.AsQueryable(); + static Expression> BuildPredicate(Guid id) // generates: (field => field.Prop3[id] == "John") + { + var parameter = Expression.Parameter(typeof(TestModel), "field"); + var property = Expression.Property(parameter, nameof(TestModel.Prop3)); + var method = typeof(IDictionary).GetProperty("Item")?.GetGetMethod()!; + var indexer = Expression.Call(property, method, Expression.Constant(id)); + var condition = Expression.Equal(indexer, Expression.Constant("John")); + return Expression.Lambda>(condition, parameter); + } + var expected = ds.Where(BuildPredicate(id)); + + // act + var queryBuilder = new QueryBuilder() + .AddMap("prop3", (field, key) => field.Prop3[key]) + .AddCondition($"prop3{{{id:N}}} = John"); + var actual = queryBuilder.Build(ds); + + // assert + Assert.NotEmpty(expected.ToList()); + Assert.Equal(expected.ToList().Count, actual.ToList().Count); + Assert.Equal(expected.ToString(), actual.ToString()); + } + + [Fact] + private void FilteringWithGenericAddMap_WithDictionaryLongSubKey_ShouldReturnTheCorrectResult() + { + // arrange + var ds = new List() { new() { Prop4 = new Dictionary { { 44, 2024 } } } }.AsQueryable(); + + var expected = ds.Where(field => field.Prop4[44] == 2024); + + // act + var queryBuilder = new QueryBuilder() + .AddMap("prop4", (field, key) => field.Prop4[key]) + .AddCondition("prop4{44} = 2024"); + var actual = queryBuilder.Build(ds); + + // assert + Assert.NotEmpty(expected.ToList()); + Assert.Equal(expected.ToList().Count, actual.ToList().Count); + Assert.Equal(expected.ToString(), actual.ToString()); + } + private class TestModel { public string Id { get; set; } = Guid.NewGuid().ToString("N"); public IDictionary Prop1 { get; set; } = new Dictionary(); public IDictionary Prop2 { get; set; } = new Dictionary(); public IDictionary Prop3 { get; set; } = new Dictionary(); + public IDictionary Prop4 { get; set; } = new Dictionary(); + public IDictionary Prop5 { get; set; } = new Dictionary(); } }