Skip to content

Commit

Permalink
test: simpleType collections
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezanet committed Jun 22, 2024
1 parent 87e86a4 commit a5c5137
Showing 1 changed file with 130 additions and 11 deletions.
141 changes: 130 additions & 11 deletions test/Gridify.Tests/IssueTests/Issue168Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,153 @@

namespace Gridify.Tests.IssueTests;

[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract")]
public class Issue168Tests
{
[Fact]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public void IssueStory()
public void ApplyFiltering_WithoutMapper_ShouldGenerateMappingsForListOfStrings()
{
// arrange
var dataSource = new List<Artist>()
var dataSource = new List<Test>()
{
new() {FavouriteColors = ["Green", "Blue"]},
new() {FavouriteColors = ["White", "Yellow"]},
new() {FavouriteColorList = ["Green", "Blue"]},
new() {FavouriteColorList = ["White", "Yellow"]},
}.AsQueryable();

var mapper = new GridifyMapper<Artist>()
.AddMap("FavouriteColors", w => w.FavouriteColors.Select(Param_0 => Param_0));
var expected = dataSource.ApplyFiltering("FavouriteColors=Red|FavouriteColors=Blue", mapper);
var mapper = new GridifyMapper<Test>()
.AddMap("FavouriteColorList", w => w.FavouriteColorList.Select(Param_0 => Param_0));
var expected = dataSource.ApplyFiltering("FavouriteColorList=Red|FavouriteColorList=Blue", mapper);

// act
var actual = dataSource.ApplyFiltering("FavouriteColors=Red|FavouriteColors=Blue");
var actual = dataSource.ApplyFiltering("FavouriteColorList=Red|FavouriteColorList=Blue");

// assert
Assert.Equal(expected.ToString(), actual.ToString());
var actualList = actual.ToList();
Assert.Equal(expected.ToList(), actualList);
Assert.Single(actualList);
}
private class Artist

[Fact]
public void GenerateMappings_ShouldGenerateMappingsForSimpleTypes()
{
var gm = new GridifyMapper<Test>()
.GenerateMappings();

Assert.Equal(typeof(Test).GetProperties().Length, gm.GetCurrentMaps().Count());
}

[Fact]
public void ApplyFiltering_WithoutMapper_ShouldGenerateMappingsForArrayOfStrings()
{
// arrange
var dataSource = new List<Test>()
{
new() {FavouriteColorsArray = ["Green", "Blue"]},
new() {FavouriteColorsArray = ["White", "Yellow"]},
}.AsQueryable();

var mapper = new GridifyMapper<Test>()
.AddMap("FavouriteColorsArray", w => w.FavouriteColorsArray.Select(Param_0 => Param_0));
var expected = dataSource.ApplyFiltering("FavouriteColorsArray=Red|FavouriteColorsArray=Blue", mapper);

// act
var actual = dataSource.ApplyFiltering("FavouriteColorsArray=Red|FavouriteColorsArray=Blue");

// assert
Assert.Equal(expected.ToString(), actual.ToString());
var actualList = actual.ToList();
Assert.Equal(expected.ToList(), actualList);
Assert.Single(actualList);
}

[Fact]
public void ApplyFiltering_WithoutMapper_ShouldGenerateTheCorrectQuery()
{
// arrange
var dataSource = new List<Test>()
{
new() {NumbersArray = [1, 2]},
new() {NumbersArray = [3, 4]},
}.AsQueryable();

var expected = dataSource.Where(__Test => __Test.NumbersArray != null && __Test.NumbersArray.Contains(2));

// act
var actual = dataSource.ApplyFiltering("NumbersArray=2");

// assert
Assert.Equal(expected.ToString(), actual.ToString());
var actualList = actual.ToList();
Assert.Equal(expected.ToList(), actualList);
Assert.Single(actualList);
GridifyGlobalConfiguration.DisableNullChecks = false;
}

[Fact]
public void ApplyFiltering_WithoutMapperWithMultipleConditions_ShouldGenerateTheCorrectQuery()
{
public List<string> FavouriteColors { get; set; } = [];
// arrange
var dataSource = new List<Test>()
{
new() {NumbersArray = [1, 2]},
new() {NumbersArray = [3, 4]},
}.AsQueryable();

var expected = dataSource.Where(__Test =>
__Test.NumbersArray != null && __Test.NumbersArray.Contains(2) || __Test.NumbersArray != null && __Test.NumbersArray.Contains(5));

// act
var actual = dataSource.ApplyFiltering("NumbersArray=2|NumbersArray=5");

// assert
Assert.Equal(expected.ToString(), actual.ToString());
var actualList = actual.ToList();
Assert.Equal(expected.ToList(), actualList);
Assert.Single(actualList);
GridifyGlobalConfiguration.DisableNullChecks = false;
}

[Fact]
public void ApplyFiltering_WithoutMapper_ShouldGenerateMappingsForEnumArray()
{
// arrange
var dataSource = new List<Test>()
{
new() {EnumsArray = [Test.MyEnum.Item1, Test.MyEnum.Item2]},
new() {EnumsArray = [Test.MyEnum.Item1]},
}.AsQueryable();

var expected = dataSource.Where(__Test =>
__Test.EnumsArray != null && __Test.EnumsArray.Contains(Test.MyEnum.Item1) ||
__Test.EnumsArray != null && __Test.EnumsArray.Contains(Test.MyEnum.Item2));

// act
var actual = dataSource.ApplyFiltering("EnumsArray=Item1|EnumsArray=Item2");

// assert
Assert.Equal(expected.ToString(), actual.ToString());
var actualList = actual.ToList();
Assert.Equal(expected.ToList(), actualList);
Assert.Equal(2, actualList.Count);
}


private class Test
{
public List<string> FavouriteColorList { get; set; } = [];
public string[] FavouriteColorsArray { get; set; } = [];

public int[] NumbersArray { get; set; } = [];
public IEnumerable<long> NumbersEnumerable { get; set; } = [];
public MyEnum[] EnumsArray { get; set; } = [];

public enum MyEnum
{
Item1,
Item2
}

}
}

0 comments on commit a5c5137

Please sign in to comment.