Skip to content

Commit 77ac9b8

Browse files
authored
test: Add integration tests (#11)
* test: Add integration tests
1 parent 797d4ec commit 77ac9b8

File tree

13 files changed

+1782
-194
lines changed

13 files changed

+1782
-194
lines changed

Taskfile.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "3"
2+
3+
tasks:
4+
# There are a few problems with a standard test loop, such that we need to
5+
# completely blow away the python environment on each loop:
6+
# - Having the dbt-prql dependency as develop=True doesn't seem to copy
7+
# `zzz_dbt_prql.pth` with that name; instead using `dbt_prql.pth`. This then
8+
# doesn't load early enough. (Seems weird, might be worth confirming)
9+
# - Having the dbt-prql dependency as develop=False doesn't seem to update
10+
# when reinstalled.
11+
reset-test-env:
12+
dir: tests
13+
14+
cmds:
15+
- cmd: poetry env remove 3.9
16+
ignore_error: true
17+
- poetry env use 3.9
18+
- poetry install
19+
20+
test:
21+
dir: tests/dbt_test_project
22+
cmds:
23+
- dbt compile --profiles-dir=..

dbt_prql/patch.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
from jinja2.ext import Extension
2+
from jinja2.parser import Parser
23

34

45
class PrqlExtension(Extension):
56
tags = {"prql"}
67

7-
def parse(self, parser):
8+
def parse(self, parser: Parser):
9+
import logging
10+
811
from jinja2 import nodes
912
from jinja2.nodes import Const
1013

14+
logger = logging.getLogger(__name__)
15+
1116
line_number = next(parser.stream).lineno
12-
prql = parser.parse_statements(["name:endprql"], drop_needle=True)
17+
prql_jinja = parser.parse_statements(["name:endprql"], drop_needle=True)
18+
logger.info(f"Parsing statement:\n{prql_jinja}")
1319
return nodes.CallBlock(
14-
self.call_method("_to_sql", [Const("")]), [], [], prql
20+
self.call_method("_to_sql", [Const("")]), [], [], prql_jinja
1521
).set_lineno(line_number)
1622

1723
def _to_sql(self, args, caller):
24+
_ = args
25+
import logging
26+
1827
import prql_python
1928

29+
logger = logging.getLogger(__name__)
30+
2031
prql = caller()
32+
logger.info(f"Parsing PRQL:\n{prql}")
2133
sql = prql_python.to_sql(prql)
2234
output = f"""
2335
-- SQL created from PRQL. Original PRQL:
2436
{chr(10).join(f'-- {line}' for line in prql.splitlines())}
2537
2638
{sql}
2739
"""
40+
logger.info(f"Parsed into SQL:\n{sql}")
2841
return output
2942

3043

@@ -42,10 +55,10 @@ def patch_dbt_environment() -> None:
4255

4356
logger = logging.getLogger(__name__)
4457

45-
log_level = os.environ.get("DBT_PRQL_LOG_LEVEL")
46-
if log_level is not None and log_level != "":
58+
log_level = os.environ.get("DBT_PRQL_LOG_LEVEL", "")
59+
if log_level != "":
4760
logging.basicConfig()
48-
logger.setLevel(int(log_level))
61+
logger.setLevel(log_level)
4962
logger.warning(
5063
f"Setting PRQL log level to {log_level}. Please note that logging anything "
5164
"from this module can affect the reliability of some libraries which "
@@ -78,10 +91,10 @@ def with_prql_extension(*args, **kwargs):
7891
jinja.get_environment = env_with_prql
7992

8093
if os.environ.get("DBT_PRQL_TEST"):
81-
test_prql_parse()
94+
test_jinja_parse()
8295

8396

84-
def test_prql_parse() -> None:
97+
def test_jinja_parse() -> None:
8598
import logging
8699

87100
from dbt.clients.jinja import get_environment

poetry.lock

Lines changed: 112 additions & 185 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ python = ">=3.7.3"
1616
# We'll try and quite quickly on a new _.X._ release from dbt.
1717
# And we can unblock this when we have upstream tests https://github.com/prql/dbt-prql/issues/6
1818
dbt-core = ">=1.1.0,<1.3.0"
19-
prql_python = ">=0.2.0"
19+
prql_python = "0.2.6"
20+
21+
[tool.poetry.dev-dependencies]
22+
black = "*"
23+
flake8 = "*"
24+
mypy = "*"
2025

2126
[build-system]
2227
build-backend = "poetry.core.masonry.api"

tests/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.user.yml

tests/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# dbt-prql tests
2+
3+
These are some early integration tests. Currently they're run from the
4+
command-line with `task`:
5+
6+
```
7+
task test
8+
```
9+
10+
Some severe caveats:
11+
12+
- Because of <https://github.com/prql/prql/issues/918>, the postgres tests
13+
demonstrate that this doesn't currently work with postgres.
14+
- The tests may fail _after_ passing the compilation, depending on whether
15+
there's a valid Google Cloud auth setup. Unfortunately it doesn't seem
16+
possible to use standard dbt commands without querying a DB. So we may need to
17+
work with [dbt's test
18+
fixtures](https://github.com/dbt-labs/dbt-core/tree/main/tests); at first
19+
glance they don't seem well-described.

tests/dbt_test_project/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
target/
3+
dbt_modules/
4+
logs/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: "dbt_test_projct"
2+
config-version: 2
3+
# Unused
4+
version: "0.0.0"
5+
profile: prql-test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
version: 2
2+
3+
# Source template from https://docs.getdbt.com/docs/building-a-dbt-project/using-sources
4+
5+
sources:
6+
- name: salesforce
7+
tables:
8+
- name: in_process
9+
- name: team
10+
tables:
11+
- name: team_sales
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% prql %}
2+
from in_process = {{ source('salesforce', 'in_process') }}
3+
derive expected_sales = probability * value
4+
join {{ source('team', 'team_sales') }} [name]
5+
group name (
6+
aggregate (sum expected_sales)
7+
)
8+
{% endprql %}

0 commit comments

Comments
 (0)