Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Commit 4c532b6

Browse files
committed
closes #10
Implement support for Concat function in OData 4.0
1 parent 0e36a4f commit 4c532b6

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

MicroLite.Extensions.WebApi.OData.Tests/Binders/FilterBinderTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,57 @@ public void ThereShouldBe2ArgumentValues()
199199
}
200200
}
201201

202+
public class WhenCallingApplyToWithConcatFunction
203+
{
204+
private readonly SqlQuery _sqlQuery;
205+
206+
public WhenCallingApplyToWithConcatFunction()
207+
{
208+
TestHelper.EnsureEDM();
209+
210+
var queryOptions = new ODataQueryOptions(
211+
"?$filter=concat(concat(Forename, ', '), Name) eq 'John, Smith'",
212+
EntityDataModel.Current.EntitySets["Customers"],
213+
Mock.Of<IODataQueryOptionsValidator>());
214+
215+
_sqlQuery = FilterBinder.BindFilter(queryOptions.Filter, ObjectInfo.For(typeof(Customer)), SqlBuilder.Select("*").From(typeof(Customer))).ToSqlQuery();
216+
}
217+
218+
[Fact]
219+
[Trait("Category", "Unit")]
220+
public void TheArgumentsShouldContainTheFirstQueryValue()
221+
{
222+
Assert.Equal(", ", _sqlQuery.Arguments[0].Value);
223+
}
224+
225+
[Fact]
226+
[Trait("Category", "Unit")]
227+
public void TheArgumentsShouldContainTheSecondQueryValue()
228+
{
229+
Assert.Equal("John, Smith", _sqlQuery.Arguments[1].Value);
230+
}
231+
232+
[Fact]
233+
[Trait("Category", "Unit")]
234+
public void TheCommandTextShouldContainTheWhereClause()
235+
{
236+
string expected = SqlBuilder.Select("*")
237+
.From(typeof(Customer))
238+
.Where("(Forename + ? + Name = ?)", ", ", "John, Smith")
239+
.ToSqlQuery()
240+
.CommandText;
241+
242+
Assert.Equal(expected, _sqlQuery.CommandText);
243+
}
244+
245+
[Fact]
246+
[Trait("Category", "Unit")]
247+
public void ThereShouldBe2ArgumentValues()
248+
{
249+
Assert.Equal(2, _sqlQuery.Arguments.Count);
250+
}
251+
}
252+
202253
public class WhenCallingBindFilterQueryOptionWithAPropertyEqualsAndGreaterThanAndLessThan
203254
{
204255
private readonly SqlQuery _sqlQuery;

MicroLite.Extensions.WebApi.OData.Tests/MicroLiteODataApiControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public void CeilingFunctionIsAllowed()
7575

7676
[Fact]
7777
[Trait("Category", "Unit")]
78-
public void ConcatFunctionIsNotAllowed()
78+
public void ConcatFunctionIsAllowed()
7979
{
80-
Assert.NotEqual(AllowedFunctions.Concat, _controller.ValidationSettings.AllowedFunctions & AllowedFunctions.Concat);
80+
Assert.Equal(AllowedFunctions.Concat, _controller.ValidationSettings.AllowedFunctions & AllowedFunctions.Concat);
8181
}
8282

8383
[Fact]

MicroLite.Extensions.WebApi.OData/Binders/FilterBinder.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ protected override void Bind(FunctionCallNode functionCallNode)
166166
_predicateBuilder.Append(")");
167167
break;
168168

169+
case "concat":
170+
Bind(parameters[0]);
171+
_predicateBuilder.Append(" + ");
172+
Bind(parameters[1]);
173+
break;
174+
169175
case "contains":
170176
Bind(parameters[0]);
171177
_predicateBuilder.Append(" LIKE ")

MicroLite.Extensions.WebApi.OData/MicroLiteODataApiController{TEntity,TEntityKey}.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected MicroLiteODataApiController(ISession session)
5959
AllowedArithmeticOperators = AllowedArithmeticOperators.All,
6060
AllowedFunctions =
6161
//// String functions
62+
AllowedFunctions.Concat |
6263
AllowedFunctions.Contains |
6364
AllowedFunctions.EndsWith |
6465
AllowedFunctions.StartsWith |

0 commit comments

Comments
 (0)