Skip to content

Commit 2ba5e6c

Browse files
authored
Generic Filter Mod to add GetValue (#86)
* closes #85 * Unit Test for Generic Filter Changes
1 parent f190624 commit 2ba5e6c

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/OLT.Searchers.GenericFilter/IOltGenericFilter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ public interface IOltGenericFilter
44
{
55
string Key { get; }
66
bool HasValue(IOltGenericParameter parameters);
7+
object GetValue(IOltGenericParameter parameters);
78
}
89

910
public interface IOltGenericFilter<TEntity> : IOltGenericFilter, IOltEntityQueryBuilder<TEntity>
1011
where TEntity : class, IOltEntity
1112
{
1213

1314
}
15+
1416
}

src/OLT.Searchers.GenericFilter/OltGenericFilter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,30 @@ public OltGenericFilter(IOltGenericParameterParser<TValueType> parser, IOltEntit
1818

1919

2020

21-
public bool HasValue(IOltGenericParameter parameters)
21+
public virtual bool HasValue(IOltGenericParameter parameters)
2222
{
2323
var hasValue = Parser.Parse(parameters);
2424
QueryBuilder.Value = Parser.Value;
2525
return hasValue;
2626
}
2727

28-
public IQueryable<TEntity> BuildQueryable(IQueryable<TEntity> queryable)
28+
public virtual object GetValue(IOltGenericParameter parameters)
29+
{
30+
var hasValue = Parser.Parse(parameters);
31+
if (hasValue)
32+
{
33+
return Parser.Value;
34+
}
35+
return default(TValueType);
36+
}
37+
38+
public virtual IQueryable<TEntity> BuildQueryable(IQueryable<TEntity> queryable)
2939
{
3040
if (!Parser.HasValue) return queryable;
3141
QueryBuilder.Value = Parser.Value;
3242
return QueryBuilder.BuildQueryable(queryable);
3343
}
44+
3445
}
3546

3647
}

tests/OLT.Searchers.Tests/Assets/FakeEntity.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,38 @@ public class FakeEntity : IOltEntity
1616
public string LastName { get; set; }
1717

1818
public int SelectValue { get; set; }
19+
public int? NullableInt { get; set; }
1920

2021
public DateTimeOffset SomeDate { get; set; }
2122

2223
public DateTimeOffset? DeletedOn { get; set; }
2324
public string DeletedBy { get; set; }
2425

26+
public static FakeEntity FakerData()
27+
{
28+
return new FakeEntity
29+
{
30+
Id = Faker.RandomNumber.Next(1000, 900000),
31+
UniqueId = Guid.NewGuid(),
32+
FirstName = Faker.Name.First(),
33+
LastName = Faker.Name.Last(),
34+
SelectValue = Faker.RandomNumber.Next(1000, 900000),
35+
NullableInt = Faker.RandomNumber.Next(1000, 900000),
36+
SomeDate = DateTimeOffset.Now.AddHours(Faker.RandomNumber.Next(-150, 150))
37+
};
38+
}
39+
40+
public static List<FakeEntity> FakerList(int number)
41+
{
42+
var list = new List<FakeEntity>();
43+
for (int i = 0; i < number; i++)
44+
{
45+
var item = FakerData();
46+
list.Add(item);
47+
}
48+
return list;
49+
}
50+
2551
public static FakeEntity FakerData(List<int> selectValues, bool deleted = false)
2652
{
2753
var result = new FakeEntity
@@ -31,6 +57,7 @@ public static FakeEntity FakerData(List<int> selectValues, bool deleted = false)
3157
FirstName = Faker.Name.First(),
3258
LastName = Faker.Name.Last(),
3359
SelectValue = selectValues.OrderBy(p => Guid.NewGuid()).FirstOrDefault(),
60+
NullableInt = Faker.RandomNumber.Next(1000, 900000),
3461
SomeDate = DateTimeOffset.Now.AddHours(Faker.RandomNumber.Next(-150, 150))
3562
};
3663

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using OLT.Constants;
2+
using OLT.Core;
3+
using OLT.Searchers.Tests.Assets;
4+
using System;
5+
using System.Linq;
6+
using Xunit;
7+
8+
namespace OLT.Searchers.Tests
9+
{
10+
public class GenericFilterTests
11+
{
12+
[Theory]
13+
[InlineData("Test", "Test", true)]
14+
[InlineData("", null, false)]
15+
[InlineData(" ", null, false)]
16+
[InlineData(null, null, false)]
17+
public void String(string value, string expected, bool parseable)
18+
{
19+
var entities = FakeEntity.FakerList(10).AsQueryable();
20+
var random = Faker.RandomNumber.Next();
21+
var key = $"key_{random}";
22+
var label = $"label_{random}";
23+
var template = new OltFilterTemplateString(key, label, false);
24+
var expr = new OltEntityExpressionStringStartsWith<FakeEntity>(p => p.FirstName);
25+
var genericParameter = TestHelper.BuildGenericParameter(key, value);
26+
var filter = new OltGenericFilter<FakeEntity, string>(template, expr);
27+
Assert.Equal(parseable, filter.HasValue(genericParameter));
28+
Assert.Equal(expected, filter.GetValue(genericParameter));
29+
30+
if (parseable)
31+
{
32+
Assert.Empty(filter.BuildQueryable(entities).ToList());
33+
}
34+
else
35+
{
36+
Assert.NotEmpty(filter.BuildQueryable(entities).ToList());
37+
}
38+
}
39+
40+
41+
}
42+
}

0 commit comments

Comments
 (0)