Skip to content

Commit 67f2135

Browse files
committed
Added tests for start/end with and fixed bug
1 parent 11131df commit 67f2135

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/DatatablesParser/DatatablesParser.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class Parser<T> where T : class
1919
private int _take;
2020
private int _skip;
2121
private bool _sortDisabled = false;
22-
private string _startsWithtoken = "*|";
23-
private string _endsWithToken = "|*";
22+
private string _startsWithtoken = Constants.DEFAULT_STARTS_WITH_TOKEN;
23+
private string _endsWithToken = Constants.DEFAULT_ENDS_WITH_TOKEN;
2424
private bool _isEnumerableQuery;
2525

2626
private Dictionary<string,Expression> _converters = new Dictionary<string, Expression>();
@@ -282,6 +282,23 @@ private string GetFilterFn(string filter)
282282
}
283283
}
284284

285+
private string RemoveFilterTokens(string filter)
286+
{
287+
string untoken = filter;
288+
289+
if(untoken.StartsWith(_startsWithtoken))
290+
{
291+
untoken = untoken.Remove(0,_startsWithtoken.Length);
292+
}
293+
294+
if(untoken.EndsWith(_endsWithToken))
295+
{
296+
untoken = untoken.Remove(filter.LastIndexOf(_endsWithToken),_endsWithToken.Length);
297+
}
298+
299+
return untoken;
300+
}
301+
285302
/// <summary>
286303
/// Generate a lamda expression based on a search filter for all mapped columns
287304
/// </summary>
@@ -297,6 +314,7 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
297314
if(!string.IsNullOrWhiteSpace(filter))
298315
{
299316
globalFilterFn = GetFilterFn(filter);
317+
filter = RemoveFilterTokens(filter);
300318
globalFilterConst = Expression.Constant(filter.ToLower());
301319
}
302320

@@ -318,7 +336,8 @@ private Expression<Func<T, bool>> GenerateEntityFilter()
318336
ConstantExpression individualFilterConst = null;
319337
if(!string.IsNullOrWhiteSpace(propMap.Value.Filter))
320338
{
321-
propFilterFn = GetFilterFn(propMap.Value.Filter);
339+
propFilterFn = GetFilterFn(propMap.Value.Filter);
340+
propMap.Value.Filter = RemoveFilterTokens(propMap.Value.Filter);
322341
individualFilterConst = Expression.Constant(propMap.Value.Filter.ToLower());
323342
}
324343

@@ -440,6 +459,9 @@ public class Constants
440459
public const string STARTS_WITH_FN = "StartsWith";
441460
public const string ENDS_WITH_FN = "EndsWith";
442461

462+
public const string DEFAULT_STARTS_WITH_TOKEN = "*|";
463+
public const string DEFAULT_ENDS_WITH_TOKEN = "|*";
464+
443465
public static string GetKey(string format,string index)
444466
{
445467
return String.Format(format, index);

test/DatatablesParser.Tests/LinqToObjectTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,42 @@ public void TotalDisplayIndividualMutiTest()
105105

106106
}
107107

108+
[Fact]
109+
public void StartsWithTest()
110+
{
111+
var context = TestHelper.GetInMemoryContext();
112+
var p = TestHelper.CreateParams();
113+
var displayLength = 1;
114+
115+
//Set filter parameter
116+
p[Constants.SEARCH_KEY] = new StringValues(Constants.DEFAULT_STARTS_WITH_TOKEN + "Cr");
117+
118+
var parser = new Parser<Person>(p, context.People.ToList().AsQueryable());
119+
120+
Console.WriteLine("InMemory - StartsWithTest: {0}",context.People.Count());
121+
122+
Assert.Equal(displayLength, parser.Parse().recordsFiltered);
123+
124+
}
125+
126+
[Fact]
127+
public void EndsWithTest()
128+
{
129+
var context = TestHelper.GetInMemoryContext();
130+
var p = TestHelper.CreateParams();
131+
var displayLength = 4;
132+
133+
//Set filter parameter
134+
p[Constants.SEARCH_KEY] = new StringValues("ie" + Constants.DEFAULT_ENDS_WITH_TOKEN );
135+
136+
var parser = new Parser<Person>(p, context.People.ToList().AsQueryable());
137+
138+
Console.WriteLine("InMemory - EndsWithTest: {0}",context.People.Count());
139+
140+
Assert.Equal(displayLength, parser.Parse().recordsFiltered);
141+
142+
}
143+
144+
108145
}
109146
}

0 commit comments

Comments
 (0)