-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use parameterized names in dynamic query #399
Merged
JonathanMagnan
merged 2 commits into
zzzprojects:master
from
ascott18:UseParameterizedNamesInDynamicQuery
Aug 7, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
using FluentAssertions; | ||
using Xunit; | ||
using User = System.Linq.Dynamic.Core.Tests.Helpers.Models.User; | ||
using System.Linq.Dynamic.Core.Tests.TestHelpers; | ||
|
||
namespace System.Linq.Dynamic.Core.Tests | ||
{ | ||
|
@@ -251,6 +252,25 @@ public Type ResolveTypeBySimpleName(string typeName) | |
} | ||
} | ||
|
||
[Fact] | ||
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_false() | ||
{ | ||
// Assign | ||
var config = new ParsingConfig | ||
{ | ||
UseParameterizedNamesInDynamicQuery = false | ||
}; | ||
|
||
// Act | ||
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\""); | ||
|
||
// Assert | ||
dynamic constantExpression = (ConstantExpression)(expression.Body as BinaryExpression).Right; | ||
string value = constantExpression.Value; | ||
|
||
Check.That(value).IsEqualTo("x"); | ||
} | ||
|
||
[Fact] | ||
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_true() | ||
{ | ||
|
@@ -261,16 +281,49 @@ public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQu | |
}; | ||
|
||
// Act | ||
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\""); | ||
var expression = DynamicExpressionParser.ParseLambda<Person, bool>(config, false, "Id = 42"); | ||
string expressionAsString = expression.ToString(); | ||
|
||
// Assert | ||
Check.That(expressionAsString).IsEqualTo("Param_0 => (Param_0.Id == value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[System.Int32]).Value)"); | ||
|
||
dynamic constantExpression = ((MemberExpression)(expression.Body as BinaryExpression).Right).Expression as ConstantExpression; | ||
dynamic wrappedObj = constantExpression.Value; | ||
|
||
var propertyInfo = wrappedObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public); | ||
string value = propertyInfo.GetValue(wrappedObj) as string; | ||
int value = (int) propertyInfo.GetValue(wrappedObj); | ||
|
||
Check.That(value).IsEqualTo("x"); | ||
Check.That(value).IsEqualTo(42); | ||
} | ||
|
||
[Theory] | ||
[InlineData("NullableIntValue", "42")] | ||
[InlineData("NullableDoubleValue", "42.23")] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_ForNullableProperty_true(string propName, string valueString) | ||
{ | ||
// Assign | ||
var config = new ParsingConfig | ||
{ | ||
UseParameterizedNamesInDynamicQuery = true | ||
}; | ||
|
||
// Act | ||
var expression = DynamicExpressionParser.ParseLambda<SimpleValuesModel, bool>(config, false, $"{propName} = {valueString}"); | ||
string expressionAsString = expression.ToString(); | ||
|
||
// Assert | ||
var queriedProp = typeof(SimpleValuesModel).GetProperty(propName, BindingFlags.Instance | BindingFlags.Public); | ||
var queriedPropType = queriedProp.PropertyType; | ||
var queriedPropUnderlyingType = Nullable.GetUnderlyingType(queriedPropType); | ||
|
||
Check.That(expressionAsString).IsEqualTo($"Param_0 => (Param_0.{propName} == {ExpressionString.NullableConversion($"value(System.Linq.Dynamic.Core.Parser.WrappedValue`1[{queriedPropUnderlyingType}]).Value")})"); | ||
dynamic constantExpression = ((MemberExpression)(((expression.Body as BinaryExpression).Right as UnaryExpression).Operand)).Expression as ConstantExpression; | ||
object wrapperObj = constantExpression.Value; | ||
|
||
var propertyInfo = wrapperObj.GetType().GetProperty("Value", BindingFlags.Instance | BindingFlags.Public); | ||
object value = propertyInfo.GetValue(wrapperObj); | ||
|
||
Check.That(value).IsEqualTo(Convert.ChangeType(valueString, Nullable.GetUnderlyingType(queriedPropType) ?? queriedPropType)); | ||
} | ||
|
||
[Theory] | ||
|
@@ -298,25 +351,6 @@ public void DynamicExpressionParser_ParseLambda_WithStructWithEquality(string qu | |
Check.That(result.ToArray()[0]).Equals(expected[0]); | ||
} | ||
|
||
[Fact] | ||
public void DynamicExpressionParser_ParseLambda_UseParameterizedNamesInDynamicQuery_false() | ||
{ | ||
// Assign | ||
var config = new ParsingConfig | ||
{ | ||
UseParameterizedNamesInDynamicQuery = false | ||
}; | ||
|
||
// Act | ||
var expression = DynamicExpressionParser.ParseLambda<string, bool>(config, true, "s => s == \"x\""); | ||
|
||
// Assert | ||
dynamic constantExpression = (ConstantExpression)(expression.Body as BinaryExpression).Right; | ||
string value = constantExpression.Value; | ||
|
||
Check.That(value).IsEqualTo("x"); | ||
} | ||
|
||
[Fact] | ||
public void DynamicExpressionParser_ParseLambda_ToList() | ||
{ | ||
|
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
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
20 changes: 20 additions & 0 deletions
20
test/System.Linq.Dynamic.Core.Tests/TestHelpers/ExpressionString.cs
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,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Linq.Dynamic.Core.Tests.TestHelpers | ||
{ | ||
public static class ExpressionString | ||
{ | ||
public static string NullableConversion(string convertedExpr) | ||
{ | ||
#if NET452 | ||
return $"Convert({convertedExpr})"; | ||
#else | ||
return $"Convert({convertedExpr}, Nullable`1)"; | ||
#endif | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@StefH This is the principal change here. I'll let you determine if this makes sense to fix the issue in this way or not.