|
23 | 23 | my_model_incremental_wrong_name_sql,
|
24 | 24 | my_model_with_nulls_sql,
|
25 | 25 | my_model_incremental_with_nulls_sql,
|
| 26 | + my_model_with_quoted_column_name_sql, |
26 | 27 | model_schema_yml,
|
27 | 28 | model_fk_constraint_schema_yml,
|
28 | 29 | constrained_model_schema_yml,
|
| 30 | + model_quoted_column_schema_yml, |
29 | 31 | foreign_key_model_sql,
|
30 | 32 | my_model_wrong_order_depends_on_fk_sql,
|
31 | 33 | my_model_incremental_wrong_order_depends_on_fk_sql,
|
@@ -165,11 +167,12 @@ def test__constraints_correct_column_data_types(self, project, data_types):
|
165 | 167 |
|
166 | 168 |
|
167 | 169 | def _normalize_whitespace(input: str) -> str:
|
168 |
| - return re.sub(r"\s+", " ", input).lower().strip() |
| 170 | + subbed = re.sub(r"\s+", " ", input) |
| 171 | + return re.sub(r"\s?([\(\),])\s?", r"\1", subbed).lower().strip() |
169 | 172 |
|
170 | 173 |
|
171 | 174 | def _find_and_replace(sql, find, replace):
|
172 |
| - sql_tokens = sql.split(" ") |
| 175 | + sql_tokens = sql.split() |
173 | 176 | for idx in [n for n, x in enumerate(sql_tokens) if find in x]:
|
174 | 177 | sql_tokens[idx] = replace
|
175 | 178 | return " ".join(sql_tokens)
|
@@ -235,17 +238,12 @@ def test__constraints_ddl(self, project, expected_sql):
|
235 | 238 | # the name is not what we're testing here anyways and varies based on materialization
|
236 | 239 | # TODO: consider refactoring this to introspect logs instead
|
237 | 240 | generated_sql = read_file("target", "run", "test", "models", "my_model.sql")
|
238 |
| - generated_sql_modified = _normalize_whitespace(generated_sql) |
239 |
| - generated_sql_generic = _find_and_replace( |
240 |
| - generated_sql_modified, "my_model", "<model_identifier>" |
241 |
| - ) |
| 241 | + generated_sql_generic = _find_and_replace(generated_sql, "my_model", "<model_identifier>") |
242 | 242 | generated_sql_generic = _find_and_replace(
|
243 | 243 | generated_sql_generic, "foreign_key_model", "<foreign_key_model_identifier>"
|
244 | 244 | )
|
245 | 245 |
|
246 |
| - expected_sql_check = _normalize_whitespace(expected_sql) |
247 |
| - |
248 |
| - assert expected_sql_check == generated_sql_generic |
| 246 | + assert _normalize_whitespace(expected_sql) == _normalize_whitespace(generated_sql_generic) |
249 | 247 |
|
250 | 248 |
|
251 | 249 | class BaseConstraintsRollback:
|
@@ -485,15 +483,50 @@ def test__model_constraints_ddl(self, project, expected_sql):
|
485 | 483 | # assert at least my_model was run - additional upstreams may or may not be provided to the test setup via models fixture
|
486 | 484 | assert len(results) >= 1
|
487 | 485 | generated_sql = read_file("target", "run", "test", "models", "my_model.sql")
|
488 |
| - generated_sql_modified = _normalize_whitespace(generated_sql) |
489 |
| - generated_sql_generic = _find_and_replace( |
490 |
| - generated_sql_modified, "my_model", "<model_identifier>" |
491 |
| - ) |
| 486 | + |
| 487 | + generated_sql_generic = _find_and_replace(generated_sql, "my_model", "<model_identifier>") |
492 | 488 | generated_sql_generic = _find_and_replace(
|
493 | 489 | generated_sql_generic, "foreign_key_model", "<foreign_key_model_identifier>"
|
494 | 490 | )
|
495 |
| - assert _normalize_whitespace(expected_sql) == generated_sql_generic |
| 491 | + |
| 492 | + assert _normalize_whitespace(expected_sql) == _normalize_whitespace(generated_sql_generic) |
496 | 493 |
|
497 | 494 |
|
498 | 495 | class TestModelConstraintsRuntimeEnforcement(BaseModelConstraintsRuntimeEnforcement):
|
499 | 496 | pass
|
| 497 | + |
| 498 | + |
| 499 | +class BaseConstraintQuotedColumn(BaseConstraintsRuntimeDdlEnforcement): |
| 500 | + @pytest.fixture(scope="class") |
| 501 | + def models(self): |
| 502 | + return { |
| 503 | + "my_model.sql": my_model_with_quoted_column_name_sql, |
| 504 | + "constraints_schema.yml": model_quoted_column_schema_yml, |
| 505 | + } |
| 506 | + |
| 507 | + @pytest.fixture(scope="class") |
| 508 | + def expected_sql(self): |
| 509 | + return """ |
| 510 | +create table <model_identifier> ( |
| 511 | + id integer not null, |
| 512 | + "from" text not null, |
| 513 | + date_day text, |
| 514 | + check (("from" = 'blue')) |
| 515 | +) ; |
| 516 | +insert into <model_identifier> ( |
| 517 | + id, "from", date_day |
| 518 | +) |
| 519 | +( |
| 520 | + select id, "from", date_day |
| 521 | + from ( |
| 522 | + select |
| 523 | + 'blue' as "from", |
| 524 | + 1 as id, |
| 525 | + '2019-01-01' as date_day |
| 526 | + ) as model_subq |
| 527 | +); |
| 528 | +""" |
| 529 | + |
| 530 | + |
| 531 | +class TestConstraintQuotedColumn(BaseConstraintQuotedColumn): |
| 532 | + pass |
0 commit comments