Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Jun 19, 2024
1 parent 159428b commit 4051019
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions test/Gridify.Tests/IssueTests/Issue165Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace Gridify.Tests.IssueTests;
Expand Down Expand Up @@ -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<TestModel>() { new() { Prop5 = new Dictionary<string, bool?> { { "subkey", true } } } }.AsQueryable();
static Expression<Func<TestModel, bool>> 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<string, bool?>).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<Func<TestModel, bool>>(condition, parameter);
}
var expected = ds.Where(BuildPredicate(true));

// act
var queryBuilder = new QueryBuilder<TestModel>()
.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<TestModel>() { new() { Prop3 = new Dictionary<Guid, string> { { id, "John" } } } }.AsQueryable();
static Expression<Func<TestModel, bool>> 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<Guid, string>).GetProperty("Item")?.GetGetMethod()!;
var indexer = Expression.Call(property, method, Expression.Constant(id));
var condition = Expression.Equal(indexer, Expression.Constant("John"));
return Expression.Lambda<Func<TestModel, bool>>(condition, parameter);
}
var expected = ds.Where(BuildPredicate(id));

// act
var queryBuilder = new QueryBuilder<TestModel>()
.AddMap<Guid>("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<TestModel>() { new() { Prop4 = new Dictionary<long, long> { { 44, 2024 } } } }.AsQueryable();

var expected = ds.Where(field => field.Prop4[44] == 2024);

// act
var queryBuilder = new QueryBuilder<TestModel>()
.AddMap<long>("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<string, string> Prop1 { get; set; } = new Dictionary<string, string>();
public IDictionary<int, string> Prop2 { get; set; } = new Dictionary<int, string>();
public IDictionary<Guid, string> Prop3 { get; set; } = new Dictionary<Guid, string>();
public IDictionary<long, long> Prop4 { get; set; } = new Dictionary<long, long>();
public IDictionary<string, bool?> Prop5 { get; set; } = new Dictionary<string, bool?>();
}
}

0 comments on commit 4051019

Please sign in to comment.