Skip to content

Commit

Permalink
add validation for empty string, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed May 2, 2024
1 parent 0c2e94d commit 683785d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
10 changes: 10 additions & 0 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ class UnparsedSourceDefinition(dbtClassMixin):
tags: List[str] = field(default_factory=list)
config: Dict[str, Any] = field(default_factory=dict)

@classmethod
def validate(cls, data):
super(UnparsedSourceDefinition, cls).validate(data)
if data.get("loaded_at_field", None) == "":
raise ValidationError("loaded_at_field cannot be an empty string.")

Check warning on line 306 in core/dbt/contracts/graph/unparsed.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/contracts/graph/unparsed.py#L306

Added line #L306 was not covered by tests

for table in data.get("tables", None):
if table.get("loaded_at_field", None) == "":
raise ValidationError("loaded_at_field cannot be an empty string.")

@property
def yaml_key(self) -> "str":
return "sources"
Expand Down
45 changes: 42 additions & 3 deletions tests/functional/sources/test_source_loaded_at_field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from dbt.tests.util import run_dbt, get_manifest, write_file
from dbt.exceptions import YamlParseDictError


loaded_at_field_null_schema_yml = """
Expand All @@ -18,6 +19,22 @@
loaded_at_field: null
"""

loaded_at_field_blank_schema_yml = """
sources:
- name: test_source
freshness:
warn_after:
count: 1
period: day
error_after:
count: 4
period: day
loaded_at_field: updated_at
tables:
- name: table1
loaded_at_field: null
"""

loaded_at_field_missing_schema_yml = """
sources:
- name: test_source
Expand Down Expand Up @@ -49,6 +66,22 @@
loaded_at_field: updated_at_another_place
"""

loaded_at_field_empty_string_schema_yml = """
sources:
- name: test_source
freshness:
warn_after:
count: 1
period: day
error_after:
count: 4
period: day
loaded_at_field: updated_at
tables:
- name: table1
loaded_at_field: ""
"""


class TestParsingLoadedAtField:
@pytest.fixture(scope="class")
Expand All @@ -75,9 +108,8 @@ def test_loaded_at_field(self, project):
manifest.sources.get("source.test.test_source.table1").loaded_at_field == "updated_at"
)

# test setting loaded_at_field to null explicitly again to make sure the change is picked up
# by parser
write_file(loaded_at_field_null_schema_yml, project.project_root, "models", "schema.yml")
# test setting loaded_at_field to nothing, should override Source value for None
write_file(loaded_at_field_blank_schema_yml, project.project_root, "models", "schema.yml")
run_dbt(["parse"])
manifest = get_manifest(project.project_root)

Expand All @@ -95,3 +127,10 @@ def test_loaded_at_field(self, project):
manifest.sources.get("source.test.test_source.table1").loaded_at_field
== "updated_at_another_place"
)

# test setting loaded_at_field at table level to an empty string - should error
write_file(
loaded_at_field_empty_string_schema_yml, project.project_root, "models", "schema.yml"
)
with pytest.raises(YamlParseDictError):
run_dbt(["parse"])

0 comments on commit 683785d

Please sign in to comment.