Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Infer the node name from the unique_id #10

Merged
merged 1 commit into from
Jan 7, 2021
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
4 changes: 2 additions & 2 deletions pytest_dbt_adapter/sequences/data_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ sequence:
length: fact.test.length
names: fact.test.names
attributes:
passing.fail: false
failing.fail: true
passing.status: pass
failing.status: fail
4 changes: 2 additions & 2 deletions pytest_dbt_adapter/sequences/data_test_ephemeral_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ sequence:
length: fact.test.length
names: fact.test.names
attributes:
passing.fail: false
failing.fail: true
passing.status: pass
failing.status: fail
- type: dbt
cmd: run
- type: run_results
Expand Down
18 changes: 15 additions & 3 deletions pytest_dbt_adapter/spec_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ def _build_expected_attributes_dict(
attributes[name][keypath] = value
return attributes

@staticmethod
def _get_name(
unique_id: str
) -> str:
""" turn a unique_id into just a node name. """
# The last part of the unique_id is the name
# Example:
# Consider unique_id "test.dbt_test_project.failing"
# Then the name is "failing"
return unique_id.split(".")[-1]

def step_run_results(self, sequence_item, tmpdir):
path = os.path.join(tmpdir, 'project', 'target', 'run_results.json')

Expand Down Expand Up @@ -302,7 +313,8 @@ def step_run_results(self, sequence_item, tmpdir):

for result in results:
try:
name = result['node']['name']
unique_id = result['unique_id']
name = self._get_name(unique_id)
except KeyError as exc:
raise DBTException(
f'Invalid result, missing required key {exc}'
Expand All @@ -323,8 +335,8 @@ def step_run_results(self, sequence_item, tmpdir):

for result in results:
try:
node = result['node']
name = node['name']
unique_id = result['unique_id']
Copy link
Contributor

@jtcohen6 jtcohen6 Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we could write this in a way that works for pre-0.19 versions, too, with handling for either result.get('unique_id') or result.get('node', {}).get('unique_id').

Edit: Based on the necessary changes to attributes (switching fail to status), I'm less sure if there's a simple way we can rewrite this so it's backwards-compatible for pre-v0.19 versions. We may just need a new minor release that's compatible for v0.19-and-later versions only.

name = self._get_name(unique_id)
except KeyError as exc:
raise DBTException(
f'Invalid result, missing required key {exc}'
Expand Down