Skip to content

Commit

Permalink
chore: add postgres dbt model to dbt tests (#2703)
Browse files Browse the repository at this point in the history
I know there are issues here, but sharing for early review:
- Changes the expected test outcome for the view materialization in the
existing dbt test from fail to pass
- Adds a test confirming that dbt will work with an external postgres
database. This uses the actual external demo postgres instance that we
mention in our docs, so is probably brittle. Ideally, we would spin
something up (Docker?) for the purpose of the test.
- Overall, this could also probably use some refactoring, but I _think_
that that will take some pytest magic which may make the tests less
legible.

---------

Co-authored-by: tycho garen <[email protected]>
  • Loading branch information
talagluck and tychoish authored Feb 28, 2024
1 parent 0eabfad commit d49fbd1
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{ config(materialized='view') }}

select *
from {{ source('my_pg', 'borough_lookup')}}
10 changes: 8 additions & 2 deletions tests/fixtures/dbt_project/models/glaredb_data/sources.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
version: 2

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

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

select *
from {{ source('public', 'dbt_test')}}
from {{ source('basic', 'dbt_test')}}
62 changes: 52 additions & 10 deletions tests/test_dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,32 @@ def dbt_project_path() -> pathlib.Path:
"model_name,run_success,query_result",
[
("table_materialization", True, 10),
pytest.param("view_materialization", True, 10, marks=pytest.mark.xfail),
("view_materialization", True, 10),
],
)
def test_dbt_glaredb(
glaredb_connection: psycopg2.extensions.connection,
dbt_project_path,
model_name,
run_success,
query_result,
dbt_project_path: pathlib.Path,
model_name: str,
run_success: bool,
query_result: int,
):
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)"
)
curr.execute("SELECT * FROM public.dbt_test")
a = curr.fetchall()

with tests.tools.env("DBT_USER", glaredb_connection.info.user):
res: dbtRunnerResult = dbtRunner().invoke(
[
"run",
"--project-dir",
dbt_project_directory,
dbt_project_path,
"--profiles-dir",
dbt_profiles_directory,
dbt_project_path,
"-m",
model_name,
]
Expand All @@ -58,3 +57,46 @@ def test_dbt_glaredb(
result: list = curr.fetchone()[0]

assert result == query_result


def test_dbt_glaredb_external_postgres(
glaredb_connection: psycopg2.extensions.connection,
dbt_project_path: pathlib.Path,
):
model_name = "postgres_datasource_view_materialization"

with glaredb_connection.cursor() as curr:
curr.execute(
"""
CREATE EXTERNAL DATABASE my_pg
FROM postgres
OPTIONS (
host = 'pg.demo.glaredb.com',
port = '5432',
user = 'demo',
password = 'demo',
database = 'postgres',
);
"""
)

with tests.tools.env("DBT_USER", glaredb_connection.info.user):
res: dbtRunnerResult = dbtRunner().invoke(
[
"run",
"--project-dir",
dbt_project_path,
"--profiles-dir",
dbt_project_path,
"-m",
model_name,
]
)

assert res.success is True

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

assert result == 5
5 changes: 4 additions & 1 deletion tests/test_lance.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_copy_to_round_trip(
res = curr.fetchone()
assert res[0] == 10


def test_copy_to_round_trip(
glaredb_connection: psycopg2.extensions.connection,
tmp_path_factory: pytest.TempPathFactory,
Expand All @@ -89,7 +90,9 @@ def test_copy_to_round_trip(
assert curr.fetchone()[0] == 10

curr.execute(f"COPY lance_test TO '{output_path}' FORMAT lance")
curr.execute(f"create external table lance_import from lance options (location '{output_path}')")
curr.execute(
f"create external table lance_import from lance options (location '{output_path}')"
)
curr.execute("alter table lance_import set access_mode to read_write")

for i in range(10):
Expand Down

0 comments on commit d49fbd1

Please sign in to comment.