Skip to content
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

[Onweek] PBT for get_time #119157

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@
"eslint-plugin-react-perf": "^3.3.0",
"eslint-traverse": "^1.0.0",
"expose-loader": "^0.7.5",
"fast-check": "^2.19.0",
"faker": "^5.1.0",
"fancy-log": "^1.3.2",
"fast-glob": "2.2.7",
Expand Down
141 changes: 141 additions & 0 deletions src/plugins/data/common/query/timefilter/get_time.pb.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { IndexPattern, TimeRange } from 'src/plugins/data/public';
import fc from 'fast-check';

fc.configureGlobal({
numRuns: 1000,
});

import { getRelativeTime, getTime } from './get_time';

const option = <T>(arb: fc.Arbitrary<T>) => fc.option(arb, { nil: undefined });

function arbitraryDataView(name?: string): fc.Arbitrary<IndexPattern> {
return fc.record({
id: fc.string({ minLength: 1 }),
title: fc.string(),
fieldFormatMap: fc.object(),
allowNoIndex: fc.boolean(),
fields: fc.array(fc.record({ name: name ? fc.constant(name) : fc.string() }), {
minLength: name ? 1 : undefined,
}),
}) as unknown as fc.Arbitrary<IndexPattern>;
}

function arbitraryRelativeTime(): fc.Arbitrary<string> {
return fc
.nat({ max: 100 })
.chain((n) => fc.constantFrom(`now-${n}y`, `now-${n}m`, `now-${n}d`, `now-${n}w`, `now-${n}h`));
}

const arbitraryISODateString = (): fc.Arbitrary<string> =>
fc.date({ min: new Date(0) }).map((date) => date.toISOString());

function arbitraryTimeRange(): fc.Arbitrary<TimeRange> {
return fc.record<TimeRange>({
from: fc.date({ min: new Date(0) }).map((date) => date.toISOString()),
to: fc.date({ min: new Date(0) }).map((date) => date.toISOString()),
mode: fc.constantFrom('absolute', 'relative', undefined),
});
}

function arbitraryMixedTimeRange(): fc.Arbitrary<TimeRange> {
return fc.record<TimeRange>({
from: fc.oneof(arbitraryRelativeTime(), arbitraryISODateString()),
to: fc.oneof(arbitraryRelativeTime(), arbitraryISODateString()),
mode: fc.constantFrom('absolute', 'relative', undefined),
});
}

describe('Time range filters', () => {
describe('getTime', () => {
it('does not throw for expected inputs', () => {
fc.assert(
fc.property(
arbitraryDataView(),
arbitraryTimeRange(),
option(fc.record({ forceNow: option(fc.date()), fieldName: option(fc.string()) })),
(indexPattern, timeRange, options) => {
expect(() => getTime(indexPattern, timeRange, options)).not.toThrow();
}
),
{ numRuns: 1000 }
);
});

it('returns a time range filter with the provided name', () => {
fc.assert(
fc.property(
arbitraryTimeRange(),
fc
.string({ minLength: 1 })
.chain((name) =>
fc.tuple(
arbitraryDataView(name),
fc.record({ forceNow: option(fc.date()), fieldName: fc.constant(name) })
)
),
(timeRange, [indexPattern, options]) => {
expect(getTime(indexPattern, timeRange, options)?.query.range).toEqual({
[options.fieldName]: {
lte: expect.any(String),
gte: expect.any(String),
format: expect.any(String),
},
});
}
),
{ numRuns: 1000 }
);
});
});

describe('getRelativeTime', () => {
it('does not throw for expected inputs', () => {
fc.assert(
fc.property(
arbitraryDataView(),
arbitraryMixedTimeRange(),
option(fc.record({ forceNow: option(fc.date()), fieldName: option(fc.string()) })),
(indexPattern, timeRange, options) => {
expect(() => getRelativeTime(indexPattern, timeRange, options)).not.toThrow();
}
),
{ numRuns: 1000 }
);
});

it('returns a time range filter with the provided name', () => {
fc.assert(
fc.property(
arbitraryMixedTimeRange(),
fc
.string({ minLength: 1 })
.chain((name) =>
fc.tuple(
arbitraryDataView(name),
fc.record({ forceNow: option(fc.date()), fieldName: fc.constant(name) })
)
),
(timeRange, [indexPattern, options]) => {
expect(getRelativeTime(indexPattern, timeRange, options)?.query.range).toEqual({
[options.fieldName]: {
lte: expect.any(String),
gte: expect.any(String),
format: expect.any(String),
},
});
}
),
{ numRuns: 1000 }
);
});
});
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14498,6 +14498,13 @@ fancy-log@^1.3.2:
color-support "^1.1.3"
time-stamp "^1.0.0"

fast-check@^2.19.0:
version "2.19.0"
resolved "https://registry.yarnpkg.com/fast-check/-/fast-check-2.19.0.tgz#e87666450e417cd163a564bd546ee763d27c0f19"
integrity sha512-qY4Rc0Nljl2aJx2qgbK3o6wPBjL9QvhKdD/VqJgaKd5ewn8l4ViqgDpUHJq/JkHTBnFKomYYvkFkOYGDVTT8bw==
dependencies:
pure-rand "^5.0.0"

fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3, fast-deep-equal@~3.1.3:
version "3.1.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
Expand Down Expand Up @@ -23558,6 +23565,11 @@ puppeteer@^5.3.1:
unbzip2-stream "^1.3.3"
ws "^7.2.3"

pure-rand@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/pure-rand/-/pure-rand-5.0.0.tgz#87f5bdabeadbd8904e316913a5c0b8caac517b37"
integrity sha512-lD2/y78q+7HqBx2SaT6OT4UcwtvXNRfEpzYEzl0EQ+9gZq2Qi3fa0HDnYPeqQwhlHJFBUhT7AO3mLU3+8bynHA==

q@^1.1.2, q@^1.5.1:
version "1.5.1"
resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7"
Expand Down