Skip to content

Commit 877d925

Browse files
authored
tested and linted snippets (dlt-hub#559)
* add snippets prototype * add tested snippet * update lockfile * create snippet test workflow * disable dedenting * fix indentation * set and clean test storage on snippet execution * pr comments
1 parent a96db7a commit 877d925

File tree

19 files changed

+593
-313
lines changed

19 files changed

+593
-313
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
name: lint
3+
4+
on:
5+
pull_request:
6+
branches:
7+
- master
8+
- devel
9+
workflow_dispatch:
10+
11+
jobs:
12+
get_docs_changes:
13+
uses: ./.github/workflows/get_docs_changes.yml
14+
15+
run_lint:
16+
name: Runs linter and tests on docs snippets
17+
runs-on: ubuntu-latest
18+
19+
steps:
20+
21+
- name: Check out
22+
uses: actions/checkout@master
23+
24+
- name: Setup Python
25+
uses: actions/setup-python@v4
26+
with:
27+
python-version: "3.10.x"
28+
29+
- name: Install Poetry
30+
uses: snok/install-poetry@v1
31+
with:
32+
virtualenvs-create: true
33+
virtualenvs-in-project: true
34+
installer-parallel: true
35+
36+
- name: Load cached venv
37+
id: cached-poetry-dependencies
38+
uses: actions/cache@v3
39+
with:
40+
path: .venv
41+
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
42+
43+
- name: Install dependencies
44+
# if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
45+
run: poetry install --no-interaction --all-extras
46+
47+
# - name: Install self
48+
# run: poetry install --no-interaction
49+
50+
- name: Run linter and tests
51+
run: make test-and-lint-snippets
52+

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ dev: has-poetry
4646

4747
lint:
4848
./check-package.sh
49-
poetry run mypy --config-file mypy.ini dlt docs/examples
50-
poetry run flake8 --max-line-length=200 dlt docs/examples
49+
poetry run mypy --config-file mypy.ini dlt
50+
poetry run flake8 --max-line-length=200 dlt
5151
poetry run flake8 --max-line-length=200 tests --exclude tests/reflection/module_cases
5252
# $(MAKE) lint-security
5353

54+
test-and-lint-snippets:
55+
poetry run mypy --config-file mypy.ini docs/snippets docs/examples
56+
poetry run flake8 --max-line-length=200 docs/snippets docs/examples
57+
poetry run pytest docs/snippets
58+
5459
lint-security:
5560
poetry run bandit -r dlt/ -n 3 -l
5661

docs/snippets/__init__.py

Whitespace-only changes.

docs/snippets/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from tests.utils import patch_home_dir, autouse_test_storage, preserve_environ
2+
3+

docs/snippets/intro_snippet.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# @@@SNIPSTART intro_snippet
2+
import dlt
3+
import requests
4+
# Create a dlt pipeline that will load
5+
# chess player data to the DuckDB destination
6+
pipeline = dlt.pipeline(
7+
pipeline_name='chess_pipeline',
8+
destination='duckdb',
9+
dataset_name='player_data'
10+
)
11+
# Grab some player data from Chess.com API
12+
data = []
13+
for player in ['magnuscarlsen', 'rpragchess']:
14+
response = requests.get(f'https://api.chess.com/pub/player/{player}')
15+
response.raise_for_status()
16+
data.append(response.json())
17+
# Extract, normalize, and load the data
18+
info = pipeline.run(data, table_name='player')
19+
print(info)
20+
# @@@SNIPEND
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
from tests.pipeline.utils import assert_load_info
3+
from docs.snippets.utils import run_snippet
4+
5+
def test_intro_snippet() -> None:
6+
variables = run_snippet("intro_snippet")
7+
assert_load_info(variables["info"])

docs/snippets/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import Dict, Any
2+
3+
from dlt.common.utils import set_working_dir
4+
5+
from tests.utils import TEST_STORAGE_ROOT, test_storage
6+
7+
BASEPATH = "docs/snippets"
8+
9+
def run_snippet(filename: str) -> Dict[str, Any]:
10+
with set_working_dir(BASEPATH):
11+
code = open(f"{filename}.py", encoding="utf-8").read()
12+
variables: Dict[str, Any] = {}
13+
exec(code, variables)
14+
return variables

docs/website/docs/dlt-ecosystem/destinations/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ Pick one of our high quality destinations and load your data to a local database
1212

1313
Otherwise pick a destination below:
1414

15-
<DocCardList />
15+
<DocCardList />

docs/website/docs/dlt-ecosystem/transformations/dbt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ If you want to transform the data before loading, you can use Python. If you wan
8484
data after loading, you can use dbt or one of the following:
8585

8686
1. [`dlt` SQL client.](sql.md)
87-
1. [Pandas.](pandas.md)
87+
1. [Pandas.](pandas.md)

docs/website/docs/dlt-ecosystem/transformations/sql.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ If you want to transform the data before loading, you can use Python. If you wan
4444
data after loading, you can use SQL or one of the following:
4545

4646
1. [dbt](dbt.md) (recommended).
47-
1. [Pandas.](pandas.md)
47+
1. [Pandas.](pandas.md)

0 commit comments

Comments
 (0)