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: add postgres dbt model to dbt tests #2703

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')}}
63 changes: 53 additions & 10 deletions tests/test_dbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@ 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()
1 == 1

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 +58,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
Loading