Skip to content
Snippets Groups Projects
Commit 3a5557e5 authored by Tom Robinson's avatar Tom Robinson
Browse files

Fix computeFilterTimeRange and unit tests

parent cc414ed7
No related branches found
No related tags found
No related merge requests found
......@@ -20,11 +20,11 @@ export function computeFilterTimeRange(filter) {
start = point.clone().startOf(bucketing);
end = point.clone().endOf(bucketing);
} else if (operator === ">" && values[0]) {
start = absolute(values[0]).startOf(bucketing);
start = absolute(values[0]).endOf(bucketing);
end = max();
} else if (operator === "<" && values[0]) {
start = min();
end = absolute(values[0]).endOf(bucketing);
end = absolute(values[0]).startOf(bucketing);
} else if (operator === "BETWEEN" && values[0] && values[1]) {
start = absolute(values[0]).startOf(bucketing);
end = absolute(values[1]).endOf(bucketing);
......@@ -51,7 +51,7 @@ export function expandTimeIntervalFilter(filter) {
field = ["datetime_field", field, "as", unit];
if (n < -1) {
return ["BETWEEN", field, ["relative_datetime", n, unit], ["relative_datetime", -1, unit]];
return ["BETWEEN", field, ["relative_datetime", n-1, unit], ["relative_datetime", -1, unit]];
} else if (n > 1) {
return ["BETWEEN", field, ["relative_datetime", 1, unit], ["relative_datetime", n, unit]];
} else if (n === 0) {
......
......@@ -25,15 +25,27 @@ describe('query_time', () => {
describe('absolute', () => {
it ('should pass through absolute dates', () => {
expect(absolute("2009-08-07T06:05:04Z").format("YYYY-MM-DD HH:mm:ss")).toBe("2009-08-07 06:05:04");
expect(
absolute("2009-08-07T06:05:04Z").format("YYYY-MM-DD HH:mm:ss")
).toBe(
moment("2009-08-07 06:05:04Z").format("YYYY-MM-DD HH:mm:ss")
);
});
it ('should convert relative_datetime "current"', () => {
expect(absolute(["relative_datetime", "current"]).format("YYYY-MM-DD HH")).toBe(moment.utc().format("YYYY-MM-DD HH"));
expect(
absolute(["relative_datetime", "current"]).format("YYYY-MM-DD HH")
).toBe(
moment().format("YYYY-MM-DD HH")
);
});
it ('should convert relative_datetime -1 "month"', () => {
expect(absolute(["relative_datetime", -1, "month"]).format("YYYY-MM-DD HH")).toBe(moment.utc().subtract(1, "month").format("YYYY-MM-DD HH"));
expect(
absolute(["relative_datetime", -1, "month"]).format("YYYY-MM-DD HH")
).toBe(
moment().subtract(1, "month").format("YYYY-MM-DD HH")
);
});
});
......@@ -42,7 +54,7 @@ describe('query_time', () => {
it ('should handle "="', () => {
let [start, end] = computeFilterTimeRange(["=", 1, "2009-08-07"]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-07 00:00:00");
expect(end).toEqual(null);
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-07 23:59:59");
});
it ('should handle "<"', () => {
let [start, end] = computeFilterTimeRange(["<", 1, "2009-08-07"]);
......@@ -51,49 +63,49 @@ describe('query_time', () => {
});
it ('should handle ">"', () => {
let [start, end] = computeFilterTimeRange([">", 1, "2009-08-07"]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-07 00:00:00");
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-07 23:59:59");
expect(end.year()).toBeGreaterThan(10000);
});
it ('should handle "BETWEEN"', () => {
let [start, end] = computeFilterTimeRange(["BETWEEN", 1, "2009-08-07", "2009-08-09"]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-07 00:00:00");
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-09 00:00:00");
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual("2009-08-09 23:59:59");
});
})
describe('relative dates', () => {
it ('should handle "="', () => {
let [start, end] = computeFilterTimeRange(["=", 1, ["relative_datetime", "current"]]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().format("YYYY-MM-DD 00:00:00"));
expect(end).toEqual(null);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().format("YYYY-MM-DD 23:59:59"));
});
it ('should handle "<"', () => {
let [start, end] = computeFilterTimeRange(["<", 1, ["relative_datetime", "current"]]);
expect(start.year()).toBeLessThan(-10000);
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().format("YYYY-MM-DD 00:00:00"));
});
it ('should handle ">"', () => {
let [start, end] = computeFilterTimeRange([">", 1, ["relative_datetime", "current"]]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().format("YYYY-MM-DD 00:00:00"));
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().format("YYYY-MM-DD 23:59:59"));
expect(end.year()).toBeGreaterThan(10000);
});
it ('should handle "BETWEEN"', () => {
let [start, end] = computeFilterTimeRange(["BETWEEN", 1, ["relative_datetime", -1, "day"], ["relative_datetime", 1, "day"]]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().subtract(1, "day").format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().add(1, "day").format("YYYY-MM-DD 00:00:00"));
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().subtract(1, "day").format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().add(1, "day").format("YYYY-MM-DD 23:59:59"));
});
});
describe('TIME_INTERVAL', () => {
it ('should handle "Past x days"', () => {
let [start, end] = computeFilterTimeRange(["TIME_INTERVAL", 1, -7, "day"]);
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().subtract(8, "day").format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().subtract(1, "day").format("YYYY-MM-DD 00:00:00"));
expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().subtract(8, "day").format("YYYY-MM-DD 00:00:00"));
expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().subtract(1, "day").format("YYYY-MM-DD 23:59:59"));
});
// it ('should handle "last week"', () => {
// let [start, end] = computeFilterTimeRange(["TIME_INTERVAL", 1, "last", "week"]);
// expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().subtract(1, "week").format("YYYY-MM-DD 00:00:00"));
// expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment.utc().subtract(1, "day").format("YYYY-MM-DD 00:00:00"));
// expect(start.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().subtract(1, "week").startOf("week").format("YYYY-MM-DD 00:00:00"));
// expect(end.format("YYYY-MM-DD HH:mm:ss")).toEqual(moment().subtract(1, "week").endOf("week")..format("YYYY-MM-DD 23:59:59"));
// });
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment