-
Notifications
You must be signed in to change notification settings - Fork 6
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
feat: new unit tests for dbt models and macros #108
Changes from all commits
ff11811
f97a4dc
105d81c
6ee4f95
87c7698
0a53cf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Run all tests & checks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we probably don't need this, as we're running dbt test here: https://github.com/openedx/aspects-dbt/blob/main/.github/workflows/coverage.yml#L47 Since dbt needs ClickHouse running, and alembic to have been run, we're just doing these tests in the Tutor / Aspects context there. |
||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
run_tests: | ||
name: tests | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.12 | ||
|
||
- name: Install requirements | ||
run: pip3 install -r requirements.txt | ||
|
||
- name: Run tests | ||
run: dbt test --profiles-dir $DBT_PROFILES_DIR |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ profile: "aspects" | |
model-paths: ["models"] | ||
analysis-paths: ["analyses"] | ||
test-paths: ["tests"] | ||
seed-paths: ["seeds"] | ||
seed-paths: ["seeds","seeds-test"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one I'm not sure how we would shoehorn into the Aspects context, we obviously don't want these seeds loaded outside of CI. We may need to find / add a way to override the dbt_project.yml in Aspects. 🤔 |
||
macro-paths: ["macros"] | ||
snapshot-paths: ["snapshots"] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{% macro drop_stale_tables(dryrun=False) %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's this for? :) Looks like it's not being used yet? |
||
|
||
{% set current_models = [] %} | ||
|
||
{% for node in graph.nodes.values() | selectattr( | ||
"resource_type", "in", ["model", "snapshot"] | ||
) %} | ||
{% do current_models.append(node.name) %} | ||
|
||
{% endfor %} | ||
|
||
{% set cleanup_query %} | ||
WITH MODELS_TO_DROP AS ( | ||
SELECT | ||
CASE | ||
WHEN TABLE_TYPE = 'BASE TABLE' THEN 'TABLE' | ||
WHEN TABLE_TYPE = 'VIEW' THEN 'VIEW' | ||
END AS RELATION_TYPE, | ||
CONCAT_WS('.', TABLE_SCHEMA, TABLE_NAME) AS RELATION_NAME | ||
FROM | ||
INFORMATION_SCHEMA.TABLES | ||
WHERE TABLE_SCHEMA = '{{ target.schema }}' | ||
AND upper(TABLE_NAME) NOT IN | ||
({%- for model in current_models -%} | ||
'{{ model.upper() }}' | ||
{%- if not loop.last -%} | ||
, | ||
{% endif %} | ||
{%- endfor -%}) | ||
) | ||
SELECT | ||
concat('DROP ',RELATION_TYPE,' ',RELATION_NAME,';') as DROP_COMMANDS | ||
FROM MODELS_TO_DROP | ||
|
||
{% endset %} | ||
{% do log(cleanup_query, info=True) %} | ||
{% set drop_commands = run_query(cleanup_query).columns[0].values() %} | ||
|
||
{% if drop_commands %} | ||
{% if dryrun | as_bool == False %} | ||
{% do log("Executing DROP commands...", True) %} | ||
{% else %} {% do log("Printing DROP commands...", True) %} | ||
{% endif %} | ||
{% for drop_command in drop_commands %} | ||
{% do log(drop_command, True) %} | ||
{% if dryrun | as_bool == False %} | ||
{% do run_query(drop_command) %} | ||
{% endif %} | ||
{% endfor %} | ||
{% else %} {% do log("No relations to clean.", True) %} | ||
{% endif %} | ||
|
||
{%- endmacro -%} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
unit_tests: | ||
- name: test_fact_enrollments | ||
model: fact_enrollments | ||
config: | ||
tags: 'ci' | ||
given: | ||
- input: ref('course_names') | ||
format: sql | ||
fixture: course_names | ||
- input: ref('enrollment_events') | ||
format: sql | ||
fixture: enrollment_events | ||
- input: ref('dim_user_pii') | ||
format: sql | ||
fixture: dim_user_pii | ||
expect: | ||
format: sql | ||
fixture: fact_enrollments_expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
unit_tests: | ||
- name: test_fact_grades | ||
model: fact_grades | ||
config: | ||
tags: 'ci' | ||
given: | ||
- input: ref('grading_events') | ||
format: sql | ||
fixture: grading_events | ||
- input: ref('course_block_names') | ||
format: sql | ||
fixture: course_block_names | ||
- input: ref('dim_user_pii') | ||
format: sql | ||
fixture: dim_user_pii | ||
- input: ref('course_names') | ||
format: sql | ||
fixture: course_names | ||
expect: | ||
format: sql | ||
fixture: fact_grades_expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
unit_tests: | ||
- name: test_macro_items_per_subsection | ||
model: int_pages_per_subsection | ||
config: | ||
tags: 'ci' | ||
given: | ||
- input: ref('dim_course_blocks') | ||
format: sql | ||
fixture: dim_course_blocks | ||
expect: | ||
format: sql | ||
fixture: items_per_subsection_expected | ||
|
||
|
||
- name: test_fact_navigation_dropoff | ||
model: fact_navigation_dropoff | ||
config: | ||
tags: 'ci' | ||
given: | ||
- input: ref('dim_course_blocks') | ||
format: sql | ||
fixture: dim_course_blocks | ||
- input: ref('fact_navigation') | ||
format: sql | ||
fixture: fact_navigation | ||
- input: ref('dim_user_pii') | ||
format: sql | ||
fixture: dim_user_pii | ||
expect: | ||
format: sql | ||
fixture: fact_navigation_dropoff_expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
unit_tests: | ||
- name: test_fact_watched_video_segments | ||
model: fact_watched_video_segments | ||
config: | ||
tags: 'ci' | ||
given: | ||
- input: ref('video_playback_events') | ||
format: sql | ||
fixture: video_playback_events | ||
- input: ref('dim_course_blocks_extended') | ||
format: sql | ||
fixture: dim_course_blocks_extended | ||
- input: ref('dim_user_pii') | ||
format: sql | ||
fixture: dim_user_pii | ||
expect: | ||
format: sql | ||
fixture: fact_watched_video_segments_expected |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
dbt-clickhouse==1.7.6 | ||
dbt-core==1.7.15 | ||
dbt-clickhouse==1.8.0 | ||
dbt-core==1.8 | ||
shandy-sqlfmt[jinjafmt]==0.21.2 | ||
dbt-coverage | ||
clickhouse-cityhash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should get rid of this one. You should be able to add
.DS_Store
to~/.gitignore
to remove them globally. In VS Code you'd need to turn on 2 options for it to recognize the global file: "Search: Use Global Ignore Files" and "Search: Use Ignore Files", I think that's what fixed those files getting auto-added for me.