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

fix: dbt unit tests feat without proper context #10849

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 60 additions & 1 deletion core/dbt/context/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,9 @@
return event_time_filter

@abc.abstractmethod
def __call__(self, *args: str) -> Union[str, RelationProxy, MetricReference]:
def __call__(
self, *args: str
) -> Optional[Union[str, int, float, RelationProxy, MetricReference]]:
pass


Expand Down Expand Up @@ -1788,6 +1790,61 @@
return ""


class ExposureVarResolver(BaseResolver):
def __call__(self, *args) -> Any: # ?? -> Any??
n_args = len(args)
if n_args == 0 or n_args > 2:
raise RefArgsError(node=self.model, args=args)

Check warning on line 1797 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1797

Added line #L1797 was not covered by tests
var = args[0]
default = None if n_args == 1 else args[1]
# context is None using the same decision at
# generate_runtime_unit_test_context
return UnitTestVar(context={}, config=self.config, node=self.model)(var, default)


# Should we create a new copy of the `envvar` method, similar to what was done for
# `TextContext` and `ProviderContext`?
class ExposureEnvVarResolver(BaseResolver):
def __call__(self, *args) -> Optional[Union[int, float, str]]:
n_args = len(args)
if n_args == 0 or n_args > 2:
raise RefArgsError(node=self.model, args=args)

Check warning on line 1811 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1811

Added line #L1811 was not covered by tests
var = args[0]
default = None if n_args == 1 else args[1]

return_value = None
if var.startswith(SECRET_ENV_PREFIX):
raise SecretEnvVarLocationError(var)

Check warning on line 1817 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1817

Added line #L1817 was not covered by tests

env = get_invocation_context().env
if var in env:
return_value = env[var]

Check warning on line 1821 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1821

Added line #L1821 was not covered by tests
elif default is not None:
return_value = default

if return_value is not None:
# Save the env_var value in the manifest and the var name in the source_file
if self.model:
# If the environment variable is set from a default, store a string indicating
# that so we can skip partial parsing. Otherwise the file will be scheduled for
# reparsing. If the default changes, the file will have been updated and therefore
# will be scheduled for reparsing anyways.
self.manifest.env_vars[var] = (
return_value if var in env else DEFAULT_ENV_PLACEHOLDER
)
# the "model" should only be test nodes, but just in case, check
# TODO CT-211
if self.model.resource_type == NodeType.Test and self.model.file_key_name: # type: ignore[union-attr] # noqa
source_file = self.manifest.files[self.model.file_id]

Check warning on line 1838 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1838

Added line #L1838 was not covered by tests
# TODO CT-211
(yaml_key, name) = self.model.file_key_name.split(".") # type: ignore[union-attr] # noqa

Check warning on line 1840 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1840

Added line #L1840 was not covered by tests
# TODO CT-211
source_file.add_env_var(var, yaml_key, name) # type: ignore[union-attr]

Check warning on line 1842 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1842

Added line #L1842 was not covered by tests
return return_value
else:
raise EnvVarMissingError(var)

Check warning on line 1845 in core/dbt/context/providers.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/providers.py#L1845

Added line #L1845 was not covered by tests


class ExposureSourceResolver(BaseResolver):
def __call__(self, *args) -> str:
if len(args) != 2:
Expand Down Expand Up @@ -1830,6 +1887,8 @@
project,
manifest,
),
"var": ExposureVarResolver(None, exposure, project, manifest),
"env_var": ExposureEnvVarResolver(None, exposure, project, manifest),
}


Expand Down
3 changes: 2 additions & 1 deletion core/dbt/parser/unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ def parse_unit_test_case(self, test_case: UnitTestDefinition):

ctx = generate_parse_exposure(
unit_test_node, # type: ignore
self.root_project,
self.root_project, # config
self.manifest,
test_case.package_name,
)

get_rendered(unit_test_node.raw_code, ctx, unit_test_node, capture_macros=True)
# unit_test_node now has a populated refs/sources

Expand Down
Loading