Skip to content

Commit

Permalink
[ES|QL] generate function validation tests (#183343)
Browse files Browse the repository at this point in the history
## Summary

Close #182390


### To test

You can test all aspects of the script by first making some changes to
`packages/kbn-esql-validation-autocomplete/src/validation/validation.test.ts`
- delete several tests from an existing function describe block ("block
1"—except don't choose "date_diff" since it has a bunch of custom tests)
- delete another function describe block completely ("block 2")
- change the expected result of several of the tests in a third function
describe block ("block 3")

Then, run `yarn maketests` from within
`packages/kbn-esql-validation-autocomplete`

**Expected result**
- Block 1 should have the deleted tests restored
- Block 2 should be restored entirely (though it may be moved in the
tests file)
- Block 3 should be untouched

### Checklist

- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
drewdaemon and kibanamachine committed May 16, 2024
1 parent dab0208 commit c4ef9bd
Show file tree
Hide file tree
Showing 11 changed files with 11,235 additions and 7,626 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ const DEV_PATTERNS = [
'x-pack/performance/**/*',
'src/setup_node_env/index.js',
'src/cli/dev.js',
'packages/kbn-esql-validation-autocomplete/scripts/**/*',
];

/** Restricted imports with suggested alternatives */
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,7 @@
"q": "^1.5.1",
"raw-loader": "^3.1.0",
"react-test-renderer": "^17.0.2",
"recast": "^0.23.7",
"regenerate": "^1.4.0",
"resolve": "^1.22.0",
"rxjs-marbles": "^7.0.1",
Expand Down
23 changes: 12 additions & 11 deletions packages/kbn-esql-validation-autocomplete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ src
| code_actions // => the quick fixes service logic
| definitions // => static assets to define all components behaviour of a ES|QL query: commands, functions, etc...
| validation // => the validation logic
scripts // => scripts used to manage the validation engine code
```

### Basic usage
Expand Down Expand Up @@ -45,12 +47,12 @@ import { validateQuery } from '@kbn/esql-validation-autocomplete';

// define only the getSources callback
const myCallbacks = {
getSources: async () => [{name: 'index', hidden: false}],
getSources: async () => [{ name: 'index', hidden: false }],
};

// ignore errors that might be triggered by the lack of some callbacks (i.e. "Unknown columns", etc...)
const { errors, warnings } = await validateQuery(
"from index | stats 1 + avg(myColumn)",
'from index | stats 1 + avg(myColumn)',
getAstAndSyntaxErrors,
{ ignoreOnMissingCallbacks: true },
myCallbacks
Expand Down Expand Up @@ -161,12 +163,12 @@ For instance to show contextual information on Hover the `getAstContext` functio
import { getAstAndSyntaxErrors } from '@kbn/esql-ast';
import { getAstContext } from '@kbn/esql-validation-autocomplete';

const queryString = "from index2 | stats 1 + avg(myColumn)";
const offset = queryString.indexOf("avg");
const queryString = 'from index2 | stats 1 + avg(myColumn)';
const offset = queryString.indexOf('avg');

const astContext = getAstContext(queryString, getAstAndSyntaxErrors(queryString), offset);

if(astContext.type === "function"){
if (astContext.type === 'function') {
const fnNode = astContext.node;
const fnDefinition = getFunctionDefinition(fnNode.name);

Expand All @@ -175,7 +177,6 @@ if(astContext.type === "function"){
}
```


### How does it work

The general idea of this package is to provide all ES|QL features on top of a custom compact AST definition (all data structure types defined in `@kbn/esql-ast`) which is designed to be resilient to many grammar changes.
Expand Down Expand Up @@ -211,9 +212,9 @@ The most complex case is the `expression` as it can cover a moltitude of cases.
### Adding new commands/options/functions/erc...

To update the definitions:

1. open either approriate definition file within the `definitions` folder and add a new entry to the relative array
2. write new tests for validation and autocomplete
* if a new function is added tests are automatically generated fro both validation and autocomplete with some standard checks
* if a new function requires a new field types, make sure to add the new type to the initial part of the test file
* this will be automatically picked up by the test generator to produce new test cases
* if a new function requires a new type of test, make sure to write it manually
2. if you are adding a function, run `yarn maketests` to add a set of fundamental validation tests for the new definition. If any of the suggested tests are wrong, feel free to correct them by hand. If it seems like a general problem, open an issue with the details so that we can update the generator code.
3. write new tests for validation and autocomplete

- if a new function requires a new type of test, make sure to write it manually
15 changes: 9 additions & 6 deletions packages/kbn-esql-validation-autocomplete/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "@kbn/esql-validation-autocomplete",
"version": "1.0.0",
"private": true,
"license": "SSPL-1.0 OR Elastic License 2.0",
"sideEffects": false
}
"name": "@kbn/esql-validation-autocomplete",
"version": "1.0.0",
"private": true,
"license": "SSPL-1.0 OR Elastic License 2.0",
"sideEffects": false,
"scripts": {
"maketests": "ts-node --transpileOnly ./scripts/generate_function_validation_tests.ts"
}
}
Loading

0 comments on commit c4ef9bd

Please sign in to comment.