-
Notifications
You must be signed in to change notification settings - Fork 186
[PPL] Add Doctest module #497
[PPL] Add Doctest module #497
Conversation
Your PR description looks pretty good. :) Could you copy it to a doc and add to this PR? Target folder is here: https://github.com/opendistro-for-elasticsearch/sql/tree/master/docs/dev. And I'm thinking probably you can remove concrete language, ex. PPL, since this would be same solution for SQL. |
doctest/test_docs.py
Outdated
def load_tests(loader, suite, ignore): | ||
tests = [] | ||
# docs with bash-based examples | ||
for fn in doctest_files('ppl/curl.rst'): # TODO: Add 'sql/explain.rst' after codebase migration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it require the develper change this file every time when they add different rst file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically, yes. That's because we need different setup, parser creation for docs containing different type of examples. sql
, ppl
or curl
Mixing sql and ppl code examples together in one doc doesn't work with doctest now, because we setup different client with different query language with CLI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added category.json
to help user setup doctest without touching the doctest core code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the change.
PPL Doctest
1. Overview
1.1 What is doctest?
Doctest is a way to test code by checking the correctness of embedded interactive examples in documentation
example explaining doctest module
example.txt
Running
doctest.testfile("example.txt")
then finds the error1.2 Why use doctest?
Doctest can make the code snippet in docs executable, and check its response as well.
As mentioned in the python doctest introduction, doctest has grown to have three primary uses:
1.3 How does it affect unit/integration testing suite?
Both doctest and unit/integration test are valuable. Use doctest for cases where the test is giving an example of usage that is actually useful as documentation. Generally, don't make these tests comprehensive, aiming solely for informative. Use doctest in reverse: not to test the code is correct based on doctest, but to check that documentation is correct based on the code.
For actually testing the code, the goal is to thoroughly test every case, rather than illustrate what it does by example. Doctests aren't meant to be a comprehensive testing solution - they're meant to ensure that simple interactive-prompt style examples in your documentation (including docstrings) don't get out of date.
1.4 Future Plan
Current SQL Documentation will need reconstruction in the future. Ideally, both SQL and PPL doctest will integrate/migrate to a separate Doctest module in the new architecture.
2. Design
2.1 Workflow
2.2 ES Test Instance
2.2.1 Testing framework
We have two options here
./gradlew run
The reason we are not using ES test framework, is due to the difficulty of integrating Python code to a Java based framework, considering we are using python built-in module
doctest
for implementation2.2.2 Gradle
Create new module/packdage
doctest
under currentopendistro-sql
, and integrate to gradle managementSet up gradle build script, which enables doctest by
./gradlew doctest
Gradle tasks:
./gradlew run
Add doctest dependency to project gradle build
2.2.3 Project Structure
bootstrap.sh
set up virtual environment for python module2.3 Parsers
Doctest is relying on the console/command line to run code examples in documentation. So we need two parsers here.
2.3.1 CLI parser
Reference: CrateDB Implementation
crash
to run doctests. https://github.com/crate/crashSimilar to CarateDB using it’s CLI “crash”, we can make use of our own SQL-CLI
To support PPL, we need to add PPL support to SQL-CLI. Since PPL and SQL expose similar http endpoint for query and share similar response format. The update won’t be much of work.
The code example in a doc using
CLI
should be like this2.3.2 bash parser
curl
commandThe code example in a doc using
bash
should be like this2.3 Test Data
Use elasticsearch python library to create connection to ES instance. It can load test data into ES instance, and delete test index after testing.
Setup:
bulk
APITearDown:
delete(index="_all")
2.3 Test Report
2.3.1 Print results
Use python faulthandler from script to print results
https://docs.python.org/3/library/faulthandler.html
2.3.2 generate report