Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: parametrize dbt test, add test case #2673

Merged
merged 12 commits into from
Feb 21, 2024
6 changes: 2 additions & 4 deletions tests/fixtures/dbt_project/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ clean-targets: # directories to be removed by `dbt clean`
# directory as views. These settings can be overridden in the individual model
# files using the `{{ config(...) }}` macro.

# Config indicated by + and applies to all files under models/example/
models:
dbt_test:
# Config indicated by + and applies to all files under models/example/
glaredb_data:
+materialized: view
glaredb_data: {}

This file was deleted.

2 changes: 1 addition & 1 deletion tests/fixtures/dbt_project/models/glaredb_data/sources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ version: 2

sources:
- name: public
database: glaredb
database: default
tables:
- name: dbt_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ config(materialized='table') }}

select *
from {{ source('public', 'dbt_test')}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ config(materialized='view') }}

select *
from {{ source('public', 'dbt_test')}}
2 changes: 1 addition & 1 deletion tests/fixtures/dbt_project/profiles.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
glaredb_dbt_test:
outputs:
test_target:
dbname: glaredb
dbname: default
host: 127.0.0.1
pass: ""
port: 5432
Expand Down
364 changes: 231 additions & 133 deletions tests/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pyarrow = "^14.0.2"
pylance = "^0.9.6"
ruff = "0.1.14"
dbt-core = "^1.7.7"
dbt-postgres = "^1.7.7"

[build-system]
requires = ["poetry-core"]
Expand Down
41 changes: 28 additions & 13 deletions tests/tests/test_dbt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pathlib
import tests

import psycopg2.extensions
import pytest
import os
Expand All @@ -6,28 +9,41 @@

from dbt.cli.main import dbtRunner, dbtRunnerResult

@pytest.fixture
def dbt_project_path() -> pathlib.Path:
return tests.PKG_DIRECTORY.joinpath("tests", "fixtures", "dbt_project")

@pytest.mark.parametrize("model_name,run_success,query_result",
[
("table_materialization", True, 10),
pytest.param("view_materialization", True, 10, marks=pytest.mark.xfail),
tychoish marked this conversation as resolved.
Show resolved Hide resolved
]
)
def test_dbt_glaredb(
glaredb_connection: psycopg2.extensions.connection,
dbt_project_path,
model_name,
run_success,
query_result
):
print("LISTDIR", os.listdir('.'))
dbt: dbtRunner = dbtRunner()

os.environ["DBT_USER"] = glaredb_connection.info.user

model_name: str = "glaredb_model" # TODO
project_directory: str = "../fixtures/dbt_project/" # TODO
dbt_profiles_directory: str = "../fixtures/dbt_project/" # TODO
dbt_project_directory: pathlib.Path = dbt_project_path
dbt_profiles_directory: pathlib.Path = dbt_project_path

with glaredb_connection.cursor() as curr:
curr.execute("create table dbt_test (amount int)")
curr.execute(
"INSERT INTO dbt_test (amount) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9)"
)

cli_args: list = [
"run",
"--project-dir",
project_directory,
dbt_project_directory,
"--profiles-dir",
dbt_profiles_directory,
"-m",
Expand All @@ -36,11 +52,10 @@ def test_dbt_glaredb(
#
res: dbtRunnerResult = dbt.invoke(cli_args)

# The below will currently fail. res contains the error message, but that message can be in different places based
# on where the failure is. Currently, it is under the top level `res.exception` key.
# assert res.success is True
#
# with glaredb_connection.cursor() as curr:
# query_result: list = curr.execute(f"select * from {model_name}").fetchall()
#
# assert len(query_result) == 10
assert res.success is run_success

with glaredb_connection.cursor() as curr:
curr.execute(f"select count(*) from {model_name}")
result: list = curr.fetchone()[0]

assert result == query_result
Loading