Skip to content

Commit

Permalink
🐛 fix test stability
Browse files Browse the repository at this point in the history
  • Loading branch information
dej611 committed Oct 17, 2022
1 parent 5eb26e0 commit 54c8fc8
Showing 1 changed file with 95 additions and 20 deletions.
115 changes: 95 additions & 20 deletions x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ describe('IndexPattern Data Source', () => {
describe('#toExpression', () => {
it('should generate an empty expression when no columns are selected', async () => {
const state = FormBasedDatasource.initialize();
expect(FormBasedDatasource.toExpression(state, 'first', indexPatterns)).toEqual(null);
expect(
FormBasedDatasource.toExpression(state, 'first', indexPatterns, 'testing-seed')
).toEqual(null);
});

it('should create a table when there is a formula without aggs', async () => {
Expand All @@ -338,7 +340,9 @@ describe('IndexPattern Data Source', () => {
},
},
};
expect(FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns)).toEqual({
expect(
FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns, 'testing-seed')
).toEqual({
chain: [
{
function: 'createTable',
Expand Down Expand Up @@ -385,8 +389,9 @@ describe('IndexPattern Data Source', () => {
},
};

expect(FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns))
.toMatchInlineSnapshot(`
expect(
FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns, 'testing-seed')
).toMatchInlineSnapshot(`
Object {
"chain": Array [
Object {
Expand Down Expand Up @@ -491,7 +496,7 @@ describe('IndexPattern Data Source', () => {
1,
],
"samplerSeed": Array [
453225886,
1889181588,
],
"timeFields": Array [
"timestamp",
Expand Down Expand Up @@ -566,7 +571,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.timeFields).toEqual(['timestamp', 'another_datefield']);
});

Expand Down Expand Up @@ -601,7 +611,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
expect((ast.chain[1].arguments.aggs[1] as Ast).chain[0].arguments.timeShift).toEqual(['1d']);
});

Expand Down Expand Up @@ -808,7 +823,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
const count = (ast.chain[1].arguments.aggs[1] as Ast).chain[0];
const sum = (ast.chain[1].arguments.aggs[2] as Ast).chain[0];
const average = (ast.chain[1].arguments.aggs[3] as Ast).chain[0];
Expand Down Expand Up @@ -872,7 +892,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.aggs[0]).toMatchInlineSnapshot(`
Object {
"chain": Array [
Expand Down Expand Up @@ -996,7 +1021,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
const timeScaleCalls = ast.chain.filter((fn) => fn.function === 'lens_time_scale');
const formatCalls = ast.chain.filter((fn) => fn.function === 'lens_format_column');
expect(timeScaleCalls).toHaveLength(1);
Expand Down Expand Up @@ -1061,7 +1091,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
const filteredMetricAgg = (ast.chain[1].arguments.aggs[0] as Ast).chain[0].arguments;
const metricAgg = (filteredMetricAgg.customMetric[0] as Ast).chain[0].arguments;
const bucketAgg = (filteredMetricAgg.customBucket[0] as Ast).chain[0].arguments;
Expand Down Expand Up @@ -1112,7 +1147,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
const formatIndex = ast.chain.findIndex((fn) => fn.function === 'lens_format_column');
const calculationIndex = ast.chain.findIndex((fn) => fn.function === 'moving_average');
expect(calculationIndex).toBeLessThan(formatIndex);
Expand Down Expand Up @@ -1160,7 +1200,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.metricsAtAllLevels).toEqual([false]);
expect(JSON.parse(ast.chain[2].arguments.idMap[0] as string)).toEqual({
'col-0-0': [expect.objectContaining({ id: 'bucket1' })],
Expand Down Expand Up @@ -1199,7 +1244,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
expect(ast.chain[1].arguments.timeFields).toEqual(['timestamp']);
expect(ast.chain[1].arguments.timeFields).not.toContain('timefield');
});
Expand Down Expand Up @@ -1256,7 +1306,7 @@ describe('IndexPattern Data Source', () => {

const optimizeMock = jest.spyOn(operationDefinitionMap.percentile, 'optimizeEsAggs');

FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns);
FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns, 'testing-seed');

expect(operationDefinitionMap.percentile.optimizeEsAggs).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -1324,7 +1374,12 @@ describe('IndexPattern Data Source', () => {
return { aggs: aggs.reverse(), esAggsIdMap };
});

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;

expect(operationDefinitionMap.percentile.optimizeEsAggs).toHaveBeenCalledTimes(1);

Expand Down Expand Up @@ -1388,7 +1443,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;

const idMap = JSON.parse(ast.chain[2].arguments.idMap as unknown as string);

Expand Down Expand Up @@ -1493,7 +1553,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
// @ts-expect-error we can't isolate just the reference type
expect(operationDefinitionMap.testReference.toExpression).toHaveBeenCalled();
expect(ast.chain[3]).toEqual('mock');
Expand Down Expand Up @@ -1526,7 +1591,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;

expect(JSON.parse(ast.chain[2].arguments.idMap[0] as string)).toEqual({
'col-0-0': [
Expand Down Expand Up @@ -1613,7 +1683,12 @@ describe('IndexPattern Data Source', () => {
},
};

const ast = FormBasedDatasource.toExpression(queryBaseState, 'first', indexPatterns) as Ast;
const ast = FormBasedDatasource.toExpression(
queryBaseState,
'first',
indexPatterns,
'testing-seed'
) as Ast;
const chainLength = ast.chain.length;
expect(ast.chain[chainLength - 2].arguments.name).toEqual(['math']);
expect(ast.chain[chainLength - 1].arguments.id).toEqual(['formula']);
Expand Down

0 comments on commit 54c8fc8

Please sign in to comment.