Skip to content

Commit

Permalink
Support casts in LINQ provider
Browse files Browse the repository at this point in the history
Update `WiqlTranslator` to support casts in LINQ queries. Added unit
tests for that and the `as` operator to validate changes.
  • Loading branch information
MattKotsenas committed May 24, 2018
1 parent f72dde5 commit 1815ed7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Qwiq.Linq/WiqlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ protected override Expression VisitUnary(UnaryExpression node)
_expressionInProgress.Enqueue(new StringFragment(" NOT "));
Visit(node.Operand);
break;
case ExpressionType.Convert:
case ExpressionType.ConvertChecked:
case ExpressionType.TypeAs:
// Casting a type; do nothing because WIQL handles type conversion
Visit(node.Operand);
Expand Down
54 changes: 54 additions & 0 deletions test/Qwiq.Linq.Tests/QueryBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,60 @@

namespace Qwiq.Linq
{
[TestClass]
// ReSharper disable once InconsistentNaming
public class when_a_query_has_a_cast_in_it : WiqlQueryBuilderContextSpecification
{
private DateTime _date;

public override void Given()
{
base.Given();
_date = DateTime.SpecifyKind(new DateTime(2012, 11, 29, 17, 0, 0), DateTimeKind.Utc);
}

public override void When()
{
base.When();
Expected =
"SELECT * FROM WorkItems WHERE (([Changed Date] > '2012-11-29 17:00:00Z'))";
Actual = Query.Where(item => (DateTime)item["Changed Date"] > _date).ToString();
}

[TestMethod]
public void it_is_translated_to_a_where()
{
Actual.ShouldEqual(Expected);
}
}

[TestClass]
// ReSharper disable once InconsistentNaming
public class when_a_query_has_an_as_operator_in_it : WiqlQueryBuilderContextSpecification
{
private DateTime _date;

public override void Given()
{
base.Given();
_date = DateTime.SpecifyKind(new DateTime(2012, 11, 29, 17, 0, 0), DateTimeKind.Utc);
}

public override void When()
{
base.When();
Expected =
"SELECT * FROM WorkItems WHERE (([Changed Date] > '2012-11-29 17:00:00Z'))";
Actual = Query.Where(item => (item["Changed Date"] as DateTime?) > _date).ToString();
}

[TestMethod]
public void it_is_translated_to_a_where()
{
Actual.ShouldEqual(Expected);
}
}

[TestClass]
// ReSharper disable once InconsistentNaming
public class when_a_query_has_a_where_clause_with_an_and_expression : WiqlQueryBuilderContextSpecification
Expand Down

0 comments on commit 1815ed7

Please sign in to comment.