diff --git a/pytest_dbt_adapter/sequences/data_test.yml b/pytest_dbt_adapter/sequences/data_test.yml index 579cd69..598b19c 100644 --- a/pytest_dbt_adapter/sequences/data_test.yml +++ b/pytest_dbt_adapter/sequences/data_test.yml @@ -1,12 +1,12 @@ name: data_test project: data_tests sequence: - - type: dbt - cmd: test - check: false - - type: run_results - length: fact.test.length - names: fact.test.names - attributes: - passing.status: pass - failing.status: fail + - type: dbt + cmd: test + check: false + - type: run_results + length: fact.test.length + names: fact.test.names + attributes: + passing.status: pass + failing.status: fail diff --git a/pytest_dbt_adapter/sequences/data_test_ephemeral_models.yml b/pytest_dbt_adapter/sequences/data_test_ephemeral_models.yml index 510c5da..d719449 100644 --- a/pytest_dbt_adapter/sequences/data_test_ephemeral_models.yml +++ b/pytest_dbt_adapter/sequences/data_test_ephemeral_models.yml @@ -1,21 +1,21 @@ name: data_test_ephemeral_models project: data_test_ephemeral_models sequence: - - type: dbt - cmd: seed - - type: run_results - length: fact.seed.length - - type: dbt - cmd: test - check: false - - type: run_results - length: fact.test.length - names: fact.test.names - attributes: - passing.status: pass - failing.status: fail - - type: dbt - cmd: run - - type: run_results - length: fact.run.length - names: fact.run.names + - type: dbt + cmd: seed + - type: run_results + length: fact.seed.length + - type: dbt + cmd: test + check: false + - type: run_results + length: fact.test.length + names: fact.test.names + attributes: + passing.status: pass + failing.status: fail + - type: dbt + cmd: run + - type: run_results + length: fact.run.length + names: fact.run.names diff --git a/pytest_dbt_adapter/spec_file.py b/pytest_dbt_adapter/spec_file.py index 0574d82..f0ad8ed 100644 --- a/pytest_dbt_adapter/spec_file.py +++ b/pytest_dbt_adapter/spec_file.py @@ -273,27 +273,45 @@ def _build_expected_attributes_dict( @staticmethod def _get_name( - unique_id: str + result: Dict[str, Any], + nodes: Dict[str, Any] ) -> 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] + """Given a run result get the unique_id and lookup the name from + a dict of nodes mapped to their unique_id. + """ + try: + unique_id = result['unique_id'] + except KeyError as exc: + raise DBTException( + f'Invalid result, missing required key {exc}' + ) from None + try: + return nodes[unique_id]['name'] + except KeyError as exc: + raise DBTException( + f'Invalid node, missing required key {exc}' + ) from None def step_run_results(self, sequence_item, tmpdir): - path = os.path.join(tmpdir, 'project', 'target', 'run_results.json') + run_results_path = os.path.join( + tmpdir, 'project', 'target', 'run_results.json') + manifest_path = os.path.join( + tmpdir, 'project', 'target', 'manifest.json') expect_exists = sequence_item.get('exists', True) - assert expect_exists == os.path.exists(path) + assert expect_exists == os.path.exists(run_results_path) if not expect_exists: return None try: - with open(path) as fp: - run_results_data = json.load(fp) + with open( + run_results_path + ) as results_fp, open( + manifest_path + ) as manifest_fp: + run_results_data = json.load(results_fp) + manifest_data = json.load(manifest_fp) except Exception as exc: raise DBTException( f'could not load run_results.json: {exc}' @@ -304,6 +322,13 @@ def step_run_results(self, sequence_item, tmpdir): raise DBTException( 'Invalid run_results.json - no results' ) from None + try: + nodes = manifest_data['nodes'] + except KeyError: + raise DBTException( + 'Invalid manifest.json - no nodes' + ) from None + if 'length' in sequence_item: expected = self.get_fact(sequence_item['length']) assert expected == len(results) @@ -312,13 +337,7 @@ def step_run_results(self, sequence_item, tmpdir): extra_results_ok = sequence_item.get('extra_results_ok', False) for result in results: - try: - unique_id = result['unique_id'] - name = self._get_name(unique_id) - except KeyError as exc: - raise DBTException( - f'Invalid result, missing required key {exc}' - ) from None + name = self._get_name(result, nodes) if (not extra_results_ok) and (name not in expected_names): raise DBTException( f'Got unexpected name {name} in results' @@ -334,14 +353,7 @@ def step_run_results(self, sequence_item, tmpdir): attributes = self._build_expected_attributes_dict(values) for result in results: - try: - unique_id = result['unique_id'] - name = self._get_name(unique_id) - except KeyError as exc: - raise DBTException( - f'Invalid result, missing required key {exc}' - ) from None - + name = self._get_name(result, nodes) if name in attributes: for key, value in attributes[name].items(): try: @@ -521,7 +533,8 @@ def step_relation_types(self, sequence_item): schemas.add(relation.without_identifier()) with self.adapter.connection_named('__test'): for schema in schemas: - found_relations.extend(self.adapter.list_relations_without_caching(schema)) + found_relations.extend( + self.adapter.list_relations_without_caching(schema)) for key, value in expected.items(): for relation in found_relations: diff --git a/requirements.txt b/requirements.txt index f92f962..506d241 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ -dbt-core>=0.17.0,<=0.18 +dbt-core>=0.19.0rc1 pytest typing_extensions>=3.7.4,<3.8 diff --git a/setup.py b/setup.py index 2894c00..e433850 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ def read(path): author="Fishtown Analytics", author_email="info@fishtownanalytics.com", url="https://github.com/fishtown-analytics/dbt-adapter-tests", - version='0.3.0', + version='0.4.0', package_data={ 'pytest_dbt_adapter': [ 'projects/*.yml',