Skip to content

Commit

Permalink
Add tests for specifcally checking the population of `SemanticModel.d…
Browse files Browse the repository at this point in the history
…epends_on` (#8226)
  • Loading branch information
QMalcolm authored Jul 31, 2023
1 parent f230e41 commit 7872f6a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tests/functional/semantic_models/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
metricflow_time_spine_sql = """
SELECT to_date('02/20/2023, 'mm/dd/yyyy') as date_day
"""

models_people_sql = """
select 1 as id, 'Drew' as first_name, 'Banin' as last_name, 'yellow' as favorite_color, true as loves_dbt, 5 as tenure, current_timestamp as created_at
union all
select 2 as id, 'Jeremy' as first_name, 'Cohen' as last_name, 'indigo' as favorite_color, true as loves_dbt, 4 as tenure, current_timestamp as created_at
union all
select 3 as id, 'Callum' as first_name, 'McCann' as last_name, 'emerald' as favorite_color, true as loves_dbt, 0 as tenure, current_timestamp as created_at
"""

models_people_metrics_yml = """
version: 2
metrics:
- name: number_of_people
label: "Number of people"
description: Total count of people
type: simple
type_params:
measure: people
meta:
my_meta: 'testing'
"""

semantic_model_people_yml = """
version: 2
semantic_models:
- name: semantic_people
model: ref('people')
dimensions:
- name: favorite_color
type: categorical
- name: created_at
type: TIME
type_params:
time_granularity: day
measures:
- name: years_tenure
agg: SUM
expr: tenure
- name: people
agg: count
expr: id
entities:
- name: id
type: primary
defaults:
agg_time_dimension: created_at
"""
52 changes: 52 additions & 0 deletions tests/functional/semantic_models/test_semantic_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from dbt.contracts.graph.manifest import Manifest
from dbt.exceptions import CompilationError
from dbt.tests.util import run_dbt


from tests.functional.semantic_models.fixtures import (
models_people_sql,
metricflow_time_spine_sql,
semantic_model_people_yml,
models_people_metrics_yml,
)


class TestSemanticModelDependsOn:
@pytest.fixture(scope="class")
def models(self):
return {
"people.sql": models_people_sql,
"metricflow_time_spine.sql": metricflow_time_spine_sql,
"semantic_models.yml": semantic_model_people_yml,
"people_metrics.yml": models_people_metrics_yml,
}

def test_depends_on(self, project):
manifest = run_dbt(["parse"])
assert isinstance(manifest, Manifest)

expected_depends_on_for_people_semantic_model = ["model.test.people"]

number_of_people_metric = manifest.semantic_models["semantic_model.test.semantic_people"]
assert (
number_of_people_metric.depends_on.nodes
== expected_depends_on_for_people_semantic_model
)


class TestSemanticModelUnknownModel:
@pytest.fixture(scope="class")
def models(self):
return {
"not_people.sql": models_people_sql,
"metricflow_time_spine.sql": metricflow_time_spine_sql,
"semantic_models.yml": semantic_model_people_yml,
"people_metrics.yml": models_people_metrics_yml,
}

def test_unknown_model_raises_issue(self, project):
with pytest.raises(CompilationError) as excinfo:
run_dbt(["parse"])
assert "depends on a node named 'people' which was not found" in str(excinfo.value)

0 comments on commit 7872f6a

Please sign in to comment.