-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…899) * changelog * add boundary test to better understand get_columns method * add call to redshift get_columns behind a behavior flag * add tests showing different behavior when the flag is turned off and on
- Loading branch information
1 parent
d189acb
commit 04bd2c0
Showing
9 changed files
with
205 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Remove `pg_catalog` from metadata queries | ||
time: 2024-08-26T12:39:54.481505-04:00 | ||
custom: | ||
Author: mikealfare, jiezhen-chen | ||
Issue: "555" |
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
Empty file.
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,28 @@ | ||
from datetime import datetime | ||
import os | ||
import random | ||
|
||
import pytest | ||
import redshift_connector | ||
|
||
|
||
@pytest.fixture | ||
def connection() -> redshift_connector.Connection: | ||
return redshift_connector.connect( | ||
user=os.getenv("REDSHIFT_TEST_USER"), | ||
password=os.getenv("REDSHIFT_TEST_PASS"), | ||
host=os.getenv("REDSHIFT_TEST_HOST"), | ||
port=int(os.getenv("REDSHIFT_TEST_PORT")), | ||
database=os.getenv("REDSHIFT_TEST_DBNAME"), | ||
region=os.getenv("REDSHIFT_TEST_REGION"), | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def schema_name(request) -> str: | ||
runtime = datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0) | ||
runtime_s = int(runtime.total_seconds()) | ||
runtime_ms = runtime.microseconds | ||
random_int = random.randint(0, 9999) | ||
file_name = request.module.__name__.split(".")[-1] | ||
return f"test_{runtime_s}{runtime_ms}{random_int:04}_{file_name}" |
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,43 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def schema(connection, schema_name) -> str: | ||
with connection.cursor() as cursor: | ||
cursor.execute(f"CREATE SCHEMA IF NOT EXISTS {schema_name}") | ||
yield schema_name | ||
with connection.cursor() as cursor: | ||
cursor.execute(f"DROP SCHEMA IF EXISTS {schema_name} CASCADE") | ||
|
||
|
||
def test_columns_in_relation(connection, schema): | ||
table = "cross_db" | ||
with connection.cursor() as cursor: | ||
cursor.execute(f"CREATE TABLE {schema}.{table} as select 3.14 as id") | ||
columns = cursor.get_columns( | ||
schema_pattern=schema, | ||
tablename_pattern=table, | ||
) | ||
|
||
assert len(columns) == 1 | ||
column = columns[0] | ||
|
||
( | ||
database_name, | ||
schema_name, | ||
table_name, | ||
column_name, | ||
type_code, | ||
type_name, | ||
precision, | ||
_, | ||
scale, | ||
*_, | ||
) = column | ||
assert schema_name == schema | ||
assert table_name == table | ||
assert column_name == "id" | ||
assert type_code == 2 | ||
assert type_name == "numeric" | ||
assert precision == 3 | ||
assert scale == 2 |
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 |
---|---|---|
@@ -1 +1 @@ | ||
# provides namespacing for test discovery | ||
# supports namespacing during test discovery |
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,59 @@ | ||
from dbt.adapters.base import Column | ||
from dbt.tests.util import run_dbt | ||
import pytest | ||
|
||
from dbt.adapters.redshift import RedshiftRelation | ||
|
||
|
||
class ColumnsInRelation: | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"my_model.sql": "select 1.23 as my_num, 'a' as my_char"} | ||
|
||
@pytest.fixture(scope="class", autouse=True) | ||
def setup(self, project): | ||
run_dbt(["run"]) | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_columns(self): | ||
return [] | ||
|
||
def test_columns_in_relation(self, project, expected_columns): | ||
my_relation = RedshiftRelation.create( | ||
database=project.database, | ||
schema=project.test_schema, | ||
identifier="my_model", | ||
type=RedshiftRelation.View, | ||
) | ||
with project.adapter.connection_named("_test"): | ||
actual_columns = project.adapter.get_columns_in_relation(my_relation) | ||
assert actual_columns == expected_columns | ||
|
||
|
||
class TestColumnsInRelationBehaviorFlagOff(ColumnsInRelation): | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"flags": {}} | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_columns(self): | ||
# the SDK query returns "varchar" whereas our custom query returns "character varying" | ||
return [ | ||
Column(column="my_num", dtype="numeric", numeric_precision=3, numeric_scale=2), | ||
Column(column="my_char", dtype="character varying", char_size=1), | ||
] | ||
|
||
|
||
class TestColumnsInRelationBehaviorFlagOn(ColumnsInRelation): | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"flags": {"restrict_direct_pg_catalog_access": True}} | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_columns(self): | ||
# the SDK query returns "varchar" whereas our custom query returns "character varying" | ||
return [ | ||
Column(column="my_num", dtype="numeric", numeric_precision=3, numeric_scale=2), | ||
Column(column="my_char", dtype="varchar", char_size=1), | ||
] |