Skip to content

Commit

Permalink
[Backport] 698 to 1.5.latest (#765)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk authored Jun 9, 2023
1 parent 4f7497d commit 8183b1a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
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"
52 changes: 33 additions & 19 deletions tests/functional/adapter/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@
my_model_wrong_name_sql,
my_model_view_wrong_name_sql,
my_model_incremental_wrong_name_sql,
model_schema_yml, constrained_model_schema_yml,
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
)
OPTIONS()
as (
select id,
color,
date_day from
(
select 'blue' as color,
1 as id,
color,
date_day from
(
-- depends_on: <foreign_key_model_identifier>
select 'blue' as color,
1 as id,
'2019-01-01' as date_day
) as model_subq
);
Expand All @@ -44,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:
@pytest.fixture
Expand Down Expand Up @@ -116,8 +124,9 @@ class TestBigQueryTableConstraintsRuntimeDdlEnforcement(
@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 Down Expand Up @@ -145,8 +154,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 Down Expand Up @@ -174,8 +184,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 @@ -185,16 +196,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,
'blue' as color,
color,
date_day from
(
-- depends_on: <foreign_key_model_identifier>
select
'blue' as color,
1 as id,
'2019-01-01' as date_day
) as model_subq
);
Expand Down

0 comments on commit 8183b1a

Please sign in to comment.