-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pytest option for dbt project dir * Use dbt project dir when initializing args * Add dbt project directory pytest variable * Add dbt project for testing * Add dbt profiles dir to tox * Move logic to plugin module * Update change log * Add test for macro without return * Remove spark context manager * Remove redundant future annotations import
- Loading branch information
1 parent
7b9157b
commit 722775a
Showing
10 changed files
with
111 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""The entrypoint for the plugin.""" | ||
|
||
import os | ||
|
||
from _pytest.config.argparsing import Parser | ||
|
||
from .fixtures import adapter, config, macro_generator, manifest | ||
|
||
__all__ = ( | ||
"adapter", | ||
"config", | ||
"macro_generator", | ||
"manifest", | ||
) | ||
|
||
|
||
def pytest_addoption(parser: Parser) -> None: | ||
""" | ||
Add pytest option. | ||
Parameters | ||
---------- | ||
parser : Parser | ||
The parser. | ||
""" | ||
parser.addoption( | ||
"--dbt-project-dir", | ||
help="The dbt project directory.", | ||
type="string", | ||
default=os.getcwd(), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: dbt_project | ||
profile: dbt_project | ||
version: '0.3.0' | ||
config-version: 2 | ||
require-dbt-version: [">=1.0.0", "<2.0.0"] | ||
macro-paths: ["macros"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% macro fetch_single_statement(statement, default_value="") %} | ||
|
||
{% set results = run_query(statement) %} | ||
|
||
{% if execute %} | ||
{% set value = results.columns[0].values()[0] %} | ||
{% else %} | ||
{% set value = default_value %} | ||
{% endif %} | ||
|
||
{{ return( value ) }} | ||
|
||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% macro to_cents(column_name) %} | ||
{{ column_name }} * 100 | ||
{% endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
dbt_project: | ||
target: test | ||
outputs: | ||
test: | ||
type: spark | ||
method: session | ||
schema: test | ||
host: NA # not used, but required by `dbt-core` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import pytest | ||
from dbt.clients.jinja import MacroGenerator | ||
from pyspark.sql import SparkSession | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"macro_generator", | ||
["macro.dbt_project.fetch_single_statement"], | ||
indirect=True, | ||
) | ||
def test_create_table( | ||
spark_session: SparkSession, macro_generator: MacroGenerator | ||
) -> None: | ||
out = macro_generator("SELECT 1") | ||
assert out == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import pytest | ||
from dbt.clients.jinja import MacroGenerator | ||
from pyspark.sql import SparkSession | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"macro_generator", | ||
["macro.dbt_project.to_cents"], | ||
indirect=True, | ||
) | ||
def test_create_table( | ||
spark_session: SparkSession, macro_generator: MacroGenerator | ||
) -> None: | ||
expected = spark_session.createDataFrame([{"cents": 1000}]) | ||
to_cents = macro_generator("price") | ||
out = spark_session.sql( | ||
"with data AS (SELECT 10 AS price) " | ||
f"SELECT cast({to_cents} AS bigint) AS cents FROM data" | ||
) | ||
assert out.collect() == expected.collect() |