Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BetimBeja committed May 8, 2023
1 parent 59723b5 commit 414fcbd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
12 changes: 6 additions & 6 deletions src/FakeXrmEasy.Core/Query/ConditionExpressionExtensions.In.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ internal static Expression ToInExpression(this TypedConditionExpression tc, Expr
{
if (value is Array)
{
foreach (var a in ((Array)value))
{
expOrValues = Expression.Or(expOrValues, Expression.Equal(
tc.AttributeType.GetAppropiateCastExpressionBasedOnType(getAttributeValueExpr, a),
TypeCastExpressions.GetAppropiateTypedValueAndType(a, tc.AttributeType)));
}
//foreach (var a in ((Array)value))
//{
// expOrValues = Expression.Or(expOrValues, Expression.Equal(
// tc.AttributeType.GetAppropiateCastExpressionBasedOnType(getAttributeValueExpr, a),
// TypeCastExpressions.GetAppropiateTypedValueAndType(a, tc.AttributeType)));
//}
}
else
{
Expand Down
16 changes: 12 additions & 4 deletions src/FakeXrmEasy.Core/Query/ConditionExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ internal static Expression ToExpression(this TypedConditionExpression c, QueryEx
break;

case ConditionOperator.In:
ValidateInConditionValues(c, entity.Name ?? qe.EntityName);
operatorExpression = c.ToInExpression(getNonBasicValueExpr, containsAttributeExpression);
break;

case ConditionOperator.NotIn:
ValidateInConditionValues(c, entity.Name ?? qe.EntityName);
operatorExpression = Expression.Not(c.ToInExpression(getNonBasicValueExpr, containsAttributeExpression));
break;

Expand Down Expand Up @@ -312,9 +314,15 @@ internal static Expression ToExpression(this TypedConditionExpression c, QueryEx

}





private static void ValidateInConditionValues(TypedConditionExpression c, string name)
{
foreach (object value in c.CondExpression.Values)
{
if (value is Array)
{
throw new Exception($"Condition for attribute '{name}.numberofemployees': expected argument(s) of a different type but received '{value.GetType()}'.");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ public void When_executing_a_query_expression_in_operator_throws_exception_for_o
[Fact]
public void When_executing_a_query_expression_in_operator_returns_exact_matches_for_single_int_array_right_hand_side()
{



_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand All @@ -254,7 +251,7 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_

var qe = new QueryExpression("contact");
qe.ColumnSet = new ColumnSet(new[] { "firstname" });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new[] { 2 });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new object[] { 2 });

var entities = _service.RetrieveMultiple(qe).Entities;

Expand All @@ -265,9 +262,6 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_
[Fact]
public void When_executing_a_query_expression_in_operator_returns_exact_matches_for_int_array_right_hand_side()
{



_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand All @@ -276,7 +270,7 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_

var qe = new QueryExpression("contact");
qe.ColumnSet = new ColumnSet(new[] { "firstname" });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new[] { 2, 3 });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new object[] { 2, 3 });

var entities = _service.RetrieveMultiple(qe).Entities;

Expand All @@ -287,9 +281,6 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_
[Fact]
public void When_executing_a_query_expression_in_operator_returns_exact_matches_for_out_of_order_int_array_right_hand_side()
{



_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand All @@ -298,7 +289,7 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_

var qe = new QueryExpression("contact");
qe.ColumnSet = new ColumnSet(new[] { "firstname" });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new[] { 3, 1, 2 });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.In, new object[] { 3, 1, 2 });

var entities = _service.RetrieveMultiple(qe).Entities;

Expand All @@ -309,9 +300,6 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_
[Fact]
public void When_executing_a_query_expression_in_operator_returns_exact_matches_for_out_of_order_int_params_right_hand_side()
{



_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand All @@ -331,9 +319,6 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_
[Fact]
public void When_executing_a_query_expression_in_operator_returns_exact_matches_for_string_array_right_hand_side()
{



_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand Down Expand Up @@ -396,10 +381,7 @@ public void When_executing_a_query_expression_in_operator_returns_exact_matches_

[Fact]
public void When_executing_a_query_expression_notin_operator_excludes_exact_matches_for_int_array_right_hand_side()
{



{
_service.Create(new Contact { FirstName = "1,2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(1), new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2) } });
_service.Create(new Contact { FirstName = "2,3", new_MultiSelectAttribute = new OptionSetValueCollection() { new OptionSetValue(2), new OptionSetValue(3) } });
Expand All @@ -408,7 +390,7 @@ public void When_executing_a_query_expression_notin_operator_excludes_exact_matc

var qe = new QueryExpression("contact");
qe.ColumnSet = new ColumnSet(new[] { "firstname" });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.NotIn, new[] { 2, 3 });
qe.Criteria.AddCondition("new_multiselectattribute", ConditionOperator.NotIn, new object[] { 2, 3 });

var entities = _service.RetrieveMultiple(qe).Entities;

Expand Down
20 changes: 20 additions & 0 deletions tests/FakeXrmEasy.Core.Tests/Issues/Issue0096.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Xrm.Sdk.Query;
using Xunit;

namespace FakeXrmEasy.Tests.Issues
{
public class Issue0096 : FakeXrmEasyTestsBase
{
[Fact]
public void Reproduce_issue_96()
{
var query = new QueryExpression("account");
query.TopCount = 2;
query.Criteria.AddCondition("numberofemployees", ConditionOperator.In, new int[] { 0, 1 });

var ex = Record.Exception(() => _service.RetrieveMultiple(query));
Assert.NotNull(ex);
Assert.Equal("Condition for attribute 'account.numberofemployees': expected argument(s) of a different type but received 'System.Int32[]'.", ex.Message);
}
}
}
4 changes: 2 additions & 2 deletions tests/FakeXrmEasy.Core.Tests/Issues/Issue180.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void When_a_query_on_lookup_with_condition_in_contains_a_match_it_should_
};

_context.Initialize(new List<Entity> { account });
var ids = new[] { account.OriginatingLeadId.Id, Guid.NewGuid(), Guid.NewGuid() };
var ids = new object[] { account.OriginatingLeadId.Id, Guid.NewGuid(), Guid.NewGuid() };

var qe = new QueryExpression(Account.EntityLogicalName);
qe.Criteria.AddCondition("originatingleadid", ConditionOperator.In, ids);
Expand All @@ -44,7 +44,7 @@ public void When_a_query_on_lookup_with_condition_in_contains_no_match_it_should

_context.Initialize(new List<Entity> { account });

var ids = new[] { Guid.Empty, Guid.Empty, Guid.Empty };
var ids = new object[] { Guid.Empty, Guid.Empty, Guid.Empty };

var qe = new QueryExpression(Account.EntityLogicalName);
qe.Criteria.AddCondition("originatingleadid", ConditionOperator.In, ids);
Expand Down

0 comments on commit 414fcbd

Please sign in to comment.