Skip to content

Commit

Permalink
fixing date comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
lostfields committed Jan 29, 2020
1 parent 6321223 commit 51c75f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/linq/expressions/expressionvisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class ExpressionVisitor implements IExpressionVisitor {
case 'DateLiteral':
let value = new Date(expression.value)

return new LiteralExpression(new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), 12, 0, 0, 999)))
return new LiteralExpression(new Date(Date.UTC(value.getUTCFullYear(), value.getUTCMonth(), value.getUTCDate(), 0, 0, 0, 0)))

case 'NumberLiteral':
return new LiteralExpression(Number(expression.value))
Expand Down
4 changes: 2 additions & 2 deletions src/linq/expressions/reducervisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ export class ReducerVisitor extends ExpressionVisitor {

switch (expression.operator) {
case LogicalOperatorType.Equal:
return new LiteralExpression(leftValue == rightValue);
return new LiteralExpression(leftValue >= rightValue && leftValue <= rightValue); // fixes date comparements
case LogicalOperatorType.NotEqual:
return new LiteralExpression(leftValue != rightValue);
return new LiteralExpression(leftValue < rightValue || leftValue > rightValue); // fixes date comparements
case LogicalOperatorType.And:
return new LiteralExpression(leftValue && rightValue);
case LogicalOperatorType.Or:
Expand Down
19 changes: 18 additions & 1 deletion src/test/odatavisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ODataVisitor } from './../linq/expressions/odatavisitor';

describe("When using OData for ExpressionVisitor", () => {
var reducer: ODataVisitor,
vars = { number: 5, stringhavingdate: '2018-05-30Z', string: 'abc', decimal: 5.50, date: new Date("2017-05-10T06:48:00Z"), object: { number: 7 } }
vars = { number: 5, stringhavingdate: '2018-05-30Z', string: 'abc', decimal: 5.50, dateonly: new Date("2020-01-28Z"), date: new Date("2017-05-10T06:48:00Z"), object: { number: 7 } }

beforeEach(() => {
reducer = new ODataVisitor();
Expand Down Expand Up @@ -98,6 +98,23 @@ describe("When using OData for ExpressionVisitor", () => {
assert.equal((<Expr.LiteralExpression>expr).value, true);
})

it("should evaluate a expression with date as type for greater and equal", () => {
let reduced = reducer.visitOData("dateonly ge 2020-01-28Z"),
expr = reducer.evaluate(reduced, vars);

assert.equal(expr.type, Expr.ExpressionType.Literal);
assert.equal((<Expr.LiteralExpression>expr).value, true);
})

it("should evaluate a expression with date as type for equal", () => {
let reduced = reducer.visitOData("dateonly eq 2020-01-28Z"),
expr = reducer.evaluate(reduced, vars);

assert.equal(expr.type, Expr.ExpressionType.Literal);
assert.equal((<Expr.LiteralExpression>expr).value, true);
})


it("should evaluate a expression with date as string", () => {
let reduced = reducer.visitOData("date ge datetime'2017-05-01Z'"),
expr = reducer.evaluate(reduced, vars);
Expand Down
2 changes: 1 addition & 1 deletion src/test/repository_readall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("When using repository to read all", () => {
it("should handle method calls", () => {
var list = repository.exposeFilters(new Enumerable<ICar>().where((car: ICar) => car.location.toLowerCase() == "no"));

if (Date.now() < new Date(2019, 11, 31).getTime())
if (Date.now() < new Date(2020, 4, 1).getTime())
return;

assert.ok(false);
Expand Down

0 comments on commit 51c75f6

Please sign in to comment.