Skip to content

Commit

Permalink
Implement simple unit test using fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Maria Lorena Rodriguez Viruel committed Aug 21, 2024
1 parent b4ecea6 commit 889c50f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 12 deletions.
8 changes: 4 additions & 4 deletions models/contacts/contacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ models:
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.document_metadata (uuid) ON DELETE CASCADE"
- type: unique
tests:
data_tests:
- not_null
- relationships:
to: ref('document_metadata')
Expand Down Expand Up @@ -51,7 +51,7 @@ models:
constraints:
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.contact (uuid) ON DELETE CASCADE"
tests:
data_tests:
- not_null
- relationships:
to: ref('contact')
Expand All @@ -72,7 +72,7 @@ models:
constraints:
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.contact (uuid) ON DELETE CASCADE"
tests:
data_tests:
- not_null
- relationships:
to: ref('contact')
Expand All @@ -91,7 +91,7 @@ models:
constraints:
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.contact (uuid) ON DELETE CASCADE"
tests:
data_tests:
- not_null
- relationships:
to: ref('contact')
Expand Down
2 changes: 1 addition & 1 deletion models/forms/forms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ models:
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.document_metadata (uuid) ON DELETE CASCADE"
- type: unique
tests:
data_tests:
- not_null
- relationships:
to: ref('document_metadata')
Expand Down
4 changes: 3 additions & 1 deletion models/root/document_metadata.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{% set import_couchdb_data = select_table("{{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}", ref('test_source_table')) %}

{{
config(
materialized = 'incremental',
Expand All @@ -18,7 +20,7 @@ SELECT
_deleted,
saved_timestamp,
doc->>'type' as doc_type
FROM {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} source_table
FROM {{ import_couchdb_data }}
{% if is_incremental() %}
WHERE source_table.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }}
{% endif %}
2 changes: 0 additions & 2 deletions models/root/root.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ models:
data_type: string
constraints:
- type: unique
tests:
- not_null
- name: saved_timestamp
data_type: timestamp
- name: _deleted
Expand Down
16 changes: 16 additions & 0 deletions models/root/tests/document_metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
unit_tests:
- name: test_document_metadata
description: |
This unit test validates the transformation logic in the `document_metadata` model
for the first test scenario, ensuring that the input data from `source_table` is correctly transformed.
model: document_metadata
overrides:
macros:
is_incremental: false
given:
- input: ref('test_source_table')
format: csv
fixture: contacts_test_source_table_input
expect:
format: csv
fixture: contacts_document_metadata_input
7 changes: 7 additions & 0 deletions models/root/tests/test_source_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ config(materialized='table') }}

select
null::timestamp as saved_timestamp,
null::text as _id,
null::boolean as _deleted,
null::jsonb as doc
2 changes: 1 addition & 1 deletion models/users/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ models:
- type: unique
- type: foreign_key
expression: "{{ env_var('POSTGRES_SCHEMA') }}.document_metadata (uuid) ON DELETE CASCADE"
tests:
data_tests:
- not_null
- relationships:
to: ref('contact')
Expand Down
8 changes: 5 additions & 3 deletions test/dbt/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ RUN flake8 dbt-run.py
FROM python:3 AS release

RUN pip install --upgrade cffi \
&& pip install cryptography~=3.4 \
&& pip install dbt-core dbt-postgres
&& pip install cryptography~=3.4 \
&& pip install dbt-core dbt-postgres

RUN adduser dbt
USER dbt

WORKDIR /dbt/
COPY test/profiles.yml profiles.yml
COPY test/profiles.yml profiles.yml
COPY dbt_project.yml dbt_project.yml
COPY packages.yml packages.yml
COPY models models
COPY macros macros
COPY test/fixtures tests/fixtures
COPY test/sqltest tests
COPY test/run_dbt_tests_docker.sh run_dbt_tests_docker.sh

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/contacts_document_metadata_input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
uuid,_deleted,saved_timestamp,doc_type
"1",false,"2024-08-01 00:00:00.000","person"
"2",false,"2024-08-01 00:00:00.000","person"
"3",false,"2024-08-02 00:00:00.000","clinic"
"4",true,"2024-08-02 00:00:00.000","person"
3 changes: 3 additions & 0 deletions test/fixtures/contacts_expect.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
uuid,name,phone,phone2,active,notes,contact_id,muted,parent_uuid,reported
"1","John Doe","1234567890","0987654321",true,"Test note","C123",false,"parent1","2021-08-01 00:00:00"
"2","Jane Doe","1234567890","0987654321",false,"Another note","C124",false,"parent2","2021-08-01 00:00:00"
5 changes: 5 additions & 0 deletions test/fixtures/contacts_test_source_table_input.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
saved_timestamp,_id,_deleted,doc
"2024-08-01 00:00:00.000","1",false,"{""type"": ""person"", ""name"": ""John Doe"", ""phone"": ""1234567890"", ""alternative_phone"": ""0987654321"", ""is_active"": ""true"", ""notes"": ""Test note"", ""contact_id"": ""C123"", ""muted"": ""false"", ""parent"": {""_id"": ""parent1""}, ""reported_date"": 1627804800000}"
"2024-08-01 00:00:00.000","2",false,"{""type"": ""person"", ""name"": ""Jane Doe"", ""phone"": ""1234567890"", ""alternative_phone"": ""0987654321"", ""is_active"": ""false"", ""notes"": ""Another note"", ""contact_id"": ""C124"", ""muted"": """", ""parent"": {""_id"": ""parent2""}, ""reported_date"": 1627804800000}"
"2024-08-02 00:00:00.000","3",false,"{""type"": ""clinic"", ""name"": ""Test Clinic"", ""phone"": ""1122334455"", ""alternative_phone"": ""5544332211"", ""is_active"": ""true"", ""notes"": ""Clinic note"", ""contact_id"": ""C125"", ""muted"": ""true"", ""parent"": {""_id"": ""parent3""}, ""reported_date"": 1627804800000}"
"2024-08-02 00:00:00.000","4",true,"{""type"": ""person"", ""name"": ""Deleted Contact"", ""phone"": ""0000000000"", ""alternative_phone"": ""1111111111"", ""is_active"": ""false"", ""notes"": ""This record should be deleted"", ""contact_id"": ""C126"", ""muted"": ""false"", ""parent"": {""_id"": ""parent4""}, ""reported_date"": 1627804800000}"

0 comments on commit 889c50f

Please sign in to comment.