-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
Oops, something went wrong.