Skip to content

Commit

Permalink
Add base filtering implementation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bardin08 authored Jun 25, 2024
2 parents 2323e00 + 8373bdb commit 9d6a82c
Show file tree
Hide file tree
Showing 23 changed files with 1,455 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Ctoss.Example/Ctoss.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Ctoss\Ctoss.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions Ctoss.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Ctoss;
using Ctoss.Example;

const string jsonString = """
{
"tin": {
"filterType": "date",
"condition1": {
"filterType": "date",
"type": "inRange",
"dateFrom": "10/10/2002",
"dateTo": "10/12/2020"
},
"conditions": [
{
"filterType": "date",
"type": "inRange",
"date": "10/10/2002",
"dateTo": "10/12/2020"
}
]
}
}
""";

var filterBuilder = new FilterBuilder();
var expr = filterBuilder.GetExpression<Entity>(jsonString);
Console.WriteLine(expr);

namespace Ctoss.Example
{
class Entity
{
public DateTime Tin { get; set; }
}
}
27 changes: 27 additions & 0 deletions Ctoss.Tests/Ctoss.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ctoss\Ctoss.csproj" />
</ItemGroup>

</Project>
235 changes: 235 additions & 0 deletions Ctoss.Tests/DateFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
using Ctoss.Models;
using Ctoss.Models.Conditions;
using Ctoss.Models.Enums;
using Ctoss.Tests.Models;

namespace Ctoss.Tests;

public class DateFilterTests
{
private readonly FilterBuilder _filterBuilder = new();

private readonly List<TestEntity> _testEntities =
[
new TestEntity
{
NumericProperty = 10, StringProperty = "abc", DateTimeProperty = new DateTime(2022, 1, 1)
},
new TestEntity
{
NumericProperty = 20, StringProperty = "def", DateTimeProperty = new DateTime(2023, 2, 2)
},
new TestEntity
{
NumericProperty = 30, StringProperty = "ghi", DateTimeProperty = new DateTime(2024, 3, 3)
}
];

[Fact]
public void DateFilter_Equals_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.Equals
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Single(result);
Assert.Equal(new DateTime(2022, 1, 1), result.First().DateTimeProperty);
}

[Fact]
public void DateFilter_GreaterThen_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "02/02/2023",
FilterType = "date",
Type = DateFilterOptions.GreaterThen
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Single(result);
Assert.Equal(new DateTime(2024, 3, 3), result.First().DateTimeProperty);
}

[Fact]
public void DateFilter_Blank_Success()
{
var condition = new DateFilterCondition
{
FilterType = "date",
Type = DateFilterOptions.Blank
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Empty(result);
}

[Fact]
public void DateFilter_LessThen_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "01/01/2023",
FilterType = "date",
Type = DateFilterOptions.LessThen
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Single(result);
Assert.Equal(new DateTime(2022, 1, 1), result.First().DateTimeProperty);
}

[Fact]
public void DateFilter_NotBlank_Success()
{
var condition = new DateFilterCondition
{
FilterType = "date",
Type = DateFilterOptions.NotBlank
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Equal(3, result.Count);
}

[Fact]
public void DateFilter_InRange_Success()
{
var condition = new DateFilterCondition
{
DateFrom = "06/06/2021",
DateTo = "09/09/2024",
FilterType = "date",
Type = DateFilterOptions.InRange
};

var filter = new NumberFilter
{
FilterType = "date",
Condition1 = condition,
Conditions = new List<FilterCondition> { condition }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Equal(3, result.Count);
}

[Fact]
public void DateFilter_NotEquals_Success()
{
var condition1 = new DateFilterCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};
var condition2 = new DateFilterCondition
{
DateFrom = "03/03/2024",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};

var filter = new NumberFilter
{
FilterType = "date",
Operator = Operator.And,
Condition1 = condition1,
Condition2 = condition2,
Conditions = new List<FilterCondition> { condition1, condition2 }
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Single(result);
Assert.Equal(new DateTime(2023, 02, 02), result.First().DateTimeProperty);
}

[Fact]
public void DateFilter_Composed_Success()
{
var condition1 = new DateFilterCondition
{
DateFrom = "01/01/2022",
FilterType = "date",
Type = DateFilterOptions.NotEquals
};

var condition2 = new DateFilterCondition
{
DateFrom = "03/03/2024",
FilterType = "date",
Type = DateFilterOptions.LessThen
};

var filter = new NumberFilter
{
Operator = Operator.And,
FilterType = "date",
Condition1 = condition1,
Condition2 = condition2,
Conditions = new List<FilterCondition>
{
condition1, condition2
}
};

var expr = _filterBuilder.GetExpression<TestEntity>("DateTimeProperty", filter)!;
var result = _testEntities.AsQueryable().Where(expr).ToList();

Assert.Single(result);
Assert.Equal(new DateTime(2023, 02, 02), result.First().DateTimeProperty);
}
}
8 changes: 8 additions & 0 deletions Ctoss.Tests/Models/TestEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Ctoss.Tests.Models;

public class TestEntity
{
public string StringProperty { get; set; } = null!;
public DateTime DateTimeProperty { get; set; }
public int NumericProperty { get; set; }
}
Loading

0 comments on commit 9d6a82c

Please sign in to comment.