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

add tests for rendering foreign key constraints #698

Merged
merged 6 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230511-143217.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: test foreign key constraint rendering
time: 2023-05-11T14:32:17.364819-04:00
custom:
Author: michelleark
Issue: "7512"
31 changes: 22 additions & 9 deletions tests/functional/adapter/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@
my_model_incremental_wrong_name_sql,
model_schema_yml,
constrained_model_schema_yml,
model_fk_constraint_schema_yml,
my_model_wrong_order_depends_on_fk_sql,
foreign_key_model_sql,
my_model_incremental_wrong_order_depends_on_fk_sql,
)

_expected_sql_bigquery = """
create or replace table <model_identifier> (
id integer not null primary key not enforced,
id integer not null primary key not enforced references <foreign_key_model_identifier> (id) not enforced,
color string,
date_day string
)
Expand All @@ -34,6 +38,7 @@
color,
date_day from
(
-- depends_on: <foreign_key_model_identifier>
select 'blue' as color,
1 as id,
'2019-01-01' as date_day
Expand All @@ -45,6 +50,8 @@
# - does not support a data type named 'text' (TODO handle this via type translation/aliasing!)
constraints_yml = model_schema_yml.replace("text", "string")
model_constraints_yml = constrained_model_schema_yml.replace("text", "string")
model_fk_constraint_schema_yml = model_fk_constraint_schema_yml.replace("text", "string")
constrained_model_schema_yml = constrained_model_schema_yml.replace("text", "string")


class BigQueryColumnEqualSetup:
Expand Down Expand Up @@ -117,8 +124,9 @@ class TestBigQueryTableConstraintsRuntimeDdlEnforcement(BaseConstraintsRuntimeDd
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_wrong_order_sql,
"constraints_schema.yml": constraints_yml,
"my_model.sql": my_model_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": model_fk_constraint_schema_yml,
}

@pytest.fixture(scope="class")
Expand All @@ -145,8 +153,9 @@ class TestBigQueryIncrementalConstraintsRuntimeDdlEnforcement(
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_incremental_wrong_order_sql,
"constraints_schema.yml": constraints_yml,
"my_model.sql": my_model_incremental_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": model_fk_constraint_schema_yml,
}

@pytest.fixture(scope="class")
Expand All @@ -171,8 +180,9 @@ class TestBigQueryModelConstraintsRuntimeEnforcement(BaseModelConstraintsRuntime
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_incremental_model_sql,
"constraints_schema.yml": model_constraints_yml,
"my_model.sql": my_model_wrong_order_depends_on_fk_sql,
"foreign_key_model.sql": foreign_key_model_sql,
"constraints_schema.yml": constrained_model_schema_yml,
}

@pytest.fixture(scope="class")
Expand All @@ -182,16 +192,19 @@ def expected_sql(self):
id integer not null,
color string,
date_day string,
primary key (id) not enforced
primary key (id) not enforced,
foreign key (id) references <foreign_key_model_identifier> (id) not enforced
)
OPTIONS()
as (
select id,
color,
date_day from
(
select 1 as id,
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day
) as model_subq
);
Expand Down